更新时间:2022-11-05 09:38:50
的上下文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);
}