Autofac.如何使用自定义方法(属性)解析某些界面? [英] Autofac. How to use custom method(property) to resolve some interface?

查看:144
本文介绍了Autofac.如何使用自定义方法(属性)解析某些界面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下接口:

public interface IConfigurationProvider<TSettings> where TSettings : ISettings, new() 
{
       TSettings Settings { get; }
}
public interface ISettings
{
}

我具有以下IConfigurationProvider实现:

I have the following implementation of IConfigurationProvider:

public class ConfigurationProvider<TSettings> : IConfigurationProvider<TSettings> where 

    TSettings : ISettings, new()
        {
            public ConfigurationProvider() 
            {
                this.BuildConfiguration();
            }

            public TSettings Settings { get; private set; }

            private void BuildConfiguration() 
            {
                this.Settings = new TSettings();
                //...load and assign properties to 'this.Settings'
                //...skipped
                // now 'Settings' property contains configured 'ISettings' instance
            }
        }

我也可以有不同的类来实现"ISettings"接口.例如,

I can also have distinct classes implementing 'ISettings' interface. For example,

public class UserSettings : ISettings
    {
        public int SomeProperty1 { get; set; }
        public int SomeProperty2 { get; set; }
    }


public class CatalogSettings : ISettings
    {
        public int SomeProperty3 { get; set; }
        public int SomeProperty4 { get; set; }
    }

我正在使用以下代码来配置"ContainerBuilder":builder.RegisterGeneric(typeof(ConfigurationProvider<>)).As(typeof(IConfigurationProvider<>));

I'm using the following code to configure 'ContainerBuilder': builder.RegisterGeneric(typeof(ConfigurationProvider<>)).As(typeof(IConfigurationProvider<>));

工作正常.为了获得"UserSettings",我使用以下代码:

It works fine. And in order to get 'UserSettings' I use the following code:

var userSettingsProvider = builder.Resolve<IConfigurationProvider<UserSettings>>();
var userSettings = userSettingsProvider.Settings;

问题:我应该如何配置"ContainerBuilder",以便可以通过以下方式解决某些"ISettings"问题:

The question: how should I configure 'ContainerBuilder' so I can resolve a certain 'ISettings' the following way:

var userSettings = builder.Resolve<UserSettings>();

Autofac可以吗?

Is it possible with Autofac?

预先感谢

推荐答案

您可以使用IRegistrationSource进行此操作-请参见

You can do this with an IRegistrationSource - see http://nblumhardt.com/2010/01/declarative-context-adapters-autofac2/ for an overview.

基本结构为:

class SettingsSource : IRegistrationSource {
    static readonly MethodInfo BuildMethod = typeof(SettingsSource).GetMethod(
        "BuildRegistration",
        BindingFlags.Static | BindingFlags.NonPublic);

    public IEnumerable<IComponentRegistration> RegistrationsFor(
            Service service,
            Func<Service, IEnumerable<IComponentRegistration>> registrations) {
        var ts= service as TypedService;
        if (ts != null && typeof(ISettings).IsAssignableFrom(ts.ServiceType) {
            var buildMethod = BuildMethod.MakeGenericMethod(ts.ServiceType);
            yield return (IComponentRegistration) buildMethod.Invoke(null, null);
        }
    }

    static IComponentRegistration BuildRegistration<TSettings>()
            where TSettings : ISettings {
        return RegistrationBuilder
            .ForDelegate((c, p) =>
                c.Resolve<IConfigurationProvider<TSettings>>().Settings)
            .CreateRegistration();
    }

    public bool IsAdapterForIndividualComponents { get { return false; } }
}

这是这样注册的:

builder.RegisterGeneric(typeof(ConfigurationProvider<>))
    .As(typeof(IConfigurationProvider<>));
builder.RegisterSource(new SettingsSource());

(未经编译或测试,请让我知道它是否掉下来;))

(Not compiled or tested, so let me know if it falls over ;))

这篇关于Autofac.如何使用自定义方法(属性)解析某些界面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆