c# - Replace casting with better pattern -


i'm working on simple client-server solution client can send different types of commands server , specific results. commands can have different properties. i'd have architecture specific command handler chosen based on type of command handles. created basic infrastructure follows:

public interface icommand { }  public class commanda: icommand {     public string customproperty { get; set; } }  public class commandb: icommand { } 

each command has own commandhandler responsible handling command , returning result. inherit commandhandlerbaseclass:

public interface icommandhandler {     bool canhandle(icommand command);     ireply handle(icommand command); }  public abstract class commandhandlerbase<tcommand> : icommandhandler     tcommand : class, icommand {     public bool canhandle(icommand command)     {         return command tcommand;     }      public ireply handle(icommand command)     {         return handle(command tcommand);     }      public abstract ireply handle(tcommand command); }  // specific handler public class commandahandler : commandhandlerbase<commanda> {     public override ireply handle(commanda command)     {         //handling command , returning result         return null;     } } 

i created class responsible choosing suitable handler , returning result:

public interface ireplycreator {     ireply getreply(icommand command); }  public class replycreator : ireplycreator {     private readonly ienumerable<icommandhandler> _commandhandlers;      public replycreator(ienumerable<icommandhandler> commandhandlers)     {         _commandhandlers = commandhandlers;     }      public ireply getreply(icommand command)     {         var commandhandler = _commandhandlers             .firstordefault(x => x.canhandle(command));          if (commandhandler == null)             return null;         return commandhandler.handle(command);     } } 

i don't casting in commandhandlerbase class, can't find patterns avoid it. create generic interface shown below, how register , choose specific handler in replycreator then?

public interface icommandhandler<tcommand>     tcommand : icommand {     bool canhandle(tcommand command);     ireply handle(tcommand command); } 

the commands received in server serialized json.net follows:

jsonconvert.serializeobject(new commanda(), new jsonserializersettings     {         typenamehandling = typenamehandling.all     };) 

so receive string need deserialized concrete command , handled suitable handler. there way of avoiding casts @ in such scenario? use structuremap ioc library.

why trying avoid cast? workarounds come mind now, not nicer this.

i avoid using as keyword purpose. fail silently in unlikely case when wrong type passed handler. in cases this, want exception thrown immediately, not somewhere later in code.


Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -