Note that there are some explanatory texts on larger screens.

plurals
  1. POUnity to Structure Map
    text
    copied!<p>I am trying out the code from this post on <a href="http://elegantcode.com/2010/01/06/event-driven-architecture-publishing-events-using-an-ioc-container/" rel="nofollow noreferrer">Event Driven Architecture</a> (very interesting by the way). His IOC container is Unity though and I would like to do this using Structure map.</p> <p>His code is:</p> <pre><code>public class EventSubscriptions : ISubscriptionService { public static void Add&lt;T&gt;() { var consumerType = typeof(T); consumerType.GetInterfaces() .Where(x =&gt; x.IsGenericType) .Where(x =&gt; x.GetGenericTypeDefinition() == typeof(IConsumer&lt;&gt;)) .ToList() .ForEach(x =&gt; IoC.Container.RegisterType(x, consumerType, consumerType.FullName)); } public IEnumerable&lt;IConsumer&lt;T&gt;&gt; GetSubscriptions&lt;T&gt;() { var consumers = IoC.Container.ResolveAll(typeof(IConsumer&lt;T&gt;)); return consumers.Cast&lt;IConsumer&lt;T&gt;&gt;(); } } </code></pre> <p>I have the following which does not seem to be working:</p> <pre><code>public class SubscriptionService : ISubscriptionService { public static void Add&lt;T&gt;() { var consumerType = typeof(T); consumerType.GetInterfaces() .Where(x =&gt; x.IsGenericType) .Where(x =&gt; x.GetGenericTypeDefinition() == typeof (IConsumer&lt;&gt;)) .ToList().ForEach(x =&gt; ObjectFactory.Inject(consumerType, x)); } public IEnumerable&lt;IConsumer&lt;T&gt;&gt; GetSubscriptions&lt;T&gt;() { var consumers = ObjectFactory.GetAllInstances(typeof(IConsumer&lt;T&gt;)); return consumers.Cast&lt;IConsumer&lt;T&gt;&gt;(); } } </code></pre> <p>I am obviously not too familiar with Structure Map. Some links or explanation on what I am doing wrong would be really appreciated.</p> <p><strong>Update:</strong></p> <p>From Henning's answer, I ended up with -</p> <pre><code>public class SubscriptionService : ISubscriptionService { public IEnumerable&lt;IConsumer&lt;T&gt;&gt; GetSubscriptions&lt;T&gt;() { var consumers = ObjectFactory.GetAllInstances(typeof(IConsumer&lt;T&gt;)); return consumers.Cast&lt;IConsumer&lt;T&gt;&gt;(); } } </code></pre> <p>And then in my bootstrapping class that is called on application startup I have:</p> <pre><code>public static void ConfigureStuctureMap() { ObjectFactory.Initialize(x =&gt; { x.Scan(y =&gt; { y.Assembly("Domain"); y.Assembly("Website"); y.AddAllTypesOf(typeof(IConsumer&lt;&gt;)); y.WithDefaultConventions(); }); }); } </code></pre>
 

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