Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to write a StructureMap Scanner for this case
    text
    copied!<p>I have these registrations in a registry and am trying to figure out the correct way to implement them with a scanner instead of manual registration.</p> <pre><code>For&lt;ISomeView&gt;.Use(HttpContext.Current.CurrentHandler) For&lt;IOtherView&gt;.Use(HttpContext.Current.CurrentHandler) For&lt;IAnotherView&gt;.Use(HttpContext.Current.CurrentHandler) </code></pre> <p>And so on. I have ever one of my interfaces inherent from <code>IView</code> to be used as a marker interface.</p> <p>My original attempt came out looking like this</p> <pre><code>public void Process(Type type, Registry registry) { Type _pluginType = typeof(IView); if (type.CanBeCastTo(_pluginType) &amp;&amp; type.IsInterface) { string name = type.Name; registry.AddType(_pluginType, type, name); registry.For(_pluginType).Use(HttpContext.Current.CurrentHandler); } } </code></pre> <p>This however caused my physical ASP.NET pages to be registered as itself instead. This is what I got from WhatDoIHave()</p> <pre><code>the_page_aspx (ASP.the_page_aspx) - 36d0cdb2-7118-4a1d-94a0-8de1b5ddc357 - Configured Instance of ASP.the_page_aspx, App_Web_4q8pukge, Version... </code></pre> <p><strong>Edit:</strong> To respond to KevM's comment what I want to acheive is anytime StructureMap needs to inject any single of my IView's that it resolves it by by returning <code>HttpContext.Current.CurrentHandler</code></p> <p>So if I'd call <code>ObjectFactory.Resolve&lt;ISomeView&gt;()</code> I would get <code>(ISomeView)HttpContext.Current.CurrentHandler</code> </p> <p>If I have a constructor that was <code>SomePresenter(IListView listView, IEditView editView)</code> that those would be resolved to <code>(IListView)HttpContext.Current.CurrentHandler</code> and <code>(IEditView)HttpContext.Current.CurrentHandler</code>. </p> <p>I could do this with numerous <code>For&lt;&gt;.Use()</code> statements as in my example which means I should be able to achieve it with a scanner instead of needing to explicitly register each one manually. My interfaces will always be named I_____View if the naming convention would help for writing the scanner. I'm just not sure what I need to use in the if statement I have above for the <code>Process()</code> method.</p> <p><strong>Update:</strong> With the answer from @KevM it pointed me in the right direction to this</p> <pre><code>public class ViewScanner : IRegistrationConvention { public void Process(Type type, Registry registry) { if (type.IsA&lt;IView&gt;() &amp;&amp; !type.IsConcrete()) { registry.For(type).Use(c =&gt; HttpContext.Current.CurrentHandler); } } } </code></pre> <p>(IsA&lt;> is just an extension for IsAssignableFrom since to me the usage of it feels backwards)</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