Is there a way via xml configuration to denote a static factory method on an object?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Inversion of control/dependency injection and
staticdo not mix well. Instead, do the following. Have anIFooFactoryand a concrete implementationFooFactory:Then, register
FooFactoryas the concrete implementation ofIFooFactorywithContainerControlledLifeTimeManagerso that it acts like a singleton:Then, when you need the factory:
If you can’t alter the implementation of your factory so that it doesn’t have
staticmethods then you will need to create a wrapper:and then register
Of course, you can register
FooFactoryorFooFactoryWrapperas the concrete implementation ofIFooFactoryin XML too. Let me know if you need help with this.The main point is get away from static.
That said, here’s how you can register a static factory in Unity:
I can not figure out how to set this up using XML and after poking around using Reflector I do not think that it is possible. I can not find any classes in
Microsoft.Practices.Unity.StaticFactorythat could handle a configuration element. You probably have to add your own handler.