我有以下接口:
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进行此操作-请参见 nblumhardt/2010/01/declarative-context-adapters-autofac2/进行概述.
You can do this with an IRegistrationSource - see nblumhardt/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.如何使用自定义方法(属性)解析某些界面?
发布评论