在 EF 4.1 代码中,UnitOfWork 和 GenericRepository 模式是否多余?

发布于 2024-11-02 17:11:22 字数 181 浏览 6 评论 0 原文

想知道我是否需要使用 Genericrepository 模式和 UnitOfWork 来模拟存储库。我正在使用 MOQ。现在它是多余的吗,因为我注意到 EF 4.1 有 IDBSet。

我还没有弄清楚如何编写使用 IDBSet 的通用内容。如果您有一个实现 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 suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

望喜 2024-11-09 17:11:22

这是已经讨论过的许多主题的重复,但我同意其中一些主题可能很难找到,因为它们嵌套在其他问题中

我希望这能给您一些答案。如果没有,请随时在此处或在新问题中询问更多信息。

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.

抱着落日 2024-11-09 17:11:22
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; }
        }
    }
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; }
        }
    }
怕倦 2024-11-09 17:11:22

另外我想补充一点,实体框架上的通用存储库和工作单元是多余的,请查看此链接 http://rob.conery.io/2014/03/04/repositories-and-unitofwork-are-not-a-good-idea/

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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文