依赖注入和实体框架

Lam*_*fif 2 c# wpf entity-framework dependency-injection mvvm

我正在使用MVVM灯和它的Ioc做一个wpf应用程序SimpleIoc.

我实现了这样的存储库模式:

 public interface ICrud<T> where  T : class 
    {
        IEnumerable<T> GetAll();
        Task<IEnumerable<T>> AsyncGetAll(); 
        void AddNew(params T[] items);
        void Delete(params T[] items);
        void Update(params T[] items);
        void SaveOrUpdate(params T[] items);
    }

 public class Crud<T> : ICrud<T> where T : class 
    {

        public void AddNew(params T[] items)
        {
            using (var context = new DataEntities())
            {
                foreach (T item in items)
                {
                    context.Entry(item).State = System.Data.Entity.EntityState.Added;
                }
                context.SaveChanges();
            }
        }

        public void Delete(params T[] items)
        {
            using (var context = new DataEntities())
            {
                foreach (T item in items)
                {
                    context.Entry(item).State = System.Data.Entity.EntityState.Deleted;
                }
                context.SaveChanges();
            }
        }

        public void Update(params T[] items)
        {
            using (var context = new DataEntities())
            {
                foreach (T item in items)
                {
                    context.Entry(item).State = System.Data.Entity.EntityState.Modified;
                }
                context.SaveChanges ();
            }
        }


        public  void SaveOrUpdate(params T[] items)
        {
            using (var context = new DataEntities())
            {
                foreach (T item in items)
                {
                    try
                    {
                        context.Entry(item).State = System.Data.Entity.EntityState.Modified;
                         context.SaveChanges();
                    }
                    catch (Exception)
                    {
                        context.Entry(item).State = System.Data.Entity.EntityState.Added;
                         context.SaveChanges();
                    }

                }

            }
        }

        public IEnumerable<T> GetAll()
        {
            using (var context = new DataEntities())
            {
                DbSet<T> dbSet = context.Set<T>(); 
                return dbSet.AsEnumerable().ToList();
            }
        }


        public Task<IEnumerable<T>> AsyncGetAll()
        {
            return Task.Factory.StartNew(() =>
            {

                    var context = new DataEntities();
                    DbSet<T> dbSet = context.Set<T>();
                    return dbSet.AsEnumerable();

            });
        }
    }
Run Code Online (Sandbox Code Playgroud)

在viewmodel定位器中,我注入了这样的依赖:

 static ViewModelLocator()
        {
            ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
            SimpleIoc.Default.Register<IDataService, DataService>();
             SimpleIoc.Default.Register<ICrud<student>, Crud<student>>();
            SimpleIoc.Default.Register<ICrud<prof>, Crud<prof>>();
//Add the other EF entities dependencies
        }
Run Code Online (Sandbox Code Playgroud)

我的问题是当我要执行crud操作时,我必须在调用之前实例化一个EF实体:

SimpleIoc.Default.GetInstance<ICrud<student>>().AddNew();
Run Code Online (Sandbox Code Playgroud)

我需要知道 :

  1. 如上所述的EF的实例化是否违反了依赖注入的概念
  2. 如果是这样,我该如何解决这个问题?

Ste*_*ven 5

你不应该注入实体.实体不是服务.使用依赖注入构建的对象图应仅由服务组成.任何包含运行时数据(实体,消息,DTO)的东西都应该使用方法调用通过构建的对象图传递.

看看这个以及这个答案和这篇博文的例子.

在单个类中混合数据和行为会使测试DI配置变得更加困难,并且难以应用横切关注点.但除此之外,将运行时数据(例如实体)注入服务的构造函数会导致模糊,因为不清楚要注入构造函数的确切实体.举个例子ICustomerService,它依赖于Customer构造函数中的实体.我们应该在这里注入哪个实体,因为我们可能有数千个.虽然这可以通过在我们创建ICustomerService实现的位置(我们的组合根)中实现选择标准来解决,但这使得DI配置非常复杂,使得很难验证配置并导致业务逻辑的一部分应用程序不应包含任何业务逻辑.