Configuring IUnityContainer

I’m using the IUni­ty­Con­tainer from the Com­pos­ite Appli­ca­tion Guid­ance in my cur­rent Sil­verlight project for Depen­dency Injec­tion and this morn­ing, I came across some­thing that I couldn’t find doc­u­mented any­where. Ini­tially, all my reg­is­tered depen­den­cies were being cre­ated using other depen­den­cies that I had reg­is­tered with the con­tainer. For exam­ple, I had the Cus­tomerOrder­Ser­vice below and it was cre­ated using an IEven­tAg­gre­ga­tor that was already reg­is­tered in the container.

    public class CustomerOrderService : ICustomerOrderService
    {
        public CustomerOrderService(IEventAggregator evAg)
        {
            this.eventAggregator = evAg;
        }
    }

I was just reg­is­ter­ing my ser­vice with the con­tainer and let­ting it do all the work.

this.container.RegisterType();

How­ever, this morn­ing, I wanted to add a sim­ple string to my con­struc­tor so that I could pass in the URI for a web ser­vice instead of hard cod­ing it in the ser­vice class. After dig­ging around a lit­tle in the docs and even­tu­ally just play­ing with the code until it worked, I came up with two ways of doing this.

First, I mod­i­fied my ser­vice to have two con­struc­tor arguments:

    public class CustomerOrderService : ICustomerOrderService
    {
        public CustomerOrderService(IEventAggregator evAg, string uri)
        {
            this.eventAggregator = evAg;
            this.baseUri = uri;
        }
    }

Then, I came up with the two ways to reg­is­ter this ser­vice in code with the con­tainer. First, you can tell the con­tainer how to con­fig­ure your depen­dency registration.

this.container.Configure().ConfigureInjectionFor(
    new InjectionConstructor(typeof(IEventAggregator), "URI GOES HERE"));

When the con­tainer is asked to cre­ate a Cus­tomerOrder­Ser­vice object, it is con­fig­ured to send in an IEven­tAg­gre­ga­tor instance that it resolves inter­nally and the URI string above. Of course, in a real world app, that URI would actu­ally be a URI that I get from configuration.

Alter­na­tively, you can tell the con­tainer what to do when you reg­is­ter the type:

this.container.RegisterType(
    new InjectionConstructor(typeof(IEventAggregator), "URI GOES HERE"));

This is a lit­tle cleaner because you’re going to have to reg­is­ter the type any­way and it makes sense to con­fig­ure it at the same time.

By allow­ing the devel­oper to spec­ify an Injec­tion­Con­struc­tor to use, the Uni­ty­Con­tainer gives the devel­oper a much more flex­i­ble way to reg­is­ter his dependencies.

PS: The Google Syn­tax high­lighter and my new Word­Press theme don’t appar­ently play well together. Or at all. So the code is dis­play­ing in a ridicu­lous way. If you need an actual exam­ple, feel free to drop me a line in the com­ments and I’ll send you one. It may be time to sep­a­rate my tech blog­ging out from The Experiment.

No Comments

Leave a Reply

Your email is never shared.Required fields are marked *