实体框架4.1数据库首先依赖注入工作单元 [英] Entity Framework 4.1 Database First Dependency Injection Unit of Work

查看:178
本文介绍了实体框架4.1数据库首先依赖注入工作单元的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,也有使用单位与依赖注入code的工作首先,使用通用库和所有的好东西的例子吨。

Ok, there are tons of examples using unit of work with dependency injection for Code First, using generic repositories and all that good stuff.

没有任何人有一个例子与数据库优先(EDMX用的DbContext生成(T4)),存储过程的函数导入,与依赖注入工作单位这样做。

Does anyone have an example doing this with Database First (edmx with dbContext Generator (T4)), Stored Procedures as Function Imports, Unit of Work with dependency injection.

推荐答案

的上下文code第一或dbfirst将是相同的(的DbContext)。

The context for code first or dbfirst will be the same (DbContext).

存储过程映射,而不是调用context.Customers调用context.Database.Query(Proc_whatever)在你的仓库。

Stored procedures are mapped in your repository instead of calling context.Customers you call context.Database.Query("Proc_whatever").

有没有你想帮助的特定位置,我可能有它code样品,但一切都在上面做了相同的方式迪,code首先,通用仓库等的唯一改变来实现的UnitOfWork是要确保你的仓库不叫SaveChanges的,你对你的UnitOfWork接口的方法称为保存(),然后再将呼叫保存更改。

Is there a specific spot you want help on, I may have a code sample for it, but everything above is done the same way as the di, code first, generic repositories, etc. The only change to implement a UnitOfWork is to ensure your repositories don't call SaveChanges, you have a method on your UnitOfWork interface called Save() that in turn calls save changes.

我会更新code在的https:// github上。 COM / adamtuliper / EF5换实时网络应用来包括工作单位。我不喜欢的实施,有件事感觉不对,从而更加导致了我对CQRS我相信。

I'll update the code at https://github.com/adamtuliper/EF5-for-Real-Web-Applications to include a unit of work. I dont like the implementation though, something doesn't feel right, and thus is leading me more towards CQRS I believe.

所以这里的想法是: 注入IUnitOfWork IUnitOfWork包含IContext其也被注入并映射到一个语境。 IUnitOfWork映射到的UnitOfWork具体实施。 的UnitOfWork具体实施引用库:

So the idea here is: Inject IUnitOfWork IUnitOfWork contains an IContext which is also injected and mapped to a Context. IUnitOfWork maps to UnitOfWork concrete implementation. UnitOfWork concrete implementation references the repositories:

这是部分地从我的头顶,所以原谅任何编译错误,这是原则上显示

This is partially off the top of my head, so excuse any compilation errors, it's to show in principle


public class YourContext : DbContext, IContext
{
   //just a regular DbContext class except use IDbSet
   public IDbSet Customers { get; set; }
}

public interface IUnitOfWork
{
     ICustomerRepository CustomerRepository { get; }
     IOrderRepository OrderRepository { get; }
     void Save();
}


 public class UnitOfWork : IUnitOfWork, IDisposable
 {
        private readonly IContext _context;
        private ICustomerRepository _customerRepository;
        private IOrderRepository _orderRepository;
        private bool _disposed = false;

        public UnitOfWork(IContext context)
        {
            _context = context;
        }

        public ICustomerRepository CustomerRepository
        {
            get
            {
                if (this._customerRepository == null)
                {
                    this._customerRepository = new CustomerRepository(_context);
                }
                return _customerRepository;
            }
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!this._disposed)
            {
                if (disposing)
                {
                    ((IDisposable)_context).Dispose();
                }
            }
            this._disposed = true;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }


public class CustomerController : Controller
{
   private readonly IUnitOfWork _unitOfWork;
   public CustomerController(IUnitOfWork unitOfWork)
   {
      _unitOfWork = unitOfWork;
   }

   [AutoMap(typeof(Customer), typeof(CustomerIndexViewModel)]
   public ActionResult Index()
   {
        return _unitOfWork.CustomersRepository.GetAll();
        //or if not using AutoMapper, use the viewmodel directly:
        //return _unitOfWork.CustomersRepository.GetAll().Select(c => new CustomerIndexViewModel
                                                    {
                                                        CustomerId = c.CustomerId,
                                                        Address = c.Address,
                                                        City = c.City,
                                                        State = c.State,
                                                        FirstName = c.FirstName,
                                                        LastName = c.LastName
                                                    }).ToArray(); ;
   }
}

要使用一个进程,在CustomerRepository你做到以下几点:

To use a proc, in the CustomerRepository you'd do the following:


public Customer GetById(int id)
{
      return this.Context.Database.SqlQuery("Proc_GetCustomer @customerID", new SqlParameter("@customerID", id)).Single();
      //instead of:  return this.Context.Customers.Include(o => o.Orders).Single(o => o.CustomerId == id);
}

这篇关于实体框架4.1数据库首先依赖注入工作单元的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆