To register or not to register that is the question.

Topics: Prism v2 - WPF 3.5
Nov 16, 2009 at 10:10 PM

We ran across an interesting scenario this afternoon that I thought I should probably ask about. All of our PresentationModels are registered with the Unity Container via each modules Module.cs. like so.

mContainer.RegisterType<IMyType, MyTYpe>();

We use the container as a service locator when working within a single module like so.

IMyType myType = mContainer.Resolve<IMyTYpe>();

A colleague of my mentioned to me today that he could remove the registration entry from the module file completely and still access the type in the container like so.

MyType myType = mContainer<MyType>();

I was a little taken back by this as I did not think it was possible. Could someone please explain the difference between registering a item and the resolving it as opposed to simple resolving it?

Thanks,

Brette

Nov 16, 2009 at 10:43 PM

I'm no expert but here is my take...

If you *always* only ever use the MyType implementation of IMyType, then there is no difference (i.e. I believe you will get pretty much the same impementaion using either method). However, the non-register method couples you to that concrete implementation. This may not be a problem depending how and where you are using IMyType/MyType.

Generally speaking decoupled is a better pattern, leading to less rigid systems. You could, potentially, create a new MyType2 that you use in certain circumstances (customer specific maybe?) so you would register it for those circumstances and MyType otherwise.

One place that decoupling can help is when Unit Testing - an interface is generally easier to Fake.

So my gut feeling is that you are probably right to use the method you are currently using, but that only you/your colleagues can decide if the extra work involved creating and using the interfaces is worth that potential benefit of decoupling.

I'm sure someone else can create a more compelling argument, but that's the best I can do :)

Cheers

Mark

Nov 16, 2009 at 11:15 PM

Yeah I agree philosophically, however I am just trying to understand if there is a technical difference.

Thanks for the insight,

Brette