简体   繁体   English

UnitOfWork模式的通用存储库模式

[英]Generic Repository Pattern with UnitOfWork Pattern

I am trying to implement a generic repository pattern. 我正在尝试实现通用的存储库模式。 I found this site which I think its well explained. 我发现这个网站我觉得很好解释。 http://www.tugberkugurlu.com/archive/generic-repository-pattern-entity-framework-asp-net-mvc-and-unit-testing-triangle http://www.tugberkugurlu.com/archive/generic-repository-pattern-entity-framework-asp-net-mvc-and-unit-testing-triangle

My purpose is to save the developers some time and keystrokes and I know this will help me. 我的目的是节省开发人员一些时间和按键,我知道这将有助于我。

So I have 2 questions: 所以我有两个问题:
1. Is this a good approach or not, will I have some problems in the future? 1.这是一个好方法,将来我会遇到一些问题吗?
2. How can I combine it with Unitofwork pattern?, I cant create an instance of the abstract class of course, so the following code its invalid. 2.如何将它与Unitofwork模式结合使用?当然,我无法创建抽象类的实例,因此以下代码无效。

public class UnitOfWork : IDisposable
    {
        #region Private fields
        private readonly MyCompanyContext _context = new MyCompanyContext();
        private GenericRepository<MyCompanyContext, Task> _taskRepository;

        public GenericRepository<MyCompanyContext, Task> TaskRepository
        {
            get
            {
                return _taskRepository ??
                         (_taskRepository = new GenericRepository<MyCompanyContext, Task>());
            }
        }




namespace MyCompany.DAL.Repository
{
    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<C, T> :
    IGenericRepository<T>
        where T : class
        where C : DbContext, new()
    {

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

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

        public virtual IQueryable<T> GetAll()
        {

            IQueryable<T> query = _entities.Set<T>();
            return query;
        }

        public IQueryable<T> FindBy(System.Linq.Expressions.Expression<Func<T, bool>> predicate)
        {
            IQueryable<T> query = _entities.Set<T>().Where(predicate);
            return query;
        }

        public virtual void Add(T entity)
        {
            _entities.Set<T>().Add(entity);
        }

        public virtual void Delete(T entity)
        {
            _entities.Set<T>().Remove(entity);
        }

        public virtual void Edit(T entity)
        {
            _entities.Entry(entity).State = System.Data.EntityState.Modified;
        }

        public virtual void Save()
        {
            _entities.SaveChanges();
        }
    }
}

There are several opinions regarding repositories, but after trying various repository implementations in production for couple years myself, I agree with Ayende's opinion, that repository, especially generic, is redundant abstraction layer. 关于存储库有几种意见,但在我自己尝试生产各种存储库几年之后,我同意Ayende的观点,即存储库,特别是通用存储库,是冗余抽象层。

I liked very much this course: http://www.pluralsight-training.net/microsoft/Courses/TableOfContents/linq-architecture 我非常喜欢这门课程: http//www.pluralsight-training.net/microsoft/Courses/TableOfContents/linq-architecture

It walked through most possible solutions and explained goods and bads. 它走过了大多数可能的解决方案并解释了货物和坏处。

What we're using right now is very thin abstraction over datacontext, just to overcome Linq2Sql testability issues, which are irrelevant in most cases when using EF. 我们现在使用的是对datacontext的非常精简的抽象,只是为了克服Linq2Sql可测试性问题,这在大多数情况下使用EF时都是无关紧要的。

With a lot of effort you might get that working, but I wonder if the effort is really worth it? 经过很多努力你可能会有所作为,但我想知道这项努力是否值得呢? I've seen implementations like this before, and they really struggle when attempting to manage many-to-many relationships (have a think about how you'd manage that in your scenario). 我以前见过这样的实现,在尝试管理多对多关系时,他们真的很挣扎(考虑一下如何在你的场景中管理它)。 You are using Entity Framework, an ORM right? 您正在使用Entity Framework,ORM对吗? ORMs like Entity Framework and nHibernate are designed to abstract the database implementation from application code, so what is the purpose of adding yet another abstraction above it to manage entities at such a granular level? 像Entity Framework和nHibernate这样的ORM被设计用于从应用程序代码中抽象出数据库实现,那么在它上面添加另一个抽象来在如此精细的层次上管理实体的目的是什么呢? If it's a question of testing, then you can use a mocking framework to mock the context, thus removing the need for an actual database during testing. 如果这是一个测试问题,那么您可以使用模拟框架来模拟上下文,从而在测试期间不再需要实际的数据库。 If however, for architectural or security reasons you are seeking to remove interactions with a db context from your app code, I'd recommend for pragmatism using an implementation of the command pattern over the top of the entity framework. 但是,出于体系结构或安全性原因,您正试图从应用程序代码中删除与db上下文的交互,我建议在实体框架的顶部使用命令模式的实现实用主义。 I've needed to do this on a larger scale enterprise (banking) application where for security reasons (rightly or wrongly) we were absolutely not allowed to have a db connection in our application code. 我需要在更大规模的企业(银行)应用程序上执行此操作,出于安全原因(正确或错误),我们绝对不允许在应用程序代码中使用数据库连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将UnitOfWork与存储库模式一起使用 - Using UnitOfWork with the Repository Pattern 实体框架的存储库+ UnitOfWork模式 - Repository + UnitOfWork pattern for entity framework 将SQliteAsyncConnection与UnitOfWork和存储库模式一起使用 - Using SQliteAsyncConnection With UnitOfWork & Repository Pattern UnitOfWork +存储库模式-添加定制存储库 - UnitOfWork + Repository pattern - Adding a custom repository 是否可以将Linqpad与Repository和UnitOfWork模式一起使用? - Is it possible to use Linqpad with a Repository and UnitOfWork pattern? 关于在存储库模式中使用UnitOfWork的困惑 - Confusion over using of UnitOfWork in Repository Pattern 在实体框架中正确实现UnitOfWork和存储库模式 - Correct implementation of UnitOfWork and Repository pattern in Entity Framework 这是围绕DDD使用仓库模式设计的一个很好的UnitOfWork - Is this a good UnitOfWork with Repository pattern Design around DDD 使用UnitOfWork和存储库模式与实体框架 - using UnitOfWork and Repository Pattern with Entity Framework 在ASP.NET MVC 5中使用具有通用存储库模式UnitOfWork的继承接口的方法 - Using Method of Inherited Interface with Generic Repository Pattern, UnitOfWork in ASP.NET MVC 5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM