使用autofac 39;s IIndex解析多个keyyed实例
本文关键字:实例 keyyed IIndex autofac 使用 | 更新日期: 2023-09-27 18:06:45
我想以状态模式和策略模式共存的方式使用AutoFac。经过研究,我熟悉了Autofac的keyyed/Named注册,并使用被动IIndex
方法将其用于我的状态。在此之后,我着眼于策略模式,这对我来说是使用相同理念的好方法,同时解决了State和Strategy的IIndex
。我以同样的方式保存了我的策略选项(enum
)作为状态,并将它们键入DependencyResolver
:
builder.RegisterType<NewAanvraag>().Keyed<IAanvraagState>(AanvraagState.Nieuw).Keyed<IAanvraagState>(BusinessState.Default);
builder.RegisterType<RareNewAanvraag>().Keyed<IAanvraagState>(AanvraagState.Nieuw).Keyed<IAanvraagState>(BusinessState.Rare);
builder.RegisterType<OpvoerenInformatie>().Keyed<IAanvraagState>(AanvraagState.OpvoerenInformatie).Keyed<IAanvraagState>(BusinessState.Default);
这样,我希望以动态顺序使用两个选项来创建,而有些实现可能与默认值相同,而有些则不是。然而,当试图访问状态和策略时,我得到了KeyedServiceIndex2 (DelegateActivator)
的概念,但两个选项都不能自行解决
private readonly IIndex<AanvraagState, IAanvraagState> _states;
private readonly IIndex<BusinessState, IAanvraagState> _strategyState;
public IAanvraagDto AanvraagDto { get; set; }
private IAanvraagState CurrentState{ get { return _states[AanvraagDto.State];} }
private IAanvraagState CurrentStrategy { get { return _strategyState[AanvraagDto.BusinessState]; } }
public Aanvraag(IIndex<AanvraagState, IAanvraagState> states, IIndex<BusinessState, IAanvraagState> strategyState)
{
_states = states;
_strategyState = strategyState;
}
public void Start()
{
CurrentStrategy.Start(AanvraagDto);
SetState(AanvraagState.OpvoerenInformatie);
}
当我尝试使用两者时,它找不到实现(也尝试了IIndex<BusinessState, IIndex<AanvraagState, IAanvraagState>>
):
private readonly IIndex<AanvraagState, IIndex<BusinessState, IAanvraagState>> _states;
public IAanvraagDto AanvraagDto { get; set; }
private IAanvraagState CurrentState { get { return _states[AanvraagDto.State][AanvraagDto.BusinessState]; } }
public Aanvraag(IIndex<AanvraagState, IIndex<BusinessState, IAanvraagState>> states)
{
_states = states;
}
public void Start()
{
CurrentState.Start(AanvraagDto);
SetState(AanvraagState.OpvoerenInformatie);
}
有没有人知道如何使用2键变量来检索网格结构来解决具体实现?
PS:这是我在StackOverflow上问的第一个问题,所以任何建设性的反馈都是非常感谢的。IIndex<K,V>
关系实际上只适用于单维键控服务。多维选择不行
您更可能寻找的是组件元数据,将任何任意数据与注册相关联并根据该数据选择注册的能力。
文档中有一些很好的示例和细节,但我将向您展示一个简单的示例,它可能非常适合您正在做的工作。
首先,需要定义一个元数据类。这将跟踪"矩阵"的各种"维度",你想通过它来选择你的组件。我将在这里做一些简单的事情——两个布尔字段,这样总共只有四种可用的元数据组合:
public class ServiceMetadata
{
public bool ApplicationState { get; set; }
public bool BusinessState { get; set; }
}
我将使用一些非常简单的空服务来说明。你的显然会做更多的事情。注意,我有四个服务——每个服务对应一个元数据组合。
// Simple interface defining the "service."
public interface IService { }
// Four different services - one for each
// combination of application and business state
// (e.g., ApplicationState=true, BusinessState=false).
public class FirstService : IService { }
public class SecondService : IService { }
public class ThirdService : IService { }
public class FourthService : IService { }
这里是您使用服务的地方。为了更容易地利用强类型元数据,您需要引用System.ComponentModel.Composition
,以便能够访问System.Lazy<T, TMetadata>
。
public class Consumer
{
private IEnumerable<Lazy<IService, ServiceMetadata>> _services;
public Consumer(IEnumerable<Lazy<IService, ServiceMetadata>> services)
{
this._services = services;
}
public void DoWork(bool applicationState, bool businessState)
{
// Select the service using LINQ against the metadata.
var service =
this._services
.First(s =>
s.Metadata.ApplicationState == applicationState &&
s.Metadata.BusinessState == businessState)
.Value;
// Do whatever work you need with the selected service.
Console.WriteLine("Service = {0}", service.GetType());
}
}
当您进行注册时,您需要将元数据与组件一起注册,以便它们知道它们属于哪个数据组合。
var builder = new ContainerBuilder();
builder.RegisterType<Consumer>();
builder.RegisterType<FirstService>()
.As<IService>()
.WithMetadata<ServiceMetadata>(m => {
m.For(sm => sm.ApplicationState, false);
m.For(sm => sm.BusinessState, false);
});
builder.RegisterType<SecondService>()
.As<IService>()
.WithMetadata<ServiceMetadata>(m => {
m.For(sm => sm.ApplicationState, false);
m.For(sm => sm.BusinessState, true);
});
builder.RegisterType<ThirdService>()
.As<IService>()
.WithMetadata<ServiceMetadata>(m => {
m.For(sm => sm.ApplicationState, true);
m.For(sm => sm.BusinessState, false);
});
builder.RegisterType<FourthService>()
.As<IService>()
.WithMetadata<ServiceMetadata>(m => {
m.For(sm => sm.ApplicationState, true);
m.For(sm => sm.BusinessState, true);
});
var container = builder.Build();
最后,您可以使用您的消费者类通过"矩阵"获得服务。这段代码:
using(var scope = container.BeginLifetimeScope())
{
var consumer = scope.Resolve<Consumer>();
consumer.DoWork(false, false);
consumer.DoWork(false, true);
consumer.DoWork(true, false);
consumer.DoWork(true, true);
}
将在控制台上产生这个:
Service = FirstService
Service = SecondService
Service = ThirdService
Service = FourthService
同样,您肯定需要查看文档以获取更多细节和示例。它将添加澄清并帮助您了解其他可用选项,从而可能使此操作更容易或在系统中更好地工作。