What does a repository look like when using many 1:Many or Many:Many tables?

血红的双手。 提交于 2019-12-07 11:48:07

问题


I have a MVC application that uses that entity framework model object directly in the view. I'm not sure how bad of a design pattern this is, but based on this sample I need to use a repository to implement caching.

namespace MVCAzureStore.Services.Caching
{
    public class CachedProductsRepository : CachedDataSource, IProductRepository
    {
        private readonly IProductRepository repository;

        public CachedProductsRepository(IProductRepository repository, ObjectCache cacheProvider) :
            base(cacheProvider, "Products")
        {
            this.repository = repository;
        }

        public List<string> GetProducts()
        {
            return RetrieveCachedData(
                "allproducts",  () => this.repository.GetProducts(),
                new CacheItemPolicy { AbsoluteExpiration = DateTime.UtcNow.AddMinutes(1) });
        }
    }
}

Since this view uses the navigation properties of the EF I need to replicate this notion(or have something similar in mine).

Question

How should I construct my repository when handling many tables that have a 1:Many or Many:Many relationship?

Any code sample, article, or more info would be appreciated, since I learn best that way


回答1:


All you should have to do is make sure repository.GetProducts() eagerly loads the appropriate navigation properties.




回答2:


See the repository in this tutorial, which allows you to specify eager loading for specified navigation properties: http://www.asp.net/entity-framework/tutorials/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application




回答3:


In most cases the repository is created for each business object. But this raises the problem of object loading which will not be done in case when lazy loading is disabled.



来源:https://stackoverflow.com/questions/8095728/what-does-a-repository-look-like-when-using-many-1many-or-manymany-tables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!