20

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 suggestions?

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
user9969
  • 15,632
  • 39
  • 107
  • 175

3 Answers3

28

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

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.

Community
  • 1
  • 1
Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • thanks a lot for those links.I will look at them now.Do you have or seen any example where moq is used as mocking framework with codeFirst? Are there any end to end examples to download including db.I did see the first question but in your answer at some point I was not sure if needed or not.thanks for your time – user9969 Apr 23 '11 at 12:48
0

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/

Parama Dharmika
  • 108
  • 1
  • 2
  • 9
0
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; }
        }
    }
Thomas
  • 1,177
  • 1
  • 14
  • 26