C#/ASP.NET MVC 4 Instantiate Object Derived From Interface In Factory Method

Posted by Chris on Stack Overflow See other posts from Stack Overflow or by Chris
Published on 2012-10-23T10:39:02Z Indexed on 2012/10/23 11:00 UTC
Read the original article Hit count: 302

Currently have a Factory class that features a GetSelector function, which returns a concrete implementation of ISelector. I have several different classes that implement ISelector and based on a setting I would like to receive the appropriate ISelector back.

public interface ISelector
{
    string GetValue(string Params);
}

public class XmlSelector : ISelector
{
    public string GetValue(string Params)
    {
        // open XML file and get value
    }
}

public static class SelectorFactory
{
    public static ISelector GetSelector()
    {
        return new XmlSelector(); // Needs changing to look at settings
    }
}

My question is what is the best way to store the setting? I am aware of using AppSettings etc. but I'm not sure whether I want to have to store strings in the web.config and perform a switch on it - just seems to be really tightly coupled in that if a new implementation of ISelector is made, then the Factory would need to be changed. Is there any way of perhaps storing an assembly name and instantiating based on that?

Thanks,

Chris

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about c#-4.0