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

标签 c# autofac

我想以状态模式和策略模式共存的方式使用 AutoFac。在研究了方法之后,我熟悉了 Autofac 的键控/命名注册,并使用被动 IIndex 将其用于我的状态。方法。在此之后,我查看了 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来检索网格状结构来解析具体实现?

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

最佳答案

IIndex<K,V>关系真的只针对单维keyed services .它不适用于多维选择。

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

The documentation has some great examples and details ,但我会向您展示一个可能与您正在做的事情非常吻合的简单示例。

首先,您需要定义一个元数据类。这是将跟踪您要选择组件的“矩阵”的各种“维度”的东西。我将在这里做一些简单的事情 - 两个 bool 字段,因此总共只有四种可用的元数据组合:

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

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

关于c# - 使用 Autofac 的 IIndex 解析多个 Keyed 实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31858656/

相关文章:

c# - 定制的 Autofac 模块应该驻留在哪个程序集中?

asp.net-mvc-3 - 由于 HttpContext 不可用,无法创建请求生命周期范围

c# - IOC 容器可以替代 CreateInstance 反射代码吗?

c# - WPF 自定义窗口在设计时不应用其通用模板

c# - 如果目标类型未实现特定接口(interface),则让 Autofac 抛出

c# - WPF 可以在 winforms 工作的任何地方工作吗? (xp,vista, 7)

c# - Task.WaitAll 方法与 Parallel.Invoke 方法

.net - Autofac 错误 : Could not load file or assembly 'System.Web.Http, Version=5.2.0.0,...' My project is Owin WebApi2 SelfHost

c# - 帮助在提供商场景中选择我的 DDD 聚合根?

c# - 如何从datagridview textchanged事件中的当前单元格获取文本?