c# - Autofac resolve dependency in CQRS CommandDispatcher -


I'm trying to implement a simple CQRS-application example.

This is my composition "Command" part:

  Public interface ICMMand {} / Command handlers Interface for IcmandHandler Base interface & lt; TCommand & gt; Where TComond: ICMMand {executed by Tickmount command; } // Example of Local Symphony Commands: ICMAND {// Some Properties} // Simple Commands Command Handler Public Class Simple Commands Handler Example: ICMandHandler & lt; Simple Commands & gt; {Executing Public Zero (Simple Command Command) {// Some Arguments}}  

This interface is ICommandDipatcher . It sends commands to your handler.

  The public interface ICMMand Dispatcher (zero dispatch) is the command of the TComand Command (TCom Command);  

this ICommandDispatcher There is a default implementation of and the main problem is that the code handler required by Autofac public class DefaultCommandDispatcher: ICommandDispatcher {public Zero Dispatch & LT; TCommand & gt; (TCommand Order) where TCommand: ICommand {// How to Solve / By Nese To get the object of ccary command handler // type of command (tycomand) handler, extex (command);}

Autofac What is the best way to resolve the implementation of ICommandHanler by order?

Thanks!

With Autofac, you can call back to the container in this way in the IComponentContext sender to resolve the required command handler: public class AutofacCommandDispatcher: ICommandDispatcher {private only IComponentContext reference to read; Public AutofacCommandDispatcher (IComponentContext reference) {this.context = context; } Public Zero Dispatch & LT; TCommand & gt; (TCommand order) {var handler = this.context.Resolve & LT; ICommandHandler & LT; TCommand & gt; & Gt; (); Zero handler Exact (command); }}

You can register as AutofacCommandDispatcher as:.

  builder.RegisterType & lt; AutofacCommandDispatcher & gt; () As the & lt; ICommandDispatcher & gt; ();  

And thus you can register all your command operators at a time:

  builder.RegisterAssemblyTypes (myAssembly) .AsClosedTypesOf (typeof (ICommandHandler & Lt; & gt;);;  

Although two notes are not t & gt; (With keyword ) as the contravariant because Resharper has said so, but this command is a bad idea for operators First of all, you probably define ICommandHandler & lt Done There is always one-to-one mapping between command and command handler, but defines the keyword in , indicating that many implementations can occur. Second, in my opinion, a command dispatcher is a bad idea, because it can hide the fact that the consumer class of the command operator has too many dependencies, which violate the single liability principle. The signal is In addition, the use of such a dispatcher creates part of the object graph (part of the command handler) until the order is actually executed (as opposed to when the consumer is resolved). This makes it difficult to verify the container's configuration when command handlers are injected directly, so you know to make sure that the entire object graph can be resolved, while the root type can be solved in your configuration . It is easy to define a command, but forget to create related command handler, so you will need to add unit tests to check that what is the handler related to each order if you remove the dispatcher all together So you can save yourself from writing such tests.


Comments