Is UnitOfWork and GenericRepository Pattern redundant In EF 4.1 code first?

后端 未结 3 1636
难免孤独
难免孤独 2020-11-27 15:15

Wondering if I need to use the Genericrepository pattern and UnitOfWork to mock the repository.I am using MOQ.Is it now redundant since I have noticed that EF 4.1 has IDBSet

相关标签:
3条回答
  • 2020-11-27 15:51
    public class MockDbSet<T> : IDbSet<T> where T : class,  new()
        {
            private List<T> _entities;
    
            public MockDbSet(List<T> entities)
            {
                _entities = entities;
            }
    
            public virtual T Add(T entity)
            {
                _entities.Add(entity);
                return entity;
            }
    
            public virtual T Attach(T entity)
            {
                _entities.Add(entity);
                return entity;
            }
    
            public TDerivedEntity Create<TDerivedEntity>() where TDerivedEntity : class, T
            {
                return new T() as TDerivedEntity;
            }
    
            public virtual T Create()
            {
                return new T();
            }
    
            public virtual T Find(params object[] keyValues)
            {
                throw new NotImplementedException();
            }
    
            public System.Collections.ObjectModel.ObservableCollection<T> Local
            {
                get
                {
                    return new ObservableCollection<T>(_entities);
                }
            }
    
            public virtual T Remove(T entity)
            {
                _entities.Remove(entity);
                return entity;
            }
    
            public IEnumerator<T> GetEnumerator()
            {
                return _entities.GetEnumerator();
            }
    
            System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
            {
                return _entities.GetEnumerator();
            }
    
            public Type ElementType
            {
                get { return _entities.AsQueryable().ElementType; }
            }
    
            public System.Linq.Expressions.Expression Expression
            {
                get { return _entities.AsQueryable().Expression; }
            }
    
            public IQueryProvider Provider
            {
                get { return _entities.AsQueryable().Provider; }
            }
        }
    
    0 讨论(0)
  • 2020-11-27 15:57

    In addition i want to add, that generic repository and unit of work on Entity Framework is redundant, check out this link http://rob.conery.io/2014/03/04/repositories-and-unitofwork-are-not-a-good-idea/

    0 讨论(0)
  • 2020-11-27 16:07

    This is duplicate of many topics already discussed on SO but I agree that some of them can be hard to find because they are nested in other question

    • What's the point of Generic repository in EF 4.1
    • Challenges with testable and mockable code in EF
    • Another question about challenges with mocking EF code
    • Implementing custom IDbSet
    • Another question resulting in discussion about repository and unit of work

    I hope this will give you some answers. If not, don't hesitate to ask for more information either here or in a new question.

    0 讨论(0)
提交回复
热议问题