开发者

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

开发者 https://www.devze.com 2023-02-28 10:50 出处:网络
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.

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.

I have not figured out how to write something generic that usic IDBSet .If you have an example where you implement IDBSet can you show it to me?

Any sugges开发者_如何学运维tions?


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.


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; }
        }
    }


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

精彩评论

暂无评论...
验证码 换一张
取 消