[英]How do I use Dependency Injection with Unit of Work and Repositories in C#? (Not a web based App)
I see a lot of questions and answers on this topic, however the vast majority are dealing with ASP.Net
or other web based applications and something called .InRequestScope
. 我对此主题有很多疑问和答案,但是绝大多数都在处理ASP.Net
或其他基于Web的应用程序以及.InRequestScope
。 I have yet to find this method in Ninject
with a Windows Application
. 我尚未在Windows Application
Ninject
找到此方法。
I have the usual Unit of Work
(UoW) and Repository
(Repo) classes and Interfaces, but I am wanting to inject the same DbContext
into both, each time a UoW is run from the DIContainer
. 我有通常Unit of Work
(UoW)和Repository
(Repo)类和接口,但是每次从DIContainer
运行DbContext
时,我DbContext
将相同的DbContext
注入两者。 My code looks like this; 我的代码看起来像这样;
public class UnitOfWork : IUnitOfWork, IDisposable
{
private readonly FinancialContext _context;
private IAccountRepository _accountRepository;
public IAccountRepository Accounts
{
get { return _accountRepository; }
}
UnitOfWork(IMyContext context, IAccountRepository accountRepository)
{
_context = context;
_accountRepository = accountRepository;
}
public void SaveChanges()
{
_context.SaveChanges();
}
public void Dispose()
{
_context.Dispose();
}
}
public class AccountRepository : Repository<Account>, IAccountRepository
{
public AccountRepository(IMyContext context) : base(context) { }
}
The DIContainer holds the following associations; DIContainer拥有以下关联;
Bind<IUnitOfWork>().To<UnitOfWork>().InTransientScope();
Bind<IUnitOfWorkFactory>().ToFactory();
Bind<IMyContext>().To<MyContext>().InSingletonScope();
Bind<IAccountTypeRepository>().To<AccountTypeRepository>().InTransientScope();
I'll come back to the .InSingletonScope(); 我将回到.InSingletonScope();
The way I have seen people do this normally has been in the UoW Properties for each Repo to have code to this effect; 我见过人们通常这样做的方式是在UoW Properties中,每个回购都具有实现此目的的代码。
private IAccountRepository _accountRepository;
public IAccountRepository Accounts
{
get
{
if(_accountRepository = null)
{
_accountRepository = new AccountRepository(_context);
}
return _accountRepository;
}
}
And remove the injected repositories from the Constructor
, there by ensuring that each instance of a repository using the same _context. 然后通过确保存储库的每个实例使用相同的_context,从Constructor
删除注入的存储库。
However in my mind this breaks the Dependency Injection
for this class. 但是在我看来,这打破了此类的Dependency Injection
。 Is there a way to do this where each creation of a UoW
like so; 有没有一种方法可以在每次创建UoW
这样做?
public TestUnitOfWork(IUnitOfWorkFactory unitOfWork)
{
using (var UoW = unitOfWork.Create())
{
Work done on UoW...
}
}
Currently the .InSingletonScope
allows this, but is this keeping an instance of the context always open? 当前.InSingletonScope
允许这样做,但是这是否使上下文实例始终处于打开状态? Introducing the errors associated with not disposing a context properly? 引入与未正确处理上下文有关的错误?
Or is it better to create a Factory
for the Repositories
and give them a context parameter, then in the properties initialise it like so; 还是为Repositories
创建一个Factory
并给它们一个上下文参数,然后在属性中像这样初始化它更好;
private IAccountRepository _accountRepository;
public IAccountRepository Accounts
{
get
{
if(_accountRepository = null)
{
_accountRepository = RepositoryFactory.CreateAccountRepository(_context);
}
return _accountRepository;
}
}
Thanks in advance for any help! 在此先感谢您的帮助!
The solution is the use Ninject's Extensions.Factory
class and pass in an IAccountFactory.Create()
to initialise a new object. 解决方案是使用Ninject's Extensions.Factory
类,并传入IAccountFactory.Create()
初始化一个新对象。 This then uses the DI Container to resolve its dependencies and doesn't break the DI approach. 然后,这将使用DI容器解析其依赖性,并且不会破坏DI方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.