Note that there are some explanatory texts on larger screens.

plurals
  1. POSimple injector open generic decorators
    primarykey
    data
    text
    <p>I am trying to make use of some of the nice features in simple injector.</p> <p>I am currently having problems with the decorators, they are not getting hit when I expect them too.</p> <p>I am registering them like this:</p> <pre class="lang-cs prettyprint-override"><code>container.RegisterManyForOpenGeneric( typeof(ICommandHandler&lt;&gt;), AppDomain.CurrentDomain.GetAssemblies()); container.RegisterDecorator( typeof(ICommandHandler&lt;&gt;), typeof(CreateValidFriendlyUrlCommandHandler&lt;&gt;), context =&gt; context.ServiceType == typeof(ICommandHandler&lt;CreateProductCommand&gt;) ); container.RegisterDecorator( typeof(ICommandHandler&lt;&gt;), typeof(CreateProductValidationCommandHandler&lt;&gt;), context =&gt; context.ServiceType == typeof(ICommandHandler&lt;CreateProductCommand&gt;) ); </code></pre> <p>I think I must be missing something as I am expecting that a call to <code>ICommandHandler&lt;CreateProductCommand&gt;</code> will invoke <code>CreateValidFriendlyUrlCommandHandler&lt;&gt;</code> and <code>CreateProductValidationCommandHandler&lt;&gt;</code> before running itself.</p> <p>I have tried a different registration like this:</p> <pre class="lang-cs prettyprint-override"><code>container.RegisterManyForOpenGeneric( typeof(ICommandHandler&lt;&gt;), AppDomain.CurrentDomain.GetAssemblies()); container.RegisterDecorator( typeof(ICommandHandler&lt;&gt;), typeof(CreateValidFriendlyUrlCommandHandler&lt;&gt;), context =&gt; context.ImplementationType == typeof(CreateProductCommandHandler) ); container.RegisterDecorator( typeof(ICommandHandler&lt;&gt;), typeof(CreateProductValidationCommandHandler&lt;&gt;), context =&gt; context.ImplementationType == typeof(CreateProductCommandHandler) ); </code></pre> <p>As I thought registering a decorator for <code>ICommandHandler&lt;CreateProductCommand&gt;</code> on the type <code>ICommandHandler&lt;CreateProductCommand&gt;</code> when the <code>CreateProductValidationCommandHandler</code> and <code>CreateValidFriendlyUrlCommandHandler</code> implement <code>ICommandHandler&lt;CreateProductCommand&gt;</code> may well hit a bit of a circular reference.</p> <p>But changing that made no difference.</p> <p>Here is my <code>CreateProductValidationCommandHandler&lt;TCommand&gt;</code>:</p> <pre class="lang-cs prettyprint-override"><code>public class CreateProductValidationCommandHandler&lt;TCommand&gt; : ICommandHandler&lt;CreateProductCommand&gt; { private readonly ICommandHandler&lt;TCommand&gt; decorated; private readonly IValidationService validationService; public CreateProductValidationCommandHandler( ICommandHandler&lt;TCommand&gt; decorated, IValidationService validationService) { this.decorated = decorated; this.validationService = validationService; } public void Handle(CreateProductCommand command) { if (!validationService.IsValidFriendlyName( command.Product.ProductFriendlyUrl)) { command.ModelStateDictionary.AddModelError( "ProductFriendlyUrl", "The Friendly Product Name is not valid..."); return; } if (!validationService.IsUniqueFriendlyName( command.Product.ProductFriendlyUrl)) { command.ModelStateDictionary.AddModelError( "ProductFriendlyUrl", "The Friendly Product Name is ..."); return; } } } </code></pre> <p>And this is my <code>CreateValidFriendlyUrlCommandHandler&lt;TCommand&gt;</code>:</p> <pre><code>public class CreateValidFriendlyUrlCommandHandler&lt;TCommand&gt; : ICommandHandler&lt;CreateProductCommand&gt; { private readonly ICommandHandler&lt;TCommand&gt; decorated; public CreateValidFriendlyUrlCommandHandler(ICommandHandler&lt;TCommand&gt; decorated) { this.decorated = decorated; } public void Handle(CreateProductCommand command) { if (string.IsNullOrWhiteSpace( command.Product.ProductFriendlyUrl)) { command.Product.ProductFriendlyUrl = MakeFriendlyUrl(command.Product.Name); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload