views:

80

answers:

2

What are the best practices around providing a smooth debug experience in Visual Studio 2008 when using dependency injection (DI)?

Specifically, suppose I have a solution with 3 projects:

MySolution: - ConsoleApp - ServiceInterface - ConcreteService

ConsoleApp has a reference to the ServiceInterface project, and uses a DI container to resolve the concrete type (from ConcreteService project).

When I start debugging, ConsoleApp will not be able to load the concrete type unless the ConcreteService.dll is present in the ConsoleApp bin\debug directory.

So the question is, how do I get the ConcreteService.dll into ConsoleApp's bin\debug folder?

Some options I've thought of are a post-build script, adding a project reference in ConsoleApp to ConcreteService and copying the dll by hand.

There are some pros/cons to all of these approaches, so I'm curious to see if there are other approaches and if not, which approach people prefer.

+3  A: 

Just add a reference to the concrete implementation.

Incidentally, it's perfectly OK architecturally to have the interface and implementation in the same assembly. You may have a good reason for not doing so, but people seem to shy away from this instinctively but for no good reason.

James L
I often do put the default implementation in the same assembly as the interface. As you mentioned that makes things easier. In this case, I wanted to provide a different implementation then the default.I have two quibbles with referencing the implementation:1) Tempts people to instantiate it directly rather then through the container2) Once I reference the implementation, I have to reference its dependencies and so on. Something about that feels less then clean.
Peter M
@PeterM: "Once I reference the implementation, I have to reference its dependencies" -> not necessarily true unless you actually *use* the implementation type (or was it the namespace? I never remember)
Mauricio Scheffer
+2  A: 

You could change the output directory of each project to the same directory. I've used MySolution\Debug for debugging DI projects to avoid adding references.

Addressing the first comment

What if you have multiple applications that use DI to get at the same dependencies? For example if I have a common data access layer used by multiple applications?

Odds are it's ok to dump everything together in the same debug folder for a solution. You'll only be debugging one application at a time and so long as you test your Release build, you should be fine.

You could also create different build configurations for the various applications in your solution and set the output folders to MySolution\ConfigurationXDebug (or something like it) for each configuration. This has the added benefit where you can explicitly choose to build the appropriate projects but is more work to setup.

Austin Salonen
What if you have multiple applications that use DI to get at the same dependencies? For example if I have a common data access layer used by multiple applications?
Peter M
Interesting idea to dump everything into the same debug folder. I actually have multiple solutions that use the same binaries, but that would still work with this setup.In order to follow the guidance from stackoverflow's FAQ about open ended questions, I'll mark this as the answer, but I'd love to see any other ideas.
Peter M
I ended up taking a combination of these approaches. See: http://blogs.claritycon.com/blogs/peter_miller/archive/2010/01/14/making-visual-studio-and-dependency-injection-play-nice.aspx
Peter M