Because I’m relative new to OOP / C# i don’t know the right pattern to solve this:
I have to build a plugin architecture for different IO Providers. The host reads the needed Provider name / type from the config, then it should the provider instantiate and parameterize.
So I have basically this interfaces:
public interface IoProvider //(Base interface, all Providers implements this)
{
void Initialize();
void Execute();
}
public interface IFileProvider: IoProvider
{
string PropertyA { get; set; }
}
public interface ISmtpProvider : IoProvider
{
string PropertyB { get; set; }
string PropertyC { get; set; }
}
As you see the derived, specialized IO Providers have different additional parameter properties which the base interface doesn’t have. To avoid if/then/else or switch statements my idea was to use a factory pattern.
But if i understand this correctly it doesn’t solve my if/then/else problem because on the client I have to check the derived type to supply the correct parameters.
So the program flow on the Host would be something like this: Host reads config, gets Name/Type of needed Provider Host calls Factory and gets the Provider
But how avoid this - is there a pattern to solve this without if/then/else?
If (provider == typeOf(IFileProvider))
PropertyA = value
else if (provider == typeOf(ISmtpProvider))
PropertyB = value
PropertyC = value
Elseif …
You can replace switch
statement with polymorphism. Just allow each provider to configure itself from config. This is a best option, because each provider knows which values to look for:
provider.Configure();
This method should exist in base interface:
public interface IoProvider
{
void Initialize();
void Execute();
void Configure();
}
And each provider implement it:
public interface ISmtpProvider : IoProvider
{
string PropertyB { get; set; }
string PropertyC { get; set; }
public void Configure()
{
PropertyB = ConfigurationManager.AppSettins["B"];
PropertyB = ConfigurationManager.AppSettins["C"];
}
}
Main benefit of this approach is that you will have only one place to change, when new provider added to your application - just add new provider class, which knows how to configure itself. You don't need to change host implementation also. And your host will satisfy OCP principle - open for extension (you can add new providers), but closed for modification (you don't need to modify existing code when new provider added).
Also you can pass some configuration object to Configure(IConfiguration config)
to this method (it will make your code testable, and not dependent on static ConfigurationManager
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With