Unity dependency injection: Where to put Interfaces?

Topics: General discussion
Nov 18, 2009 at 8:04 AM

I have a very simple and common scenario:  WCF Service "MyService : IMyService" calling methods on objects from a different class library "MyLibrary.MyWorkerClass.DoWork()"

I'd like to use Unity in the service method to create the concrete instance of the MyWorkerClass.

To help with unit testing I'd like to do this through interfaces, so "MyService.PerformWork()" creates an instance of "IMyWorkerClass", and uses Unity to determine what the concrete implementation is.

My question is: Where do I declare the interface "IMyWorkerClass"?

If I delcare it in the Library, then I need the service to have a reference to the library.  If I declare it in the Service project, then vice versa.  If I want to be able to replace the library with a different one, then in the interface needs to be declared in the Service project?

Which is the sensible/correct one?

Thanks :)

Nov 18, 2009 at 8:44 AM


I would suggest that you declare the interface definition in a separate class library which eventually will be referenced by the Service and the Library with the concrete implementation.

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.