如何编写从工作单元获取某种类型的存储库的方法
您好,有人可以告诉我如何编写一种方法来从工作单元获取某种类型的存储库吗?
所以我的工作单元是:
public class UnitOfWork : IUnitOfWork, IDisposable
{
private Context context = new Context();
private VectorCheckRepository<Invoice> _invoiceRepository;
private VectorCheckRepository<InvoiceLine> _invoiceLineRepository;
public virtual Repository<Invoice> InvoiceRepository
{
get
{
if (this._invoiceRepository == null) {
this._invoiceRepository = new VectorCheckRepository<Invoice>(context);
}
return _invoiceRepository;
}
}
public virtual VectorCheckRepository<InvoiceLine> InvoiceLineRepository
{
get
{
if (this._invoiceLineRepository == null) {
this._invoiceLineRepository = new VectorCheckRepository<InvoiceLine>(context);
}
return _invoiceLineRepository;
}
}
public void Save()
{
context.SaveChanges();
}
private bool disposed = false;
protected virtual void Dispose(bool disposing)
{
if (!this.disposed) {
if (disposing) {
context.Dispose();
}
}
this.disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
但是在运行时我想从基于类型的工作单元获取存储库。
假设我去了:
_unitOfWork.GetRepository(invoice);
我在这里要做的是想要取回 InvoiceRepository,因为我向它传递了一张发票。
或者:
_unitOfWork.GetRepository(invoiceLine);
我希望它返回 InvoiceLineRepository。
有谁知道如何实现这一目标?
Hi can anybody tell me how you could write a method to get a repository of a certain type from the unit of work?
So my unit of work is:
public class UnitOfWork : IUnitOfWork, IDisposable
{
private Context context = new Context();
private VectorCheckRepository<Invoice> _invoiceRepository;
private VectorCheckRepository<InvoiceLine> _invoiceLineRepository;
public virtual Repository<Invoice> InvoiceRepository
{
get
{
if (this._invoiceRepository == null) {
this._invoiceRepository = new VectorCheckRepository<Invoice>(context);
}
return _invoiceRepository;
}
}
public virtual VectorCheckRepository<InvoiceLine> InvoiceLineRepository
{
get
{
if (this._invoiceLineRepository == null) {
this._invoiceLineRepository = new VectorCheckRepository<InvoiceLine>(context);
}
return _invoiceLineRepository;
}
}
public void Save()
{
context.SaveChanges();
}
private bool disposed = false;
protected virtual void Dispose(bool disposing)
{
if (!this.disposed) {
if (disposing) {
context.Dispose();
}
}
this.disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
However at run time I want to get a repository from the unit of work based on a type.
So say I went:
_unitOfWork.GetRepository(invoice);
What I would be doing here is wanting to get back the InvoiceRepository because I passed it an invoice.
or:
_unitOfWork.GetRepository(invoiceLine);
I would want it to return the InvoiceLineRepository.
Does anyone know how to achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没有通用的方法可以做到这一点(除了一些丑陋的缓慢反射与命名约定相结合=没有编译时检查和容易破坏的解决方案)。我通过简单地创建带有大量 if 的方法或使用准备好的字典来实现此目的,在字典中我根据实体类型或动态创建的存储库类型获取整个存储库实例。
There is no generic way to do this (except some ugly slow reflection combined with naming conventions = no compile time check and easy to break solution). I did this by simply creating either method with a lot of
if
s or by using prepared dictionary where I got either whole repository instance based on entity type or repository type for dynamic creation.假设您的工作单元包装了一个 ObjectContext,您可以使用
CreateObjectSet
,它为您的存储库提供了要包装的数据源。例如:
您可以在此 MVC3 项目中看到一个示例:http://dl.dropbox。 com/u/13430676/employeetimecards.zip
该代码附带一篇关于使用 UoW 和存储库进行 EF 可测试性的文章:http://msdn.microsoft.com/en-us/library/ff714955.aspx
希望有帮助,
Assuming your unit of work wraps an ObjectContext, you can use
CreateObjectSet<T>
, which gives you the data source for your repository to wrap.For example:
You can see an example in this MVC3 project: http://dl.dropbox.com/u/13430676/employeetimecards.zip
The code accompanies an article on EF Testability using UoW and Repositories: http://msdn.microsoft.com/en-us/library/ff714955.aspx
Hope that helps,