Is there a way via xml configuration to denote a static factory method on an object?
相关问题
- Generic Generics in Managed C++
- How to Debug/Register a Permanent WMI Event Which
- 'System.Threading.ThreadAbortException' in
- Bulk update SQL Server C#
- Should I use static function in c# where many call
Inversion of control/dependency injection and
static
do not mix well. Instead, do the following. Have anIFooFactory
and a concrete implementationFooFactory
:Then, register
FooFactory
as the concrete implementation ofIFooFactory
withContainerControlledLifeTimeManager
so 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
static
methods then you will need to create a wrapper:and then register
Of course, you can register
FooFactory
orFooFactoryWrapper
as the concrete implementation ofIFooFactory
in 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.StaticFactory
that could handle a configuration element. You probably have to add your own handler.