On All Things Web

Aug 12

The other supported type of injection by the WebFormsMvpExtensions framework is model injection using a DI container, like Unity or Castle.  Setting up this type of container is easy.  In the appropriate WebFormsMvp third party project, an Injection namespace was added with an appropriate model injector.  This setup is a little bit different than the previous model injection example, so bear with me.  Using the Unity framework, we begin by making a call to ModelInjectors, and setting the Current property with the UnityModelInjector class.  This model injector requires the UnityContainer with all of the mappings defined at the time of construction.  It would look something like the following:

var container = new UnityContainer();
container.RegisterInstance<IKeysContract>(new KeysContract());
container.RegisterInstance<IValuesContract>(new ValuesContract());
ModelInjectors.Current = new UnityModelInjector(container);

Once this is established, to use the unity container, we need to craft our model injection this way:

public class ContractAttributedTestModel



public IKeysContract Keys { get; set; }



public IValuesContract Values { get; set; }


Notice LookupType isn’t specified; the ModelInjection definition uses the Unity container to lookup the property return types (IKeysContract and IValuesContract) and pushes in the values in the DI container.  It strictly reads the property type to perform the injection, so this is important.  Optionally, a Key attribute can also be specified, which can be passed to the DI container, in case multiple registrations have occurred.

Update on the other DI containers, the Castle DI container has been written, but not tested, and any other supported containers are not yet written, but will occur soon.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>