How to decorate a card in the structure?

I am trying to decorate IProjectService, but I can not find documentation for struturemap

For<IProjectService>().Use<ProjectServiceLogDecorator>(); For<IProjectService>().Use<ProjectService>() .DecorateWith((context, decorator) => context.GetInstance<ProjectServiceLogDecorator>()); 

What am I doing wrong?

Exeption:

Bidirectional relationship detected! Check the StructureMap stacktrace below: 1.) the new ProjectServiceLogDecorator (the default is IRepository, the default is IUnitOfWork, the default is IProjectService, the default is IPrincipal, the default is AuditEventLogger) 2.) Project.Services.Projects.ProjectServiceLogDecorator 3.) Instance Project.Services.Projects.ProjectServiceLogDecorator 4.) FuncInterceptor Project.Services.Projects.IProjectService: IContext.GetInstance () 5.) Project.Services.Projects.ProjectService 6.) Instance Project.Services.Projects.IProjectService (Project.Services .Projects.ProjectService) 7.) new UsersController (By default IUserService, By default IService, By default IUserNotificationService, By default IService, By default IProjectService, By default ILicenseGrou pService) 8.) Project.Web.Api.Controllers.UsersController 9.) Instance of Project.Web.Api.Controllers.UsersController 10.) Container.GetInstance (Project.Web.Api.Controllers.UsersController)

I found a solution, but disgusting :

 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>() ) ); 
+6
source share
1 answer

Although there is no documentation yet , there is a complete set of unit tests showing all the different ways to customize a decorator template. I believe you want:

 For<IProjectService>().DecorateAllWith<ProjectServiceLogDecorator>(); For<IProjectService>().Use<ProjectService>(); 

You can add additional decorators simply by doing the following. However, note that the external decorator is the last .DecorateAllWith , so it may be more intuitive to specify the innermost class first.

 For<IProjectService>().Use<ProjectService>(); For<IProjectService>().DecorateAllWith<ProjectServiceLogDecorator>(); For<IProjectService>().DecorateAllWith<SomeOtherDecorator>(); 

Result:

 SomeOtherDecorator ProjectServiceLogDecorator ProjectService 

If you need more control, you can always use smart instances to explicitly apply constructor parameters to the decorator (without having to explicitly specify all parameters).

 var projectService = For<IProjectService>().Use<ProjectService>(); For<IProjectService>().Use<ProjectServiceLogDecorator>() .Ctor<IProjectService>().Is(projectService); 
+4
source

All Articles