使用Autofac的IIndex解析多个Keyed实例

时间:2015-08-06 14:37:38

标签: c# autofac

我希望以状态和策略模式共存的方式使用AutoFac。在研究了如何之后,我熟悉了Autofac的Keyed / Named注册,并使用被动IIndex方法将其用于我的状态。在此之后,我正在研究战略模式,对我来说,这似乎是一种使用相同想法的好方法,同时为状态和策略解析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个Keyed变量来检索类似网格的结构以解决具体实现?

PS:这是我在StackOverflow上提出的第一个问题,因此非常感谢任何建设性的反馈。

1 个答案:

答案 0 :(得分:2)

IIndex<K,V>关系实际上只适用于单维keyed services。它不适用于多维选择。

您更有可能寻找的是component metadata ,能够将任意数据与注册相关联,并根据该数据选择注册。

The documentation has some great examples and details,但我会向您展示一个可能与您正在做的事情密切相关的简单示例。

首先,您需要定义元数据类。这是追踪各种维度的因素。 &#34;矩阵&#34;您想要选择您的组件。我在这里做一些简单的事情 - 两个布尔字段,因此只有四种可用元数据的总组合:

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();

最后,您可以使用您的消费者类通过&#34;矩阵来获取服务,&#34;正如你所说。这段代码:

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

Again, you'll definitely want to check out the documentation for additional details and examples. 它将添加说明并帮助您了解您可以使用的其他选项,以便在您的系统中更轻松或更好地工作。