Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 4 web shop项目中的存储库和工作单元使用_Asp.net Mvc 4_Repository Pattern - Fatal编程技术网

Asp.net mvc 4 web shop项目中的存储库和工作单元使用

Asp.net mvc 4 web shop项目中的存储库和工作单元使用,asp.net-mvc-4,repository-pattern,Asp.net Mvc 4,Repository Pattern,我正在使用MVC4 razor语法、存储库和工作单元创建一个web商店。至少这是我想做的 我仍然对存储库和工作单元的使用有一些疑问,我希望我能在这里得到一些澄清 我找到了两个关于它的好教程 第一个: 第二条: 现在,我都读过了,我有点困惑。 在第一个类中,GenericRepository类是抽象的,TC和T.TC是DbContext对象,T是类。在第二个tut中,只需要一个类 为什么会有这些不同的方法? 是否因为第二个教程也使用了工作单元?是一个比另一个好,还是工作单元不一定好 在我的项目

我正在使用MVC4 razor语法、存储库和工作单元创建一个web商店。至少这是我想做的

我仍然对存储库和工作单元的使用有一些疑问,我希望我能在这里得到一些澄清

我找到了两个关于它的好教程

第一个:

第二条:

现在,我都读过了,我有点困惑。 在第一个类中,GenericRepository类是抽象的,TC和T.TC是DbContext对象,T是类。在第二个tut中,只需要一个类

为什么会有这些不同的方法? 是否因为第二个教程也使用了工作单元?是一个比另一个好,还是工作单元不一定好

在我的项目中,我有20个表,这是否意味着如果其中一些用于创建多语言站点,我必须创建20个存储库

最后,这些存储库是属于模型文件夹还是属于像“数据访问层”这样的单独文件夹? 如果是后者,使用存储库的模型文件夹中是否有任何内容

有人能提供清晰的信息吗? p.S.:我自己的存储库代码看起来像这样,并且可以工作(没有工作单元):

IGenericRepository

namespace ArtWebShop.Data_Access_Layer
{
    public interface IGenericRepository<T> where T : class
    {
        IQueryable<T> GetAll();
        IQueryable<T> FindBy(Expression<Func<T, bool>> predicate);
        void Add(T entity);
        void Delete(T entity);
        void Edit(T entity);
        void Save();
    }
}
public abstract class GenericRepository<TC, T> : IGenericRepository<T>
    where T : class
    where TC : DbContext, new()
{

    private TC _entities = new TC();
    public TC Context
    {

        get { return _entities; }
        set { _entities = value; }
    }

    public virtual IQueryable<T> GetAll()
    {

        IQueryable<T> query = _entities.Set<T>();
        return query;
    }
...
public interface IMenuRepository : IGenericRepository<menu_items_local>
{
    List<menu_items_local> GetMenuItemsByLanguage(string language);
}
public class MenuRepository : GenericRepository<ArtWebshopEntities, menu_items_local>, IMenuRepository
{
    public List<menu_items_local> GetMenuItemsByLanguage(string language)
    {
        var menuItemsLocal = Context.menu_items_local.Where(m => m.cultures.name == language);
        if (!menuItemsLocal.Any()) throw new HttpException(404, "menu items not found");

        return menuItemsLocal.ToList();
    }
}

在分析了这两种方法之后,我得出结论,主要区别在于,在第二个教程中使用了工作单元,实体上下文只声明了一次,而在第一个教程中,每个存储库中都提到了上下文实体

由于第一个教程不使用工作单元,因此不同的存储库不会存储在一个位置


总之,我可以说第二个教程是更好的方法

在分析了这两种方法之后,我得出结论,主要区别在于,在使用工作单元的第二个教程中,实体上下文只声明一次,而在第一个教程中,每个存储库中都会提到上下文实体

由于第一个教程不使用工作单元,因此不同的存储库不会存储在一个位置

总之,我可以说第二个教程是更好的方法

public class CommonController : Controller
{
    private readonly IMenuRepository _menuRepository;

    public CommonController()
    {
        this._menuRepository = new MenuRepository();
    }

    public CommonController(IMenuRepository menuRepository)
    {
        _menuRepository = menuRepository;
    }

    [HandleError]
    [ChildActionOnly]
    public ActionResult MenuItems(string language)
    {
        return PartialView("_menuPartial", _menuRepository.GetMenuItemsByLanguage(language));
    }
}