用单例实现依赖注入
Implementing Dependency Injection with a singleton
我将 WPF 与 Entity Framework 6
(数据库优先)、Caliburn.Micro
和 MEF
.
一起使用
我正在尝试在我的项目中实施 IoC
。我是 IoC
的绝对初学者,不幸的是,我找不到太多 MEF
与 Repository Pattern
一起使用的示例。
所以我有一些存储库,并且我创建了我的通用工作单元:
class GenericUoW : IUoW, IDisposable
{
protected readonly DbContext _ctx;
public GenericUoW(DbContext context)
{
_ctx = context;
}
public void Complete()
{
_ctx.SaveChanges();
}
public void Dispose()
{
_ctx.Dispose();
}
}
我的实际工作单元 classes 是这样实现的:
class InvoiceUoW : GenericUoW, IInvoiceUoW
{
public InvoiceUoW(DbContext _ctx) : base(_ctx)
{
salesrepo = new SaleRepository(_ctx);
itemsrepo = new ItemRepository(_ctx);
materialrepo = new MaterialRepository(_ctx);
raterepo = new RateRepository(_ctx);
clientrepo = new ClientRepository(_ctx);
taxrepo = new TaxRepository(_ctx);
stockhistoryrepo = new StockHistoryRepository(_ctx);
proformarepo = new ProformaRepository(_ctx);
}
public ISaleRepository salesrepo { get; private set; }
public IItemRepository itemsrepo { get; private set; }
public IMaterialRepository materialrepo { get; private set; }
public IRateRepository raterepo { get; private set; }
public IClientRepository clientrepo { get; private set; }
public ITaxRepository taxrepo { get; private set; }
public IStockHistoryRepository stockhistoryrepo { get; private set; }
public IProformaRepository proformarepo { get; private set; }
}
现在,在我的 ViewModel
中,我正在做这样的事情:
[Export(typeof(InvoiceViewModel))]
class InvoiceViewModel : PropertyChangedBase
{
#region ctor
[ImportingConstructor]
public InvoiceViewModel(IInvoiceUoW invoiceUoW)
{
_uow = invoiceUoW;
Clients = new BindableCollection<Client>(_uow.clientrepo.FetchAll());
}
#endregion
private readonly IInvoiceUoW _uow;
//other properties and methods
}
我的问题与 InvoiceUoW
中 IoC/DI
的使用有关。如何在 class 中实现 constructor injection
?因为 class 中的每个存储库都必须使用相同的 DataContext
进行实例化。如何使 DataContext
成为单例?请注意,我有几个 Unit of Work
classes,每个 viewmodel
.
注册要解析的 DbContext
实例并像单例一样运行:
var container = new CompositionContainer(); //Get it from where it belongs
var dbContext = container.GetExportedValue<DbContext>();
container.ComposeExportedValue<DbContext>(dbContext);
对于您的 UoW,MEF 的工作方式与 Unity 不同,在 MEF 中,您宁愿装饰实现类型以告诉容器它应该注册它:
[Export(typeof(IUnitOfWork))]
public class MyUnitOfWork : IUnitOfWork
我将 WPF 与 Entity Framework 6
(数据库优先)、Caliburn.Micro
和 MEF
.
我正在尝试在我的项目中实施 IoC
。我是 IoC
的绝对初学者,不幸的是,我找不到太多 MEF
与 Repository Pattern
一起使用的示例。
所以我有一些存储库,并且我创建了我的通用工作单元:
class GenericUoW : IUoW, IDisposable
{
protected readonly DbContext _ctx;
public GenericUoW(DbContext context)
{
_ctx = context;
}
public void Complete()
{
_ctx.SaveChanges();
}
public void Dispose()
{
_ctx.Dispose();
}
}
我的实际工作单元 classes 是这样实现的:
class InvoiceUoW : GenericUoW, IInvoiceUoW
{
public InvoiceUoW(DbContext _ctx) : base(_ctx)
{
salesrepo = new SaleRepository(_ctx);
itemsrepo = new ItemRepository(_ctx);
materialrepo = new MaterialRepository(_ctx);
raterepo = new RateRepository(_ctx);
clientrepo = new ClientRepository(_ctx);
taxrepo = new TaxRepository(_ctx);
stockhistoryrepo = new StockHistoryRepository(_ctx);
proformarepo = new ProformaRepository(_ctx);
}
public ISaleRepository salesrepo { get; private set; }
public IItemRepository itemsrepo { get; private set; }
public IMaterialRepository materialrepo { get; private set; }
public IRateRepository raterepo { get; private set; }
public IClientRepository clientrepo { get; private set; }
public ITaxRepository taxrepo { get; private set; }
public IStockHistoryRepository stockhistoryrepo { get; private set; }
public IProformaRepository proformarepo { get; private set; }
}
现在,在我的 ViewModel
中,我正在做这样的事情:
[Export(typeof(InvoiceViewModel))]
class InvoiceViewModel : PropertyChangedBase
{
#region ctor
[ImportingConstructor]
public InvoiceViewModel(IInvoiceUoW invoiceUoW)
{
_uow = invoiceUoW;
Clients = new BindableCollection<Client>(_uow.clientrepo.FetchAll());
}
#endregion
private readonly IInvoiceUoW _uow;
//other properties and methods
}
我的问题与 InvoiceUoW
中 IoC/DI
的使用有关。如何在 class 中实现 constructor injection
?因为 class 中的每个存储库都必须使用相同的 DataContext
进行实例化。如何使 DataContext
成为单例?请注意,我有几个 Unit of Work
classes,每个 viewmodel
.
注册要解析的 DbContext
实例并像单例一样运行:
var container = new CompositionContainer(); //Get it from where it belongs
var dbContext = container.GetExportedValue<DbContext>();
container.ComposeExportedValue<DbContext>(dbContext);
对于您的 UoW,MEF 的工作方式与 Unity 不同,在 MEF 中,您宁愿装饰实现类型以告诉容器它应该注册它:
[Export(typeof(IUnitOfWork))]
public class MyUnitOfWork : IUnitOfWork