Note that there are some explanatory texts on larger screens.

plurals
  1. POStructure Map Generic Type Scanner
    text
    copied!<p><strong>High Level</strong></p> <p>With StructureMap, Can I define a assembly scan rule that for an interface <code>IRequestService&lt;T&gt;</code> will return the object named TRequestService</p> <p>Examples:</p> <ul> <li><code>FooRequestService</code> is injected when <code>IRequestService&lt;FooRequest&gt;</code> is requested</li> <li><code>BarRequestService</code> is injected when <code>IRequestService&lt;BarRequest&gt;</code> is requested</li> </ul> <p><strong>Details</strong></p> <p>I have a generic interface defined</p> <pre><code>public interface IRequestService&lt;T&gt; where T : Request { Response TransformRequest(T request, User current); } </code></pre> <p>and then I have multiple Request objects that implement this interface</p> <pre><code>public class FooRequestService : IRequestService&lt;Foo&gt; { public Response TransformRequest(Foo request, User current) { ... } } public class BarRequestService : IRequestService&lt;Bar&gt; { public Response TransformRequest(Bar request, User current) { ... } } </code></pre> <p>Now I am at the point where I need to register these classes so that StructureMap knows how to create them because in my controller I want have the following ctor (which I want StructureMap to inject a <code>FooRequestService</code> into)</p> <pre><code>public MyController(IRequestService&lt;Foo&gt; fooRequestService) { ... } </code></pre> <p>Right now to get around my issue I have implemented an empty interface and instead of having the <code>FooRequestService</code> implement the generic interface I have it implement this empty interface</p> <pre><code>public interface IFooRequestService : IRequestService&lt;Foo&gt; { } </code></pre> <p>Then my controllers ctor looks like so, which works with StructureMaps' Default Convention Scanner</p> <pre><code>public MyController(IFooRequestService fooRequestService) { ... } </code></pre> <p>How could I create a rule with StructureMap's assembly scanner to register all objects named TRequestService with <code>IRequestService&lt;T&gt;</code> (where T = "Foo", "Bar", etc) so that I don't have to create these empty Interface definitions?</p> <p>To throw something else into the mix, where I am handling StructureMap's assembly scanning does not have any reference to the assembly that defines <code>IRequestService&lt;T&gt;</code> so this has to use some sort of reflection when doing this. I scanned the answer to "<a href="https://stackoverflow.com/questions/516892/structuremap-auto-registration-for-generic-types-using-scan">StructureMap Auto registration for generic types using Scan</a>" but it seems as though that answer requires a reference to the assembly that contains the interface definition.</p> <p>I am on the path of trying to write a custom StructureMap.Graph.ITypeScanner but I am kind of stuck on what to do there (mainly because I have little experience with reflection).</p>
 

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