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

Dependency Injection: Simple Example of Instantiating Objects

In my last post I showed how you can use the InjectionMethod attribute to have a method called after the object is instantiated. Of course this will not happen if you simply new your object, but instead you have to use ObjectBuilder to instantiate the object. Now I thought I show you just a simple example of how you instantiate objects with ObjectBuilder.
 
Builder builder = newBuilder();
SimpleClass o = builder.BuildUp<SimpleClass>(null, null, null);
 
Builder is an implementation of IBuilder<TStageEnum> and uses all the default strategies shipped with ObjectBuilder. The strategies that is shipped with ObjectBuilder is TypeMappingStrategy, SingletonStrategy, ConstructorReflectionStrategy, PropertyReflectionStrategy, MethodReflectionStrategy, CreationStrategy, PropertySetterStrategy, MethodExecutionStrategy and BuilderAwareStrategy.
The three arguments to this overload of BuildUp is locator, idToBuild and existing. It is also possible to call the method with policies which is a parameter array as the last argument. The locator is a lifetime container that stores a reference to the objects to control the management and disposal of the objects. I guess I will come back to this in a post on more advanced building. The idToBuild isn't either of so much interest if you haven't provided any locator since it is used to identify an object. This can be used to get an existing object, at least that is what I think, but I'm not sure because if haven't test this so much yet. The existing parameter is used if you want to run an existing object through the strategies pipeline and for example have the method marked with InjectionMethod called. I don't see when you would want to do this, but I haven't been using DI so much, so that could change.
Anyway, the point is that it doesn't have to be difficult to use ObjectBuilder instead of just newing the objects. To finish this up I could show you a short example of the other overload for BuildUp method too. This is very useful if you for example have a configuration file with the name of the class you want to create. Here I have hardcode the name of the class for simplicity.
 
Builder builder = new Builder();
Type t = Type.GetType("ObjectBuilderLab.SimpleClass, ObjectBuilderLab");

SimpleClass o = builder.BuildUp(null, t, null, null) as SimpleClass;
 
Of course the example would be more realistic if the variable o was declared as some type of interface or base class. One I come to think of immediately is that the variable is a Form which should be shown as a result of a user clicking a menu item or similar.
 
 
Published den 16 augusti 2006 08:56 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

 

allan said:

this shorten my research time about dependency inject.  thanks!

november 20, 2007 10:39
 

Arvind Chaodhary said:

OB made easy. Hope I will find a full fledged container created in some of the next posts..THNX
juli 30, 2010 14:34

Leave a Comment

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