linq - Is there any way to clean up the following generic method using any of the new C# 4 features? -
i've modified method handling ddd commands (previously had no return type):
public static commandresult<treturn> execute<tcommand, treturn>(tcommand command) tcommand : idomaincommand { var handler = iocfactory.getinstance<icommandhandler<tcommand, treturn>>(); return handler.handle(command); }
the method fine, , want do, using creates fugly code:
commandresult<customer> result = domaincommands.execute<customercreatecommand, customer> ( new customercreatecommand(message) );
before added customer
return type treturn
, nice , tidy , method infer types usage. that's no longer possible.
is there way using new c# features rewrite above make tidier, i.e. using func, action, expression, etc? i'm expecting impossible, i'm getting fed of writing code call single method used simple.
one option reduce have static generic type type parameter can't inferred, allowing have generic method 1 type parameter can inferred:
public static class domaincommands<treturn> { public static commandresult<treturn> execute<tcommand>(tcommand command) tcommand : idomaincommand { var handler = iocfactory.getinstance<icommandhandler<tcommand, treturn>>(); return handler.handle(command); } }
then:
var result = domaincommands<customer>.execute(new customercreatecommand(msg));
it's not much nicer, it's better. of course, if domain command type generic, might - customercreatecommand
implement idomaincommand<customer>
example. if still needed nongeneric idomaincommand, make idomaincommand<t>
derive idomaincommand
.
Comments
Post a Comment