Autofac。如何使用自定义方法(属性)来解决一些接口?

时间:2011-01-31 14:05:56

标签: c# autofac

我有以下界面:

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

我有以下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”界面。例如,

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

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

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

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

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

是否可以使用Autofac?

提前致谢

1 个答案:

答案 0 :(得分:6)

您可以使用IRegistrationSource执行此操作 - 请参阅http://nblumhardt.com/2010/01/declarative-context-adapters-autofac2/以获取概述。

基本结构如下:

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

(未编译或测试过,所以如果它失效请告诉我;))