Technologist, Geek and a Photographer!
– February 19, 2013
For those who are following me on Codetails, would know that I am actively blogging (since Nov 2012) on Codetails on all technology stuff. So whatever I write there, would be posted on this website after 1 month. One of the series of articles I wrote on Codetails that got great applause from the technical community is on Mocking Frameworks with .NET Here are the links that describe the technical details
And here is my proud moment – it getting featured on Microsoft Channel9
– January 20, 2013
If you are new to dI.Hook, please visit the di.Hook Product Page for Overview, Release Version and Source Code.
Some posts you might want to read would be:
So this is the ninth article in the series which will deal with creating a dependency injection container and using them in your applications
Creating a dependency container is very simple. You can either choose a default implementation of Container, or you can implement the interface IContainer and write your own definition of container. The implementation you would require to implement would be
Assuming that you are not implementing this interface and are using the default implementation provided with dI.Hook, you would involve having
For the sake of understanding, let’s have a eCommerce case study where we have 3 interfaces –
So let’s quickly set the stage with these interface and their implementation
Now, let’s use the container with these business entities
The above code creates a default objects of the BillingProcessor, InternetCustomer and EmailNotifier. If you want to pass your own object you could write as below (see BillingProcessor object)
Now let’s place an order on an eCommerce website using this container. An eCommerce class would be of no use without a reference to ICustomer, IBillingProcessor and INotifier. So eCommerce class for this example would be
So your eCommerce takes objects of ICustomer, IBillingProcessor and INotifier in the constructor and in the Process method, it
Now let’s see how this can be invoked using dI.Hook container that we created
The above code creates an object of ECommerce class. Now what is important here is to see what internally happens. When you call Resolve method, it tries creating an object of ECommerce class. It finds that its constructor requires objects of IBillingProcessor, ICustomer and INotifier. Now since you have already registered objects of these interfaces the dI.Hook Container retrieves the object from the collection and then will try to create an object of ECommerce. If, however, ECommerce constructor required an un-registered object it will try to create a default object and add it in the container.
So if you explicitly created an object of BillingProcessor with payment type as CreditCard and added them in Container, it will use that object; otherwise, it will use default object of BillingProcessor with payment type as Cash
dI.Hook Container ensures that it resolves the hierarchical dependencies automatically. However if it is unable to resolve for any reason, it will throw an exception.
– September 9, 2012