Previously I have had parameterless repositories being injected into my MVC controllers:
以前,我曾将无参数存储库注入到我的MVC控制器中:
ProjectRepository
implementation:
ProjectRepository实现:
public class ProjectRepository : EntityFrameworkRepository<Project>, IProjectRepository
{
public ProjectRepository()
{ }
}
UnityConfig.cs
dependency resolution:
UnityConfig。cs依赖分辨率:
container.RegisterType<IProjectRepository, ProjectRepository>();
MVC Controller:
MVC控制器:
private IProjectRepository _projectRepository { get; set; }
public ProjectController(IProjectRepository projectRepository)
{
_projectRepository = projectRepository;
}
This worked great.
这个伟大的工作。
Now I have implemented a Unit of Work pattern into my repository classes so that I can commit transactional changes to data (especially when changes are being made to more than one repository).
现在我已经在我的存储库类中实现了一个工作模式单元,这样我就可以向数据提交事务更改(特别是在对多个存储库进行更改时)。
The new ProjectRepository
implementation accepts a IUnitOfWork
in its constructor:
新的ProjectRepository实现在其构造器中接受IUnitOfWork:
public class ProjectRepository : EntityFrameworkRepository<Project>, IProjectRepository
{
public ProjectRepository(IUnitOfWork unitOfWork): base(unitOfWork)
{ }
}
This means that multiple repositories can share the same IUnitOfWork
and changes can be collectively committed using UnitOfWork.SaveChanges()
.
这意味着多个存储库可以共享相同的IUnitOfWork,可以使用UnitOfWork.SaveChanges()集体提交更改。
QUESTION:
问题:
How do I now use dependency injection to instantiate the repository with an instance of IUnitOfWork?
我现在如何使用依赖项注入用IUnitOfWork实例实例实例化存储库?
public ProjectController(IProjectRepository projectRepository, IUnitOfWork unitOfWork)
{
_projectRepository = projectRepository;
_unitOfWork = unitOfWork;
}
There could also be more than one repository injected into the controller. How can these all be instantiated with the same IUnitOfWork?
还可以向控制器注入多个存储库。如何用相同的IUnitOfWork实例化这些?
1 个解决方案
#1
1
When you register your IUnitOfWork
instance, use PerResolveLifetimeManager
, this will ensure every dependency of IUnitOfWork
within a single IUnityContainer.Resolve
gets provided the same instance.
当您注册IUnitOfWork实例时,使用PerResolveLifetimeManager,这将确保在单个IUnityContainer中对IUnitOfWork的每个依赖。Resolve获得相同的实例。
For example:
例如:
public class SomeDependency
{
}
public class Service
{
public Service(SomeDependency someDependency, SomeDependency someDependency2)
{
Console.WriteLine(someDependency == someDependency2);
}
}
public static void Main()
{
using(var container = new UnityContainer())
{
container.RegisterType<SomeDependency>(new PerResolveLifetimeManager());
container.Resolve<Service>();
}
}
This will output True
to the Console.
这将输出True到控制台。
See the page for Understanding Lifetime Managers for further details.
有关更详细的信息,请参阅了解生命周期管理器的页面。
#1
1
When you register your IUnitOfWork
instance, use PerResolveLifetimeManager
, this will ensure every dependency of IUnitOfWork
within a single IUnityContainer.Resolve
gets provided the same instance.
当您注册IUnitOfWork实例时,使用PerResolveLifetimeManager,这将确保在单个IUnityContainer中对IUnitOfWork的每个依赖。Resolve获得相同的实例。
For example:
例如:
public class SomeDependency
{
}
public class Service
{
public Service(SomeDependency someDependency, SomeDependency someDependency2)
{
Console.WriteLine(someDependency == someDependency2);
}
}
public static void Main()
{
using(var container = new UnityContainer())
{
container.RegisterType<SomeDependency>(new PerResolveLifetimeManager());
container.Resolve<Service>();
}
}
This will output True
to the Console.
这将输出True到控制台。
See the page for Understanding Lifetime Managers for further details.
有关更详细的信息,请参阅了解生命周期管理器的页面。