SyntaxHighlighter

Wednesday, March 5, 2008

Which (IoC) Should I Use?

Today it became clear. I'm writing a Windows Forms Application using MVP(supervising controller) "Test First"... and the presenter is taking on the responsibility of calling in to other domains such as Services Layer, Persistence Layer, etc. The presenter is communicating entirely through interfaces which makes it nice and generic... I'm abstracting away the implementation details. So there's about six different contracts(Interfaces) that the presenter needs to get access to. Currently I'm using poor mans dependency injection via Constructor Injection... this is the act of constructor chaining where the overloaded constructor takes on the responsibility of wiring up the concrete types to the abstractions. While this works... it's kind of messy and code is beginning to smell. So I need to implement a Inversion Of Control Container... their are so many to choose from... with choices like... Spring.NET, Structure Map,Object Builder, and Unity... it's very difficult to make the choice of which to use. Since I've learned about Object Builder at the recent code camp... it's kind of fresh in mind, however Unity builds on top of Object Builder, and it has a seamless configuration option which allows a nice clean and easy to read mapping of types to Interfaces. Once the setup is complete... I can just use the container to call the abstraction methods.


Sample of poor mans dependency injection
-----------------------------------------------------

public class Presenter
{
IService m_service;
Presenter()this() //Constructor Chaining
{
//Default Implementation
}
Presenter()
{
m_service = new Service(); //Constructor Injection
}
}

VS. Unity Sample Below
------------------------------------------------

IUnityContainer container = new UnityContainer();

container

.Register<IService, Service>();

IService service = container.Get<IService>();



Unity is a much cleaner approach... unfortunately, not many people would be able to maintain my code if I were to go on vacation with out some major documentation... so I need to do the best I can with making this approach understandable to other developers on the team.



Until Next Time.....



-Develop with Passion
Jean Paul S. Boodhoo

No comments:

Post a Comment