I have this wcf method
Profile GetProfileInfo(string profileType, string profileName)
and a business rule:
if profileType is "A" read from database.
if profileType is "B" read from xml file.
The question is: how to implement it using a dependency injection container?
Great answer by Mark, However the solution given is not Abstract factory but the implementation of Standard Factory pattern. Please check how Marks classes fit in the Standard Factory Pattern UML diagram. Click here to see above classes applied to Factory pattern UML
Since in Factory pattern, the factory is aware of the concrete classes, we can make the code of the
ProfileRepositoryFactory
much simpler like below. The problem with injecting the different repositories to factory is that you have more code changes every time you add a new concrete type. With below code you only have to update the switch to include new concrete classAbstract Factory is more advanced pattern used for creating families of related or dependent objects without specifying their concrete classes. The UML class diagram available here explains it well.
Let's first assume that you have an IProfileRepository something like this:
as well as two implementations:
DatabaseProfileRepository
andXmlProfileRepository
. The issue is that you would like to pick the correct one based on the value of profileType.You can do this by introducing this Abstract Factory:
Assuming that the IProfileRepositoryFactory has been injected into the service implementation, you can now implement the GetProfileInfo method like this:
A concrete implementation of IProfileRepositoryFactory might look like this:
Now you just need to get your DI Container of choice to wire it all up for you...