Recently I got into a healthy discussion with Keith Patton about Constructor Injection and other such topics to do with Test Driven and Domain Driven Design.
One way or another this lead to the birth of Kanda Alpha with the goal of demonstrating best practice domain driven design utilising Visual Studio 2010 Beta, ASP.NET MVC 1.0 and Db4o as the data store.
I’ve decided to give an explanation as to the benefits of Mocking and the Test First approach.
Mocking quite simply allows you to test components in isolation from their dependencies. By mocking out the results and behaviour of the dependent components you can focus on verifying the behaviour of a single class.
Where I’ve found it most useful is in designing the service layer and this is usually the first thing I design after hashing out the Domain Entities.
There has also been some comments on Keith’s blog about the purpose of the Service layer if all it is doing is acting as a Facade to the Repository. This is a valid point and I’m going to try and give a real life example which demonstrates that this isn’t the case.
From Martin Fowler’s Domain Driven Design
“A Service Layer defines an application's boundary [Cockburn PloP] and its set of available operations from the perspective of interfacing client layers. It encapsulates the application's business logic, controlling transactions and coordinating responses in the implementation of its operations.”
http://martinfowler.com/eaaCatalog/serviceLayer.html
Second only to the Entities Model the Service Layer is the heart of your application so should be the focus of the design and that’s where Mocking will add value.
The example I’m going to demonstrate is the very simple but very common Registration of a User.
When a User is created there are a number of requirements that need to be met:
- Geocode the Users Address
- Persist the User to a data store
- Send an Email asking them to confirm their email
Right so let’s see some code.
Firstly I will define the IRegistration interface
Ok as we are doing Test first let’s create an Implementation and set up our first Unit Test.
Now the Mocking framework of choice is Rhino.Mocks.
So here is the very first Unit Test.
Note this test will fail as RegisterUser has not been implemented yet, this is fine as it is failing for a very valid reason.
Because of the requirements listed above the IRegistrationService implementation is going to have dependencies on some other components to fulfil these requirements.
- IUserRepository
- IAddressGeocoder
- IMessageSender
So let’s define those
Now we will define the IUserRepository which will be used to persist the User.
Finally we need a component to send the confirmation email.
Ok now that we have defined the Interfaces for our dependencies, let’s add them to our RegistrationService implementation. For this I am going to use Constructor Injection (link here) it will become clear shortly why.
Now that we have obviously broken our test because there is no longer a default constructor. So let’s fix up our test so that it runs again.
You will notice above that I have used the MockRepository to generate in-memory Stubs. We do this because we don’t want to worry about the implementations at this stage.
Now we need to Setup some results.
Now that we have setup our test we can write the implementation, which should be easy.
Now running the test should give us the all important green tick.
Well that’s it. By following this approach you are forced to think about how classes interact with other classes and as a result you end up with loosely coupled components that are Unit Testable.
The source code is available to on the KandaAlpha project on Codeplex.