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

Using Autofac's IIndex to resolve multiple Keyed instances

我想以状态和策略模式共存的方式使用 AutoFac。在研究了方法之后,我熟悉了 Autofac 的 Keyed/Named 注册,并使用被动 IIndex 方法将其用于我的状态。之后,我研究了 Strategy 模式,在我看来,它是使用相同想法的好方法,为 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 Keyed variables来检索grid-like结构来解析具体实现?

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

IIndex<K,V> 关系实际上只针对一维keyed services。它不适用于多维 selection.

您更有可能寻找的是 component metadata,将任意数据与注册相关联的能力以及 select 基于那个数据。

The documentation has some great examples and details,但我将向您展示一个可能与您的工作密切相关的简单示例。

首先,您需要定义一个元数据class。这是将跟踪 "matrix" 的各种 "dimensions" 的东西,您希望通过这些 select 您的组件。我将在这里做一些简单的事情——两个布尔字段,因此总共只有四种可用的元数据组合:

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

最后,您可以使用您的消费者 class 按您所说的 "matrix," 获得服务。此代码:

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. 它将增加说明并帮助您了解您可用的其他选项,这些选项可能会使这更容易或在您的系统中更好地工作。