David Hayden [MVP C#]

Sponsors

The Lounge

News

  • CodeBetter.Com Home

Other Links

Teas

Patterns & Practices

Florida .NET Developer

Book Reviews

Tampa ASP.NET MVC Developer Group

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
Simple Dependency Injection Application Block Using Application Block Software Factory

I needed to create a simple application block for Enterprise Library using the new Application Block Software Factory for a presentation. Given that I just created an Enterprise Library Plug-In for the RepositoryFactory:

 

Enterprise Library Plug-In

 

I realized I had the makings of a simple Dependency Injection Application Block. Very simple. Actually, more of a glorified Factory Method that has potential :)

As it turns out, it dawned on me that the RepositoryFactory Class in the Repository Factory is not constrained to just creating Repository Classes generated by the software factory, but will essentially create any type with a default constructor. The following:

 

ICustomerRepository repository = RepositoryFactory.Create<ICustomerRepository>();

 

could just as well be:

 

IProductCatalog catalog = RepositoryFactory.Create<IProductCatalog>();

 

In fact in retrospect, perhaps the class could/should have been called ObjectFactory and placed into a separate assembly.

Basically, this is what I was aiming for in the Dependency Injection Application Block. An ObjectFactory that created objects with simple default constructors similar to the RepositoryFactory Class. Again, this is an example for a presentation.

So, that is basically what I did. The GUI Designer is pretty much the same as what I created before except with a few name changes:

 

 

I thought I might be able to use code from the RepositoryFactory Class, but the design was not appropriate for my needs. I wanted more functionality and ended up with a starter API like:

 

ObjectFactory.Initialize();
ObjectFactory.Initialize(IConfigurationSource configurationSource);
ObjectFactory.Create<T>();
ObjectFactory.Add<TInterface,TConcrete>();
ObjectFactory.Remove<T>();
ObjectFactory.Clear();

 

Here is a quick console application I created just for play:

 

 

Here is some code that I was playing with to show the use of the Dependency Injection Application Block:

 

// Get From App.config
ObjectFactory.Initialize();

ObjectFactory.Clear();

// Get From DI.config
ObjectFactory.Initialize(new FileConfigurationSource("../../DI.config"));

// Add ProductCatalog Manually
ObjectFactory.Add<IProductCatalog,ProductCatalog>();

// Get Product
IProductRepository repository = ObjectFactory.Create<IProductRepository>();
Product product = repository.GetProductByProductId(1);

// Get Category
ICategoryRepository repository2 = ObjectFactory.Create<ICategoryRepository>();
Category category = repository2.GetCategoryByCategoryId(3);

// Remove CategoryRepository
ObjectFactory.Remove<ICategoryRepository>();

ObjectFactory.Clear();

 

Pretty basic, but not a bad example that ties in not only the RepositoryFactory but mimics some basic features found in both the Smart Client and Web Client Software Factory.

If you are interested more in the RepositoryFactory Class, you can watch the screencasts:

 


Posted 09-16-2007 1:56 PM by David Hayden

[Advertisement]

Comments

ScottBellware wrote re: Simple Dependency Injection Application Block Using Application Block Software Factory
on 09-17-2007 1:20 AM

If what you're doing isn't dependency injection, but you're presenting from you authority position as dependency injection, are you serving the community appropriately considering that some of your audience will be confused or possibly misdirected?

David Hayden wrote re: Simple Dependency Injection Application Block Using Application Block Software Factory
on 09-17-2007 8:03 AM

I don't know that the group will be confused as the objective is to show off creating an application block.

I was bugged about it, too, however, so I went ahead and added dependency injection features to it last night while watching football.

Jeremy D. Miller wrote re: Simple Dependency Injection Application Block Using Application Block Software Factory
on 09-17-2007 8:14 AM

Just out of curiosity David, do you realize that you're basically recreating PicoContainer from the Java world here?  There's a .Net version of it out there somewhere.

David Hayden wrote re: Simple Dependency Injection Application Block Using Application Block Software Factory
on 09-17-2007 9:05 AM

Jeremy,

I haven't played with PicoContainer. I typically use Windsor and have also used StructureMap.

My goal isn't to add another dependency injection tool into the wild, because there are already good tools out there.

I needed something to show off the application block software factory for a presentation and thought people who use patterns & practices tools could relate to this example because it relates to a number of features in the software factories.

David Hayden [MVP C#] wrote Adding the Dependency Injection into the Dependency Injection Application Block Sample
on 09-17-2007 1:26 PM

So I shamelessly created a Dependency Injection Application Block ProtoType for an Application Block

Christopher Steen wrote Link Listing - September 18, 2007
on 09-19-2007 6:44 AM

Link Listing - September 18, 2007