title: Repository pattern: Implementation and lazy loading of model relationships
body: <p>I have an application which deals with products and product categories. For each of these I have models defined using POCO.</p>
  
  <pre><code>// Represents a product.
  class Product {
    public virtual int ID { get; set; }
    public virtual string Name { get; set; }
    public virtual ProductCategory Category { get; set; }
  }
  
  // Represents a product category.
  class ProductCategory {
    public virtual int ID { get; set; }
    public virtual string Name { get; set; }
    public virtual IEnumerable&lt;Product&gt; Products { get; set; }
  }
  </code></pre>
  
  <p>The application uses a repository to access these models</p>
  
  <pre><code>// The interface implemented by the application's repository
  interface IProductRepository {
    IEnumerable&lt;Product&gt; GetAllProducts();
  
    void Add(Product product);
    void Remove(Product product);
    void Save(Product product);
  }
  </code></pre>
  
  <p>In the Product class, the property named Category of type ProductCategory should be loaded only when it is needed/accessed (lazy-loading). I want my models to remain POCO and contain only the structure of the model.</p>
  
  <p><strong>Am I taking the right approach ?</strong><br>
  Should I have only the ID of the category in the Product class and use a separate repository for product categories to load the category ?</p>
  
  <p><strong>Implementing lazy-loading and relationships</strong><br>
  For now my implementation of the repository interface returns an object of a type which extends the Product type and has support for lazy-loading through the repository instance.</p>
  
  <p>Who should be responsible for loading the product category ?</p>
  
  <p>I'm interested in how the product and category repositories should interact to achieve the lazy-loading ? Should they reference each other or should I have a main repository with the two sub repositories and pass that to my extended model types ?</p>
  
  <p><strong>What approach would you take ?</strong> (any suggestions and criticism is welcomed)</p>
  
  <hr>
  
  <p>I should note that I want the application to be extensible and all the interfaces for the repositories and the models themselves will be in a separate asembly. This means that the extender will not have direct access to the model class definition.</p>