Dependency injection with Unity Application Block

708 views Asked by At

I am trying to learn about dependency injection and i'm using the unity application block to help.

What I want to do is, have a console app that will register a class (as long as it implements a specific interface) and execute a method... So the method on the class that implements the method will be executed.

Hope that makes sense...a nice nudge in the right direction would be perfect!

I'm looking at the docs on msdn, but i'm still not 100% sure of how to go about it.

Thx Steve

2

There are 2 answers

4
Mark Seemann On BEST ANSWER
var container = new UnityContainer();
container.RegisterType<IFoo, Foo>();

container.Resolve<IFoo>().Bar();

When Resolve is called, it will return an instance of Foo since that was what was registered for the IFoo interface.

Unity does not have convention-based registration features like more advance DI Containers. If you want late-bound composition, you may want to take a look at MEF instead.

0
Simon Hart On

I've never heard of the MEF but all you need to do is implement a simple plugin pattern. I wrote an article a while back on how to do this for a database engine but it can easilly be applied to anything that implements an interface:

http://www.simonrhart.com/2009/04/example-of-plugin-pattern-on-compact.html