c# 4.0 - Possible to register NullObject implementation as a fallback for a generic interface? -
we use lot of generics in our code. example icommandhandler<t> t icommand, icommandvalidator<t> etc etc
not has icommandvalidator implementation. looking use nullobject pattern provide fall option avoid having test if validator null.
for example
public class nullobjectcommandvalidator : icommandvalidator<icommand> { public bool isvalid(icommand command) { return true; } } we register like:
builder.registerassemblytypes(assemblies) .asclosedtypesof(typeof(icommandvalidator<>)) .instanceperhttprequest(); i hoping able register nullobjectcommandvalidator default icommandvalidator didn't have concrete implementation using process registering other icommandvalidators<> , registering null version @ end , preserving existing defaults.
is possible?
you should change nullobjectcommandvalidator generic type nullobjectcommandvalidator<tcommand>. way can register follows:
builder.registergeneric(typeof(nullobjectcommandvalidator<>)) .as(typeof(icommandvalidator<>)); nullobjectcommandvalidator<tcommand> looks this:
public class nullobjectcommandvalidator<tcommand> : icommandvalidator<tcommand> { public bool isvalid(tcommand command) { return true; } }
Comments
Post a Comment