Reputation: 83
Existing Implementation:
I have implemented factory pattern for DB connection. I am having three DB like sql,oracle and sybase. All three classes like sqlConnector,OracleConnector and SybaseConnector has implemented in Iconnect interface and in the factory method i have created the object of these concrete classes(sqlConnector,OracleConnector and SybaseConnector) based on switch case and returned specific object.
Scenario:
I want to modify this factory pattern and want to remove all switch case from factory method and want to configure through config file/xml and also without using reflection and spring.net. Suppose tommorow i introduce new db type , i don't want to change the code.I will make entry in the config/xml ,it should returned the object to client without changing factory.
Question:
How i can implement configurable factory pattern means suppose tomorrow introducing new db type like "MySql", i don't want to change the code of factory.
Upvotes: 1
Views: 1336
Reputation: 4320
This is a very broad question, but I think that the technology that will best serve you is Managed Extensibility Framework, or MEF.
MEF is an integral part of .NET that allows you to include additional components into your solution without having to know what those components are beforehand. MEF automatically discovers and includes components into your solution after your solution is built.
You define the points where your solution can be extended; each component is written to take advantage of those extensibility points; MEF ties them together automatically.
If you have used an Inversion of Control framework, like a Dependency Injection library (Ninject, Castle Windsor, SimpleInjector, etc.), then you have already done something a little like this, and the concepts should be somewhat familiar to you.
Explaining it and showing you how to use it will take a book. However, I'm reasonably certain that it will solve your problem best. There are books, blogs, videos, and many, many examples.
Upvotes: 2