Dependency Injection with Ninject and Mocking with MoQ

Today it's my great pleasure to lead the discussion about Dependency Injection with Ninject and Mocking with MoQ at the SEVDNUG.

Our purpose as software developers is to orchestrate together loosely coupled, well tested components.  Yet far too often, our code turns into a big ball of mud.  These two tools and their supporting techniques can create loosely coupled, eagerly testable code that will make you much more productive.

Our starting place is this, an n-tier app with hard-coded dependencies.  This is very hard to unit test.  I grant there's good separation of concerns here, and that typically that's harder than it looks.  After a brief discussion of the architecture of this app, we'll look for seams in the code -- places where we can insert interfaces and separate the dependencies.  We'll make the adjustments with the help of Resharper, then get Ninject to wire up the dependencies for us.  With dependencies solved, we'll look at unit testing various pieces (time permitting).  We'll instantiate the code under test, and mock the dependencies using MoQ.  Once we're done, we'll end up with something similar to this, an app with loosly bound dependencies, a clean separation of concerns, and good test coverage.  Even more important, we will understand the purpose and benefits of Dependency Injection, and have a firm, practical grasp of how to accomplish it.  We'll also have a firm understanding of mocking, and practical tools at hand to make unit testing easy.

Where can you go from here?  Let's look at unit testing routes, integration testing, continuous integration, etc.  The sky's the limit.

Rob

posted @ Thursday, April 28, 2011 4:37 PM

Print

Comments on this entry:

# re: Dependency Injection with Ninject and Mocking with MoQ

Left by Jon Davis at 4/28/2011 8:15 PM
Gravatar
Great job, Rob. I look forward to future presentations made by you. I have much to glean from you, both in topic knowledge and in presentation skill.

# re: Dependency Injection with Ninject and Mocking with MoQ

Left by Jonathan at 5/2/2011 10:11 AM
Gravatar
I'm trying to build the final decoupled version and it appears that the Tests.Base/ReflectionHelpers.cs file didn't make it into the zip. Could you add that file and upload a new zip?

Thanks,
Jonathan

ps - great presentation!

# re: Dependency Injection with Ninject and Mocking with MoQ

Left by rob at 5/30/2011 2:02 PM
Gravatar
@Jonathan: you're right, the project reference was accidentally included in the solution. You can just exclude it, and all will work well. I've also updated the download.

# re: Dependency Injection with Ninject and Mocking with MoQ

Left by Josh Lewis at 1/7/2012 1:43 PM
Gravatar
Do you have a slide deck that goes with this post? Your project covers several areas that I'm currently trying to learn which makes it kind of perfect to see them all in one place.

-Josh

Your comment:



 (will not be displayed)


 
 
 
Please add 8 and 1 and type the answer here:
 

Live Comment Preview: