c# - Define filter for DecorateAllWith() method in structure-map 3 -
i used following statement decorate icommandhandlers<> decorator1<>:
objectfactory.configure(x => { x.for(typeof(icommandhandler<>)).decorateallwith(typeof(decorator1<>)); }); but because decorator1<> implements icommandhandlers<>, decorator1<> class decorates too.
so, problem decorator1 registers inadvertently, when register icommandhandler<>'s. how can filter decoratewithall()to decorate icommandhandler<>, except decorator1<>?
objectfactory obsolete.
you can exclude decorator1<> being registered vanilla icommandhandler<> code this:
var container = new container(config => { config.scan(scanner => { scanner.assemblycontainingtype(typeof(icommandhandler<>)); scanner.exclude(t => t == typeof(decorator1<>)); scanner.connectimplementationstotypesclosing(typeof(icommandhandler<>)); }); config.for(typeof(icommandhandler<>)).decorateallwith(typeof(decorator1<>)); }); update
for multiple modules can use the registry dsl compose portion of application
using structuremap; using structuremap.configuration.dsl; public class commandhandlerregistry : registry { public commandhandlerregistry() { scan(scanner => { scanner.assemblycontainingtype(typeof(icommandhandler<>)); scanner.exclude(t => t == typeof(decorator1<>)); scanner.connectimplementationstotypesclosing(typeof(icommandhandler<>)); }); for(typeof(icommandhandler<>)).decorateallwith(typeof(decorator1<>)); } } only composition root needs aware of registry implementations.
var container = new container(config => { config.addregistry<commandhandlerregistry>(); }); you have option of finding registry instances @ runtime (you still need ensure required assemblies loaded)
var container = new container(config => { var registries = ( assembly in appdomain.currentdomain.getassemblies() type in assembly.definedtypes typeof(registry).isassignablefrom(type) !type.isabstract !type.namespace.startswith("structuremap") select activator.createinstance(type)) .cast<registry>(); foreach (var registry in registries) { config.addregistry(registry); } });
Comments
Post a Comment