Question

I read the following article .NET Junkie - Meanwhile... on the command side of my architecture which was suggested by another stackoverflow user that outlines the command pattern and provides a strategy for how to use it with DI at the end of the article.

This has helped immensely but the one thing I'm missing, let's say I create a new class called CheckoutCustomerCommandHandler.

Now, let's say I need to inject this command and the MoveCustomerCommandHandler into a controller for whatever reason via the constructor. How does this affect the DI container setup and the constructor?

At the core, they both implement the same interface. It seems like this would result in a lookup issue for the DI container. In the article example, here is their sample injector setup:

public interface ICommandHandler<TCommand>
{
    void Handle(TCommand command);
}

// Exactly the same as before, but now with the interface.
public class MoveCustomerCommandHandler
: ICommandHandler<MoveCustomerCommand>
{
    private readonly UnitOfWork db;

    public MoveCustomerCommandHandler(UnitOfWork db,
    [Other dependencies here])
    {
        this.db = db;
    }

    public void Handle(MoveCustomerCommand command)
    {
        // TODO: Logic here
    }
}

// Again, same implementation as before, but now we depend
// upon the ICommandHandler abstraction.
public class CustomerController : Controller
{
    private ICommandHandler<MoveCustomerCommand> handler;

    public CustomerController(
    ICommandHandler<MoveCustomerCommand> handler)
    {
        this.handler = handler;
    }

    public void MoveCustomer(int customerId, 
        Address newAddress)
    {
        var command = new MoveCustomerCommand
        {
            CustomerId = customerId,
            NewAddress = newAddress
        };

        this.handler.Handle(command);
    }
}

using SimpleInjector;
using SimpleInjector.Extensions;

var container = new Container();

// Go look in all assemblies and register all implementations
// of ICommandHandler<T> by their closed interface:
container.RegisterManyForOpenGeneric(
    typeof(ICommandHandler<>),
    AppDomain.CurrentDomain.GetAssemblies());

// Decorate each returned ICommandHandler<T> object with
// a TransactionCommandHandlerDecorator<T>.
container.RegisterDecorator(typeof(ICommandHandler<>),
    typeof(TransactionCommandHandlerDecorator<>));

// Decorate each returned ICommandHandler<T> object with
// a DeadlockRetryCommandHandlerDecorator<T>.
container.RegisterDecorator(typeof(ICommandHandler<>),
    typeof(DeadlockRetryCommandHandlerDecorator<>));
Was it helpful?

Solution

Here's what your class declarations would look like...

public class CheckoutCustomerCommandHandler :
    ICommandHandler<CheckoutCustomerCommand> {...}

public class MoveCustomerCommandHandler : 
    ICommandHandler<MoveCustomerCommand> {...}

These may look like they implement the same interface, but they actually compile to two different interfaces because the generics arguments are different. Your DI framework will be able to distinguish between them.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top