Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

autofac

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

like image 311
Andrei M Avatar asked Jan 31 '11 14:01

Andrei M


1 Answers

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

like image 168
Nicholas Blumhardt Avatar answered Nov 15 '22 11:11

Nicholas Blumhardt