Monday, July 03, 2006

ObjectBuilder


Let's talk about a cool tool named ObjectBuilder that is a subsystem used by CAB and EntLib to perform the repetitive job at the moment of creating objects.

ObjectBuilder (OB) is a Pattern & Practices Application Block. The owners of the project define it as “a framework for composing object-oriented systems”. OB was conceived in CAB (Composite UI Application Block). It uses the Dependency Injection pattern to populate object’s members via Constructors and Property Setters strategies. (To read more about Dependency Injection, see Martin Fowler’s article about what dependency injection is and how it is implemented by various frameworks).

OB can:

The main concepts of OB are Strategies, Policies, Locator, Lifetime Container and of course the Builder. The OB's design is based on the "GoF Builder" design pattern. This pattern is categorized as a stateful creational pattern because we first setup the builder and then tell him (yes, tell don't ask) for a new instance... so, generally an example says than a million word...

1. Builder builder = new Builder();
2. Locator locator = CreateLocator();
3. builder.Policies.Set<ISingletonPolicy>(new SingletonPolicy(true), typeof(Foo), "foo");
4. Foo obj1 = builder.BuildUp<Foo>(locator, "foo", null);
5. Foo obj2 = builder.BuildUp<Foo>(locator, "foo", null);
6. Assert.AreSame(obj1, obj2);

First, in 1 and 2 we create a Builder and a Locator. Then, a Singleton Policy is added to the builder (line 3). The line 4 tells the builder for an instance of Foo and line 5 tells for another instance. Finally, the line 6 asserts that the obj1 and obj2 references are the same so the Singleton Policy works ok.

When BuildUp method is fired, OB lunches a chain of responsibility (yes, another GoF design pattern), using all Strategies that internallyhas, in order that for each strategy uses to decide whether it must execute using the configured policy.

OB has a lot of more power but it is not well documented yet. By now, you can find some bits in CAB's documentation and in meaningful blogs as kzu and brian button

Labels:


Comments:
What code is within the CreateLocator() method? Using a standard Locator doesn't seem to work.
 
Sorry for the delay... the CreateLocator() method is the same as the one that exists in the OB Tests. I leave you the code...

private Locator CreateLocator()
{
Locator locator = new Locator();
LifetimeContainer lifetime = new LifetimeContainer();
locator.Add(typeof(ILifetimeContainer), lifetime);
return locator;
}
 
خدمات وجهات
اماكن سياحية تبوك
افضل اسعار فنادق تبوك

 
Post a Comment



<< Home

This page is powered by Blogger. Isn't yours?