Adding Creation Strategies to Dependency Injection Application Block - More Chain of Responsibility
by David Hayden ( Florida .NET Developer )
I added some additional functionality today with respect to creation strategies in the sample Dependency Injection Application Block. These creation strategies are also set-up using the Chain of Responsibility Pattern as I discussed in Chain of Responsibility Pattern - Builder Pattern - Fluent Interfaces.
Originally I assumed a TypeMapping Strategy, where you map a concrete type to another type and retrieve a new concrete type as such:
// Register ProductCatalog as IProductCatalog
ObjectFactory.Add<IProductCatalog,ProductCatalog>();
// Retrieve ProductCatalog via the Interface
IProductCatalog catalog =
ObjectFactory.Create<IProductCatalog>();
More than not, that is how I use Dependency Injection Tools.
However, what about singletons? Maybe I want to create the object only once and then use that same object each time. Or, maybe I just want the tool to create the object I give it:
// Create ProductCatalog
ProductCatalog catalog =
ObjectFactory.Create<ProductCatalog>();
Once again as mentioned in the previous post on the Chain of Responsibility Pattern, I went ahead and added pluggable strategies to deal with 1) Singletons, 2) Type Mappings, and 3) Default Construction:

The diagram may be a bit misleading. At anytime one of the strategies can indeed provide the object it will and not call the other strategies downstream from it. If you have specified in registration that you want a type to be registered as a singleton and it has been created before, the SingletonCreationStrategy will grab the “old“ object from its container and just provide it. TypeMappingCreationStrategy and DefaultCreationStrategy will not be called. Etc. etc.
Although each strategy is optional, the SingletonCreationStrategy has to be first in the line if you include it to insure that it looks in its container for an old object before the other strategies attempt to create a new one. Registering a singleton can be done in configuration or:
ObjectFactory.Add<IProductCatalog,
ProductCatalog>(Lifestyle.Singleton);
The sample is getting a bit more useful now, but I still prefer Windsor :)
Feed: David Hayden ( Florida .NET Developer )