Created
September 14, 2012 09:54
-
-
Save KennyEliasson/3721094 to your computer and use it in GitHub Desktop.
Asmx command pipe
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[XmlInclude(typeof(UpdateIdentityPermissionCommand))] | |
[XmlInclude(typeof(RemoveIdentityAccessCommand))] | |
[XmlInclude(typeof(UpdateProfileImageCommand))] | |
//Måste inkludera alla subklasser, suger men så är livet :) | |
public class Command : MessageBase //Om ni nu har en MessageBase | |
{ } | |
//Exempel command | |
public class UpdateProfileImageCommand : Command | |
{ | |
public int IdentityId { get; set; } | |
public string FileName { get; set; } | |
} | |
//CommandHandlers | |
public abstract class Handles<T> : IHandleCommand where T : Command | |
{ | |
public CommandResponse Execute(object command) | |
{ | |
//Här kan man logga etc :P | |
return Process((T)command); | |
} | |
public abstract CommandResponse Process(T command); | |
} | |
//Ärv denna sen typ | |
public class ChangeIdentityImage : Handles<UpdateProfileImageCommand> { } | |
//I webservicen | |
[WebMethod(true, Description = "Execute a command")] | |
public CommandResponse ExecuteCommand(Command command) | |
{ | |
//StructureMap för att Hitta Handlers | |
var commandHandlerType = typeof(Handles<>).MakeGenericType(command.GetType()); | |
var commandHandler = ObjectFactory.GetInstance(commandHandlerType) as IHandleCommand; | |
//Vill få ett exception om CommandHandler till Type inte finns | |
return commandHandler.Execute(command); | |
} | |
//Alla Handlers returnerar tillbaka CommandResponse, vill man att de skall säga mer måste man göra samma sak som med Commands, dvs [XmlInclude] på andra responsen. | |
//Lika bra att kunna executa flera commands i rad :) T.ex. workflows eller slippa flera anrop över HTTP | |
//De körs i synk atm, kanske ha en flagga så kan man köra async! | |
[WebMethod(true, Description = "Execute a command")] | |
public List<CommandResponse> ExecuteCommands(List<Command> commands) | |
{ | |
var results = new List<CommandResponse>(); | |
foreach (var command in commands) { | |
var commandHandlerType = typeof(Handles<>).MakeGenericType(command.GetType()); | |
var commandHandler = ObjectFactory.GetInstance(commandHandlerType) as IHandleCommand; | |
//Vill få ett exception om CommandHandler till Type inte finns | |
results.Add(commandHandler.Execute(command)); | |
} | |
return results; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment