Unity Dependency Injection 如何将映射名称传递给在父对象构造函数中解析的子对象
Unity Dependency Injection how to pass mapping name into sub objects resolved in parent objects constructor
我有以下问题
我正在开发一项 Windows 服务,我将存储库注入实体以进行数据检索。但是我有几个类似风格的存储库,这些存储库将使用服务在(运行时变量)
下的 运行 的上下文来解决
下面是我正在写的内容的精简示例
public static void Main() {
IUnityContainer container = new UnityContainer();
container.RegisterType<IRepo, RepoBase>(new ContainerControlledLifetimeManager());
container.RegisterType<IRepo, RepoA>("processA", new ContainerControlledLifetimeManager());
container.RegisterType<IRepo, RepoB>("processB", new ContainerControlledLifetimeManager());
container.RegisterType<IEntity, EntityA1>("ProcessA");
container.RegisterType<IEntity, EntityA2>("ProcessB");
var processType = "ProcessB"; //this is read from a queue and changes with every request the service processes
var entityInstance = container.Resolve<IEntity>(ProcessType);
entityInstance.DoSomething() }
public RepoBase : IRepo {
public virtual object ReturnData() {return baseData} }
public RepoA : RepoBase {
public override object ReturnData() {
var x = base.ReturnData();
return x + RepoAData; } }
public RepoB : RepoBase {
public override object ReturnData() { return RepoBData; } }
public class EntityA1 : IEntity {
public EntityA1(IRepo repo){...}
public void DoSomething() {
var data = repo.ReturnData();
//....Do something with data } }
public class EntityA2 : IEntity {
public EntityA2(IRepo repo){...}
public void DoSomething() {
var data = repo.ReturnData();
//....Do something with data } }
我遇到的问题是,当我解析 IEntity 时,我想将上下文 processType 传递给 IRepo 的解析,以便它解析为 ProcessA 的 RepoA,ProcessB 的 RepoB 等。
目前它解析为 RepoBase,因为没有传入上下文。
此外,我认为我可以通过向实体构造函数签名添加依赖属性来做到这一点,例如
public EntityA1([Dependency("ProcessA")] IRepo repo) {....}
但是我不想让我的 类 乱丢这个属性,因为它将我的服务与我的依赖注入 registration/resolution 等紧密结合在一起,我想将它们分开并放在一个中心位置(服务层,而不是业务逻辑层或更下层)。
如有任何关于如何在注册时执行此操作的建议,我将不胜感激。
谢谢
我会去工厂:
var entityInstance = myFactory.CreateEntity(ProcessType);
和
internal class EntityFactory
{
public EntityFactory( IUnityContainer container )
{
_container = container;
}
public IEntity CreateEntity( string processType )
{
return _container.Resolve<IEntity>( processType, new ParameterOverride( "repo", _container.Resolve<IRepo>( processType ) ) );
}
private readonly IUnityContainer _container;
}
经过多次尝试,我找到了解决方法
_container.RegisterType<IEntity, EntityA1>("processA",
new InjectionConstructor(new ResolvedParameter<IRepo>("processA")));
然后解析 IRepo 的正确映射
var entityInstance = container.Resolve<IEntity>(processType);
这实现了我希望在注册时澄清这一点而不是解决。
如果有人对上述内容有任何改进,我将不胜感激
谢谢
我有以下问题
我正在开发一项 Windows 服务,我将存储库注入实体以进行数据检索。但是我有几个类似风格的存储库,这些存储库将使用服务在(运行时变量)
下的 运行 的上下文来解决下面是我正在写的内容的精简示例
public static void Main() {
IUnityContainer container = new UnityContainer();
container.RegisterType<IRepo, RepoBase>(new ContainerControlledLifetimeManager());
container.RegisterType<IRepo, RepoA>("processA", new ContainerControlledLifetimeManager());
container.RegisterType<IRepo, RepoB>("processB", new ContainerControlledLifetimeManager());
container.RegisterType<IEntity, EntityA1>("ProcessA");
container.RegisterType<IEntity, EntityA2>("ProcessB");
var processType = "ProcessB"; //this is read from a queue and changes with every request the service processes
var entityInstance = container.Resolve<IEntity>(ProcessType);
entityInstance.DoSomething() }
public RepoBase : IRepo {
public virtual object ReturnData() {return baseData} }
public RepoA : RepoBase {
public override object ReturnData() {
var x = base.ReturnData();
return x + RepoAData; } }
public RepoB : RepoBase {
public override object ReturnData() { return RepoBData; } }
public class EntityA1 : IEntity {
public EntityA1(IRepo repo){...}
public void DoSomething() {
var data = repo.ReturnData();
//....Do something with data } }
public class EntityA2 : IEntity {
public EntityA2(IRepo repo){...}
public void DoSomething() {
var data = repo.ReturnData();
//....Do something with data } }
我遇到的问题是,当我解析 IEntity 时,我想将上下文 processType 传递给 IRepo 的解析,以便它解析为 ProcessA 的 RepoA,ProcessB 的 RepoB 等。
目前它解析为 RepoBase,因为没有传入上下文。
此外,我认为我可以通过向实体构造函数签名添加依赖属性来做到这一点,例如
public EntityA1([Dependency("ProcessA")] IRepo repo) {....}
但是我不想让我的 类 乱丢这个属性,因为它将我的服务与我的依赖注入 registration/resolution 等紧密结合在一起,我想将它们分开并放在一个中心位置(服务层,而不是业务逻辑层或更下层)。
如有任何关于如何在注册时执行此操作的建议,我将不胜感激。
谢谢
我会去工厂:
var entityInstance = myFactory.CreateEntity(ProcessType);
和
internal class EntityFactory
{
public EntityFactory( IUnityContainer container )
{
_container = container;
}
public IEntity CreateEntity( string processType )
{
return _container.Resolve<IEntity>( processType, new ParameterOverride( "repo", _container.Resolve<IRepo>( processType ) ) );
}
private readonly IUnityContainer _container;
}
经过多次尝试,我找到了解决方法
_container.RegisterType<IEntity, EntityA1>("processA",
new InjectionConstructor(new ResolvedParameter<IRepo>("processA")));
然后解析 IRepo 的正确映射
var entityInstance = container.Resolve<IEntity>(processType);
这实现了我希望在注册时澄清这一点而不是解决。
如果有人对上述内容有任何改进,我将不胜感激
谢谢