Splitting unit of work and repository functionality
For years we (a lot of people I know and myself included) have been using the Unit of Work and Repository pattern combined with each other. This makes quite a lot of sense as, in most cases, they both have something to do with your database calls.
When searching for both of these patterns you’ll often be directed to a popular article on the Microsoft documentation site. The sample code over there has a very detailed implementation on how you can implement both of these patterns for accessing and working with your database. I kind of like this post as it goes in great length to describe both the unit of work- and repository pattern and the advantages of using them. I see a lot of projects/companies having implemented the pattern combo like described in the Microsoft article. I can’t really blame them as it’s one of the top hits when you search for it in any search engine.
There is a downside to this sample though. It violates the Open/Closed principle which states
software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification
Whenever you need to add a new repository to your database context, you also need to add this repository to your unit of work, therefore violating the open/closed principle.
It also violates the Single Responsibility Principle, which states “every module or class should have responsibility over a single part of the functionality provided by the software, and that responsibility should be entirely encapsulated by the class. All its services should be narrowly aligned with that responsibility.” or in short “A class should have only one reason to change.”. The reason why the sample implementation violates this principle is because it is handling multiple responsibilities. The unit of work’s purpose should be to encapsulate and commit or rollback transactions of atomic operations. However, it’s also creating and managing the several repository objects, therefore having multiple responsibilities.
Implementing the unit of work and repository pattern can be done in multiple ways. Derek Greer goes on about this at great length about this in an old post of him. As always there are several ways to improve the design. You might even want to keep the mentioned design in the Microsoft example, because ‘it-just-works’. For the sake of cleaner code I’ll describe one of the ways, which I personally like very much, to improve the software design. By adding a decorator to the project the functional code will be much cleaner.
First thing you have to consider is implementing some form of CQRS in your software design. This will make your live much easier when splitting the command, unit of work and repository functionality. You can perfectly implement the described solution without implementing CQRS, but why would you want to do this?
I’ll just assume you have a command handler in your application. The interface will probably look similar to the following piece of code.
public interface IIncomingFileHandler<in TCommand>
where TCommand : IncomingFileCommand
{
void Handle(TCommand command);
}
The actual command handler can be implemented like the following piece of code.
public class IncomingFileHandler<TCommand> : IIncomingFileHandler<TCommand>
where TCommand : IncomingFileCommand
{
private readonly IRepository<Customer> customerRepository;
private readonly IRepository<File> fileRepository;
protected IncomingFileHandler(IRepository<Customer> customerRepository, IRepository<File> fileRepository)
{
this.customerRepository = customerRepository;
this.fileRepository = fileRepository;
}
public void Handle(TCommand command)
{
//Implement your logic over here.
var customer = customerRepository.Get(command.CustomerId);
customer.LatestUpdate = command.Request;
customerRepository.Update(customer);
var file = CreateNewIncomingFileDto(command);
fileRepository.Add(file);
return;
}
}
All of the necessary repositories are injected over here so we can implement the logic for this functional area. The implementation doesn’t make much sense, but keep in mind it’s just an example. This piece of code wants to write to the database multiple times. We could implement the call to the SaveChanges()
method inside the Update
- and Add
-methods, but that’s a waste of database requests and you’ll sacrifice transactional consistency.
At this time nothing is actually written back to the database, because the SaveChanges
isn’t called anywhere and we aren’t committing (or rolling back) any transaction either. The functionality for persisting the data will be implemented in a transaction handler, which will be added as a decorator. The transaction handler will create a new TransactionScope
, invoke the Handle
-method of the actual IIncomingFileHandler<TCommand>
implementation (in our case the IncomingFileHandler<TCommand>
), save the changes and commit the transaction (or roll back).
A simple version of this transaction decorator is shown in the following code block.
public class IncomingFileHandlerTransactionDecorator<TCommand> : IIncomingFileHandler<TCommand>
where TCommand : IncomingFileCommand
{
private readonly IIncomingFileHandler<TCommand> decorated;
private readonly IDbContext context;
public IncomingFileHandlerTransactionDecorator(IIncomingFileHandler<TCommand> decorated, IDbContext context)
{
this.decorated = decorated;
this.context = context;
}
public void Handle(TCommand command)
{
using (var transaction = context.BeginTransaction())
{
try
{
decorated.Handle(command)
context.SaveChanges();
context.Commit(transaction);
}
catch (Exception ex)
{
context.Rollback(transaction);
throw;
}
}
}
}
This piece of code is only responsible for creating a transaction and persisting the changes made into the database.
We are still using the repository pattern and making use of the unit-of-work, but each piece of code now has its own responsibility. Therefore making the code much cleaner. You also aren’t violating the open/closed principle as you can still add dozens of repositories, without affecting anything else in your codebase.
The setup for this separation is a bit more complex compared to just hacking everything together in one big file/class. Luckily Autofac has some awesome built-in functionality to add decorators. The following two lines are all you need to make the magic happen.
builder.RegisterGeneric(typeof(IncomingFileHandler<>)).Named("commandHandler", typeof(IIncomingFileHandler<>));
builder.RegisterGenericDecorator(typeof(IncomingFileHandlerTransactionDecorator<>), typeof(IIncomingFileHandler<>), fromKey: "commandHandler");
This tells Autofac to use the IncomingFileHandlerTransactionDecorator
as a decorator for the IncomingFileHandler
.
After having implemented the setup you are good to go. So, whenever you think of implementing the unit-of-work and repository pattern in your project, keep in mind the suggestions in this post.