resolve particular implementation of IDependency

Topics: Core, General, Writing modules
Nov 23, 2015 at 4:29 PM
Hello
I have multiple implementations of interface which is IDependency. In code depends on passed param I need particular implementation which currently pick up by means of Name property in intrerface.
public interface IBrowser : IDependency
   {
        string Name { get; }
        // Other Methods
    }
public class BrowserService : IDependency {
       IEnumerable<IBrowser> browsers
       public BrowserService(IEnumerable<IBrowser> browsers) 
       {
       this.browsers = browsers;
       }

       object GetBrowserData(string BrowserName) 
       {
        var browser = browsers.First(b => b.Name == BrowserName);
        // getting browser data
       }
}
I see potential problem in initializing all implementation (we currently have ~ one hundred) which is useless in this usecase since I need only one.
Could anybody advise me how I can pick up specific one or may be it is not a problem at all?
Developer
Nov 23, 2015 at 5:00 PM
The simplest way is to give your IBrowser types a Name property, so you can inject an enumerable of all IBrowser objects and use a simple LINQ statement to get the service you want. Alternatively, you can take care of registering your IBrowser types yourself by implementing AutoFac's Module class.
Nov 24, 2015 at 3:49 PM
Thank you.
I have implemented last one with Autofac Module:
    public class BrowserModule : Module
    {
        protected override void AttachToComponentRegistration(Autofac.Core.IComponentRegistry componentRegistry, Autofac.Core.IComponentRegistration registration)
        {
            if (typeof(IBrowser).IsAssignableFrom(registration.Activator.LimitType))
            {
                var browserName = ReflectionHelper.GetStaticPropertyValue<string>(registration.Activator.LimitType, BrowseMetadataNames.Name);
                if (!String.IsNullOrEmpty(browserName))
                {
                    registration.Metadata[BrowseMetadataNames.Name] = browserName;
                }
                else
                {
                    throw new ArgumentException(registration.Activator.LimitType.ToString() + " should define public static property " + BrowseMetadataNames.Name);
                }
            }
        }
    }
in BrowserService
public class BrowserService : IBrowserService
{
        IEnumerable<Meta<Lazy<IBrowser>>> browsers;
        
        public BrowserService(IEnumerable<Meta<Lazy<IBrowser>>> browsers)
        {
            this.browsers = browsers;
        }

        private IBrowser GetBrowser(string browserName) 
        {
            IBrowser browser = null;
            var browserMeta = browsers.FirstOrDefault(b => b.Metadata.ContainsKey(BrowseMetadataNames.Name) && b.Metadata[BrowseMetadataNames.Name].ToString() == browserName);
            if (browserMeta != null)
            {
                browser = browserMeta.Value.Value;
            }
            return browser;
        }

}