Note that there are some explanatory texts on larger screens.

plurals
  1. POUse Container/DependencyResolver in other dll
    text
    copied!<p>I'm trying to get myself familiar with <code>MVC3</code> and <code>autofac</code> but I've encountered small problem that I'm having trouble resolving.</p> <p>I am using <code>autofac</code> integrated with <code>MVC3</code> and all works well, pages are loading correctly, dependencies are being injected and that's cool. What's bugging me is how to use <code>autofac</code>'s <code>Container</code> or <code>MVC</code>'s <code>DependencyResover</code> in class library project.<br> I'm trying to create static class that will help me handle domain events. I simply want to be able to call the method with event parameter and everything should be handeled by this class. Here is code:</p> <pre><code>public static IContainer Container { get; set; } public static void Raise&lt;T&gt;(T e) where T : IDomainEvent { foreach (var eventHandler in DomainEventManager.Container.Resolve&lt;IEnumerable&lt;EventHandlers.Handles&lt;T&gt;&gt;&gt;()) { eventHandler.Handle(e); } } </code></pre> <p>As you can see it's pretty straightforward and everything would work great if it wasn't <code>MVC</code> approach. Some of my dependencies are registeres as <code>InstancePerHttpRequest</code> (<code>NHibernate</code>' session), while other are registered as <code>InstancePerDependency</code> or <code>SingleInstance</code>. Thus when I try to use <code>container</code> created in my UI project, I get exception that there is no <code>httpRequest</code> tag available.</p> <p>How can i reuse the <code>Container</code> created in web project to get access to all of it's features, including <code>InstancePerHttpRequest</code> and <code>httpRequest</code> tag?</p> <p>Or maybe there is other solution to my problem? I was thinking about using delegate function to obtain event handlers, but I cannot (can I?) create generic delegate that I would not need to initialize with concrete type at time of assignment.</p> <p>Why I want to do this using static class is basically every <code>entity</code> and <code>aggregate</code> or <code>service</code> needs to be able to raise domain event. Injecting <code>EventManager</code> into every one of these would be troublesome and static class is exactly what would resolve all my problems.</p> <p>If anyone could help me get my head around it I would be grateful.<br> Cheers, Pako</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