如何使用Unity DI传递DbContext?

时间:2014-07-13 14:45:23

标签: asp.net-mvc entity-framework unity-container

我使用的是最新版本的 MVC EF &的统一

 public interface IUnitOfWork
    {
        IGenericRepository<Topic> TopicRepository { get; }
        bool Commit();
    }

以下是我的IUnitOfWork

  public class UnitOfWork : DbContext, IUnitOfWork
    {

        private GenericRepository<Topic> _topicRepository;
        private DbSet<Topic> Topics { get; set; }


        public IGenericRepository<Topic> TopicRepository
        {
            get { return _topicRepository ?? (_topicRepository = new GenericRepository<Topic>(Topics)); }
        }
        public bool Commit()
        {
            this.SaveChanges();
            return true;
        }
    }

...

public interface IGenericRepository<T> where T : class
    {
        IEnumerable<T> GetAll();
        IEnumerable<T> Find(Expression<Func<T, bool>> predicate);
        T Single(Expression<Func<T, bool>> predicate);
        T SingleOrDefault(Expression<Func<T, bool>> predicate);
        T First(Expression<Func<T, bool>> predicate);
        T GetById(int id);
        void Add(T entity);
        void Delete(T entity);

    }

... 

public class GenericRepository<T> : IGenericRepository<T> where T : class
    {

        private readonly DbSet<T> _dbSet;
        public GenericRepository(DbSet<T> dbSet)
        {
            this._dbSet = dbSet;
        }
        public IEnumerable<T> GetAll()
        {
            return _dbSet;
        }

        public IEnumerable<T> Find(Expression<Func<T, bool>> predicate)
        {
            return this._dbSet.Where(predicate);
        }

        public T Single(Expression<Func<T, bool>> predicate)
        {
            return this._dbSet.Where(predicate).Single();
        }

        public T SingleOrDefault(Expression<Func<T, bool>> predicate)
        {
            return this._dbSet.Where(predicate).SingleOrDefault();
        }

        public T First(Expression<Func<T, bool>> predicate)
        {
            return this._dbSet.Where(predicate).First();
        }

        public T GetById(int id)
        {
            return this._dbSet.Find(id);
        }

        public void Add(T entity)
        {
            _dbSet.Add(entity);
        }

        public void Delete(T entity)
        {
            _dbSet.Remove(entity);
        }
    }

...

UnityConfig.cs

public static void RegisterTypes(IUnityContainer container)
        {
            // NOTE: To load from web.config uncomment the line below. Make sure to add a Microsoft.Practices.Unity.Configuration to the using statements.
            // container.LoadConfiguration();

            // TODO: Register your types here
            // container.RegisterType<IProductRepository, ProductRepository>();
            container.RegisterType<DbContext>();
            //container.RegisterType<DbContext, MFRContext>(); 
            container.RegisterType<IUnitOfWork, UnitOfWork>(new HierarchicalLifetimeManager());
            container.RegisterType(typeof(IGenericRepository<>), typeof(GenericRepository<>));
        }

...

在我的HomeController中,我有这个:

public class HomeController : Controller
    {

        private UnitOfWork _uow;
        public ActionResult Index(UnitOfWork uow)
        {
            _uow = uow;

            _uow.TopicRepository.Add(new Topic { Title = "Title 1", Post = "Post 2", CreatedOn = DateTime.Now, CreatedBy = "Ad" });

            _uow.TopicRepository.GetAll();


            return View();
        }
}
  1. 应该注入哪个班级DbContext
  2. 如何配置Unity IUnitOfWork注入HomeController

1 个答案:

答案 0 :(得分:0)

尝试在构造函数中注入工作单元:

public class HomeController : Controller
{
    private UnitOfWork _uow;

    public HomeController(IUnitOfWork uow)
    {
        _uow = uow;
    }

    public ActionResult Index()
    {
        _uow.TopicRepository.Add(new Topic { Title = "Title 1", Post = "Post 2", CreatedOn = DateTime.Now, CreatedBy = "Ad" });

        _uow.TopicRepository.GetAll();


        return View();
    }
}