简体   繁体   中英

Autofac. How to use custom method(property) to resolve some interface?

I have the following interfaces :

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

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
            }
        }

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

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

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

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

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

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

Is it possible with Autofac?

Thanks in advance

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

The basic structure would be:

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

This is registered like so:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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