Skip to content

Using factory extension as a dictionary adapter

Regis Bittencourt edited this page Aug 3, 2015 · 2 revisions

It's possible to use the factory as a dictionary adapter using the IInstanceProvider:

public class AppSettingProvider : IInstanceProvider
{
    public object GetInstance(IInstanceResolver instanceResolver, MethodInfo methodInfo, object[] arguments)
    {
        string key = methodInfo.Name;
        if (key.StartsWith("get_"))
        {
            key = key.Substring(4);
        }

        return Convert.ChangeType(ConfigurationManager.AppSettings[key], methodInfo.ReturnType);
    }
}

Then you could use:

Kernel.Bind<ISettings>().ToFactory(() => new AppSettingProvider());

To bind a ISettings interface that the auto-properties will be automagically filled by the ConfigurationManager.AppSettings.

Source