July 2008
M T W T F S S
« Jun   Aug »
 123456
78910111213
14151617181920
21222324252627
28293031  
Chris Donnan

Create Your Badge

Chris Donnan : Programming – Brooklyn Style

software, trading, family, fun

The simplest possible DI container (part 3)

In this segment, we will make our stuff dispose nicely.

1st – let us look @ our creation policies in a bit more detail:

Here is the simplest possible one – a prototype creation policy. It will simply call the user’s func each time it is called, passing in the calling container for possible child dependency resolution :

The singleton creation policy is a bit more:

This policy uses the user supplied func to create a value 1x, then returns the value for all subsequent calls.

Now – why is this relevant? Well in order to dispose properly, we need to make our container disposable and implement that so – in normal form – here is a test and a little class to help test:


All we want to do is to dispose our container and ensure that any disposable items are disposed – for creation policies that want it as such.

So – we will add a dispose method to our container:

Our container has a field that is a dictionary of string to Definition. The list of values is a list of definition. If our definition has a disposable creation policy, we dispose it. This is using LINQ to select the values in an orderly fasion and a functional-esque call to dispose each in the ToList().ForEach(lambda) call.

We updated our singleton creation policy to look like this:

So – now our singleton creation policy will dispose singletons when the container disposes. If a caller supplies any disposable custom creation policy – it will also dispose as per their mechanism.

Now – I need to make sure that I throw exceptions ‘at the door’ when users provide bad data, or use the container incorrectly. After that, I need to makeĀ a pass at thread safety for the container…


Responses are currently closed, but you can trackback from your own site.