views:

47

answers:

2

Hi,

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

A: 
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.

Mark Seemann
Thanks, i'll have a play not.What I'm hoping to end up with is an application that will read dll's, instantiate classes in those dll's that implement the specific interface and execute the code.The dll's will live in a directory, so I can just drop in a dll for some extra functionality without changing the application.Hopefully im going in the right direction!!Never looked at MEF, wil take a look...thx
SteveCl
MEF will do that for you. Unity will not.
Mark Seemann
Do you have any examples (like above few lines)?I've got the code working so I am registering multiple classes that implement the same interface and able to resolve all and execute the code as I want to. Now its just opening the dll's!! Maybe I should start another question for that 1!
SteveCl
Here's a good introductory article about MEF: http://msdn.microsoft.com/en-us/magazine/ee291628.aspx
Mark Seemann
A: 

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

Simon Hart