Eric's Blog

Day to day experience in .NET
Welcome to Blogs @ IRM Sign in | Join | Help
 Search

Disclaimer

The content of this site is my own personal opinion and does not in any way represent my employer, it's subsideries or affiliates. These postings are provided "AS IS" with no warranties, and confer no rights.

This Blog

A Series of Posts about Dependency Injection

I’ve started to dig a little deeper into PAG:s Composite Application Block (CAB) and one place to start is to check out ObjectBuilder (which is also used by EntLib). ObjectBuilder is an implementation of Dependency Injection, which is a specific name for the pattern “Inversion of Control” (IoC). You can read more about it on Martin Fowlers site.
 
ObjectBuilder is quite complex, and I have probably only scratched the surface so far, but I will try to blog a little about what I learn along the way. In this first post, I thought that I would start with a simple example of what you can do, when you write classes that are instantiated with the help of ObjectBuilder. I will start by covering the InjectionMethod and InjectionConstructor attributes.
 
Public Class Form1

    <Microsoft.Practices.ObjectBuilder.InjectionMethod()> _
    Public Sub
AfterInitializeComponent()
        
'Do something useful ...
    End
Sub

End Class
 
When I write forms I often find myself adding an AfterInitializeComponent-method to do some additional initialization, you guessed it, after the InitializeComponent has run. Instead of adding the default constructor to my class, and from there calling my method I can instead attribute the method with InjectionMethod, which will make it called by the ObjectBuilder after the object is instantiated. Note that, for ObjectBuilder to call the method it has to be public, though.
Now, you might protest that, it’s not that much of work to add a single call to a method yourself, and of course it’s not. But this method is run after some other things have happened with your class when using dependency injection (for example after properties marked with CreateNew-attribute), and that can be very important.
 
The attribute InjectionConstructor is used to mark which constructor should be used by ObjectBuilder, when instantiating the class. This should be used when the class has more than one constructor, and is only allowed on one of them.
 
Behind the scenes ObjectBuilder uses a pipeline of strategies, where each strategy can perform operations on the object when it is instantiated. Some of the built-in strategies uses attributes to control their behavior, as shown above. For example is the ConstructorReflectionStrategy using the InjectionConstructor attribute when it determines which constructor should be used for object instantiation.
 
Published den 14 augusti 2006 09:03 by ericqu

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

Erics Blog said:

Overview of how policies affects object creation with ObjectBuilder (an example of ConstructorPolicy).

oktober 31, 2006 09:14
 

Erics Blog said:

In my last post I showed how you can use the InjectionMethod attribute to have a method called after

oktober 31, 2006 09:17

Leave a Comment

(required) 
(optional)
(required) 
Submit
Powered by Community Server, by Telligent Systems