Skip to end of metadata
Go to start of metadata

This example uses the MovieFinder and MovieLister example Martin Fowler described in his article Inversion of Control Containers and the Dependency Injection pattern. We will use the code that has already been written to take advantage of Dependency Injection. The code below demonstrates how to define classes to be registered with the Container through NanoContainer.NET custom Attributes.

MovieLister simply registers itself to the container without a key. Therefor it'll be registered under it's concrete class type.

MovieLister.cs

ColonMovieFinder registers itself to the container under the key typeof(IMovieFinder). Now this isn't a requirment. PicoContainer.NET is smart enough to realize that ColonMovieFinder is an implementation of IMovieFinder. The ConstantParameter Attribute is also used on this class. This states that when creating an instance of ColonMovieFinder set the first parameter (0) passed to the constructor the value "movies1.txt". In other words when the container initializes ColonMovieFinder the filename is set to "movies1.txt"

ColonMovieFinder.cs

Okay so now lets set up the code so that these classes are registered to the container. The code below will follow the path to "MyNanoAssembly.dll" and load the dll. Those classes tagged with RegisterWithContainer attribute will be added to the container.

BootStrap.cs

So as you can see with only a few lines of code and using some meta data you can completely define and construct a ccontainer.

  • No labels