简体   繁体   English

使用autofac注册命名类型并使用名称作为字符串参数

[英]Registering a named type with autofac and using the name for string parameter

I have the following registrations (note that I haven't finished this code yey, so it may not even work as expected): 我有以下注册信息(请注意,我尚未完成此代码,所以它甚至可能无法按预期工作):

builder.RegisterType<SimpleInMemoryChannel>()
    .Named<IChannel>("ErrorChannel")
    .WithParameter(new NamedParameter("channelName", "ErrorChannel"));
builder.RegisterType<SimpleInMemoryChannel>()
    .Named<IChannel>("RequestCbrInput")
    .WithParameter(new NamedParameter("channelName", "RequestCbrInput"));

// Constructor: public SimpleInMemoryChannel(string channelName)

As you can see, I'm trying to use the name of the registered object for the channelName value. 如您所见,我正在尝试将注册对象的名称用作channelName值。 The code is a bit verbose. 该代码有点冗长。 Is there some way I can have that assignment happen automatically? 有什么办法可以让我自动进行分配? eg I'd like to just write: 例如,我只想写:

builder.RegisterType<SimpleInMemoryChannel>()
    .Named<IChannel>("ErrorChannel");
builder.RegisterType<SimpleInMemoryChannel>()
    .Named<IChannel>("RequestCbrInput");

and have the channelName set automatically. 并自动设置channelName。

There is nothing in the default functionality which would allow this to happen. 默认功能没有任何东西可以允许这种情况发生。 You will either need to customise your registration, or your resolving using a factory. 您将需要自定义注册,或使用工厂进行解析。 The simplest solution is the one you mentioned in your comment - add a helper function for registering the channel - that way you can still use the default resolution process. 最简单的解决方案是您在注释中提到的解决方案-添加用于注册频道的帮助程序功能-这样,您仍然可以使用默认解析过程。

Having a RegisterChannel method may be the more elegant solution. 使用RegisterChannel方法可能是更优雅的解决方案。

By the way, if you can't have such a method you can use a custom module : 顺便说一句,如果您没有这种方法,可以使用自定义模块:

public class NamedParameterModule<TServiceType> : Module
{
    private readonly string _parameterName;

    public NamedParameterModule(String parameterName)
    {
        this._parameterName = parameterName;
    }

    protected override void AttachToComponentRegistration(
        IComponentRegistry componentRegistry, IComponentRegistration registration)
    {
        KeyedService keyedService = registration.Services
            .OfType<KeyedService>()
            .FirstOrDefault(ks => ks.ServiceType == typeof(TServiceType));

        if (keyedService != null)
        {
            registration.Preparing += (sender, e) =>
            {
                e.Parameters = e.Parameters.Concat(new Parameter[] { 
                    new NamedParameter(this._parameterName, keyedService.ServiceKey)
                });
            };
        }

        base.AttachToComponentRegistration(componentRegistry, registration);
    }
}

And register it using builder.RegisterModule(new NamedParameterModule<IFoo>("channelName")); 并使用builder.RegisterModule(new NamedParameterModule<IFoo>("channelName"));

This this dotnetfiddle for live example : https://dotnetfiddle.net/MPfMup . 这是dotnetfiddle的实时示例: https ://dotnetfiddle.net/MPfMup。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM