I have an interface and I wish to do a factory pattern to instantiate different implementor of the interface. But I wish to keel the details of which implementor to be instantiated in the config file. I shall like to have it in the fashion of RoleProvider configuration:
<section name="MembershipProvider" type="MyOwn.UserManagement.Providers.MembershipProvider.CustomMembershipProviderConfigurationSection,MyOwn.UserManagement.Providers.MembershipProvider" allowDefinition="MachineToApplication" />
So its kind of an dependency injection technique. Can somebody help me?
Edit:
The actual situation is like this:
I have an interface IDataExchange and want to implement it with different assemblies for different customers. So I might have two assemblies that have classes implementing IDataExchange say, DataExchange1 and DataExchange2. Now when I deploy, depending upon to which customer I am providing it, I shall like to set the details of the assembly (either DataExchange1 or DataExchange2) in the config file. This will also allow me or any developer to write new assemblies implementing IDataExchange for any change required, if the instantiation decision is handled automatically.
So how can I do it?
My implementation:
Thanks to all of you. And special thanks to @Pauli Østerø
I have taken cue from your answers and implemented a solution.
In the config file I added the following:
<add key="Exchanger" value="DExchanger.DExchange1, DExchanger.DExchange1"/>
I added a class (to work as a DI container or the abstract factory). The class is DIContainer containing the following method:
public IDataExchange CreateInstance(string config)
{
var type = Type.GetType(config);
return (IDataExchange)Activator.CreateInstance(type);
}
And while I am instantiating, I write the following:
var config = ConfigurationManager.AppSettings.Get("Exchanger");
DIContainer x = new DIContainer();
var instance = x.CreateInstance(config);
Console.Write("Provide your input please: ");
string inp=Console.ReadLine();
Console.WriteLine(instance.DoDataExchange(inp));
Console.ReadLine();
And this is giving me the result I was looking for.
I shall request all of you to put your feedback for further improvement.
Add a property to your
CustomMembershipProviderConfigurationSectionnamedTypeso you can configure it like this in your config fileOn your
CustomMembershipProviderConfigurationSectionyou create a method namedCreateInstancethat returnsIDataExchange. The body of the method is pretty simpleand when you need an
IDataExchangereference you write