c# - command pattern using Dependency Injection for MVC 3 controller -
I read the following article which was suggested by another stack overflow user which outlines the command pattern and its How to use at the end of the article with DI
This has been of great help but one thing that I am missing, let me suppose that I create a new class named "CodeCustomerCommandHandler"
Now, suppose I need to include this command and the In the core, they both implement the same interface. It looks as if there is a lookup issue for the DI Container, in the example of the article, their sample Injector setup is: How these are your class announcements ... It may seem that they implement the same interface, but they actually compile for two separate interfaces because the generic logic is different The D Framework will be able to differentiate between them. MoveCustomerCommandHandler in the controller for any reason through the controller. How does this affect the container setup and the constructor?
Public Interface ICMMandHandler & lt; TCommand & gt; {Zero handle (tickmomment command); // // just like before, but now with the interface Public Class MoveCustomerCommandHandler: ICommandHandler & lt; MoveCustomerCommand & gt; {Private Redonline Unitofwork DB; Public Human Capital Commands Handler (Unitofwork DB, [other dependencies here]) {this.dbb = db; } Public Zero Handle (MoveCustomerCommand Order) {// TODO: Logic here}} // Again, the implementation as before, but now we are on the ICMMandandHandler Abstract / Public Class Customer Controller: Controller {Private ICommandHandler & lt; MoveCustomerCommand & gt; Handler; Public Customer Controller (ICMandHandler & Move; Move Customer Client & gt; Handler) {this.handler = Handler; } Public Zero Move customer (int customer ID, address newer) {var command = new haul customer comand (customer ID = customer ID, newAd = new account); This.handler.Handle (order); }} Using a simple injector; Using SimpleInjector.Extensions; Var Container = New Container (); // See in all parliaments and all implementations // ICMMandHandler's & lt; T & gt; By their closed interface: Container. Registrar Many Forgerenic (Typef (ICMandHandler>), App Domain. Current Domain. GetAssemblies ()); // To decorate your returned ICMMandHandler & lt; T & gt; // Item with a Commodity Commands Handler Dictator & lt; T & gt; Container.DistrictDecorator (typef (ICMandHandler & lt; & gt;), typef (transaction commandhandler dancerator & lt; & gt;); // To decorate your returned ICMMandHandler & lt; T & gt; // A Deadlock Route Commands Handler Dictator & lt; T & gt; With Object Container Registrar decontroller (typef (ICMandHandler & lt; & gt;), typef (deadlockersmandhandler dossator & lt; & gt;));
Public Class Checkout Clients Customer Handler: ICMandHandler & lt; CheckoutCustomerCommand & gt; {...} Public Class MoveCustomerCommandHandler: ICommandHandler & lt; MoveCustomerCommand & gt; {...}
Comments
Post a Comment