TypeMapping and Singleton

Topics: ObjectBuilder
Jun 5, 2007 at 7:12 PM
I am trying to create a factory for creating concrete instances given an interface. To simplify, this is the scenario.

IntAssembly contains IPerson interface.
ImplAssembly contains Person interface. Person implements IPerson.

Doesn't returns singletons:-
<build-rule name="Person" type="IPerson,IntAssembly" mode="Singleton">
<mapped-type type="Person, ImplAssembly"/>
</build-rule>

Does return singletons:-
<build-rule name="Person" type="Person,ImplAssembly" mode="Singleton">
</build-rule>

Seems like TypeMappingPolicy and Singletons are at odds. Appreciate any help.

Sree

Coordinator
Jun 6, 2007 at 2:43 AM
It's all about the order of the strategies.

If the type mapping strategy comes before the singleton strategy, then it appears that the typeToBuild is Person, not IPerson. It looks for the singleton rule for Person, not IPerson.

You can make arguments on both sides whether the singleton rule should be applied to the pre-mapped type or the post-mapped type. Should I be able to say that all implementations of IPerson are singletons? Should I be able to say that only specific concrete IPerson implementations are singletons? Both? Clearly, there's no one right answer for everybody.

If you can pick one or the other, then you can move around the order of the two strategies appropriately. Extending the context to include the original type and name being built (as well as fixing the singleton strategy to check both) would allow both to work.
Coordinator
Jun 7, 2007 at 10:39 PM
We just checked in the CodePlex Container that has this fix in it.
Coordinator
Jun 8, 2007 at 2:26 AM
Well, to be clear, we didn't fix the singleton strategy bug. What we checked in today was the enabler (putting the original type and ID into the context) for someone who wanted to fix this bug. :)
Jun 8, 2007 at 3:25 PM

Thank you for the quick response. In our case, we want all the implementation of IPerson to be Singletons. We modified the configuration file to add another buildRule for the concrete type.

<build-rule name="Person" type="IPerson,IntAssembly" mode="Singleton">
<mapped-type type="Person, ImplAssembly"/>
</build-rule>
<build-rule type="Person, ImplAssembly" mode="Singleton" />


Sree