c# - How decorate in structuremap? -
i try decorate iprojectservice can't find documentation struturemap
for<iprojectservice>().use<projectservicelogdecorator>(); for<iprojectservice>().use<projectservice>() .decoratewith((context, decorator) => context.getinstance<projectservicelogdecorator>());
what wrong?
exeption:
bi-directional dependency relationship detected! check structuremap stacktrace below: 1.) new projectservicelogdecorator(default of irepository, default of iunitofwork, default of iprojectservice, default of iprincipal, default of auditeventlogger) 2.) project.services.projects.projectservicelogdecorator 3.) instance of project.services.projects.projectservicelogdecorator 4.) funcinterceptor of project.services.projects.iprojectservice: icontext.getinstance() 5.) project.services.projects.projectservice 6.) instance of project.services.projects.iprojectservice (project.services.projects.projectservice) 7.) new userscontroller(default of iuserservice, default of iservice, default of iusernotificationservice, default of iservice, default of iprojectservice, default of ilicensegroupservice) 8.) project.web.api.controllers.userscontroller 9.) instance of project.web.api.controllers.userscontroller 10.) container.getinstance(project.web.api.controllers.userscontroller)
i found solution disgustingly:
for<iprojectservice>().use<projectservice>().decoratewith((ctx, service) => new projectservicelogdecorator( ctx.getinstance<irepository<project>>(), ctx.getinstance<iunitofwork>(), service, ctx.getinstance<ilicenseservice>(), ctx.getinstance<iprincipal>(), ctx.getinstance<auditeventlogger>() ));
although there no documentation yet, there complete set of unit tests showing of different ways can configure decorator pattern. believe 1 want is:
for<iprojectservice>().decorateallwith<projectservicelogdecorator>(); for<iprojectservice>().use<projectservice>();
you can add additional decorators doing following. however, note outermost decorator last .decorateallwith
specified, might more intuitive specify innermost class first.
for<iprojectservice>().use<projectservice>(); for<iprojectservice>().decorateallwith<projectservicelogdecorator>(); for<iprojectservice>().decorateallwith<someotherdecorator>();
which results in:
someotherdecorator projectservicelogdecorator projectservice
if need more control that, can use smart instances apply constructor parameters decorator explicitly (without needing supply parameters explicitly).
var projectservice = for<iprojectservice>().use<projectservice>(); for<iprojectservice>().use<projectservicelogdecorator>() .ctor<iprojectservice>().is(projectservice);
Comments
Post a Comment