I have the following:
public interface ICommand { }
public class AddUser : ICommand
{
public string Name { get; set; }
public string Password { get; set;
To have all of the command handlers injected into the command dispatcher, create a new, non-generic ICommandHandler interface which ICommandHandler<T> derives from. It has an Execute method which takes an object. The downside is that each of your command handlers has to implement that method to call the typed overload:
public class AddUserHandler : ICommandHandler<AddUser>
{
public void Execute(object command)
{
Execute((AddUser)command);
}
public void Execute(AddUser command)
{
Console.WriteLine("{0}: User added: {1}", GetType().Name, command.Name);
}
}
This will enable your command dispatcher to have a constructor dependency on IEnumerable<ICommandHandler>, which StructureMap will automatically populate.
In SendCommand you have 2 ways of getting the appropriate set of handlers. A simple filter based on type:
commandHandlers.OfType<ICommandHandler<T>>
or add a CanHandle(object command) to the ICommandHandler interface:
commandHandlers.Where(x => x.CanHandle(command))
The second approach requires more code, but gives you a little more flexibility by allowing you to attach handler by more than just type. This may make solving your first problem easier, by making your AuditTrailHandler always return true from CanHandle.