I'm trying to get myself familiar with MVC3
and autofac
but I've encountered small problem that I'm having trouble resolving.
I am using autofac
integrated with MVC3
and all works well, pages are loading correctly, dependencies are being injected and that's cool. What's bugging me is how to use autofac
's Container
or MVC
's DependencyResover
in class library project.
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:
public static IContainer Container { get; set; }
public static void Raise<T>(T e) where T : IDomainEvent
{
foreach (var eventHandler in DomainEventManager.Container.Resolve<IEnumerable<EventHandlers.Handles<T>>>())
{
eventHandler.Handle(e);
}
}
As you can see it's pretty straightforward and everything would work great if it wasn't MVC
approach. Some of my dependencies are registeres as InstancePerHttpRequest
(NHibernate
' session), while other are registered as InstancePerDependency
or SingleInstance
. Thus when I try to use container
created in my UI project, I get exception that there is no httpRequest
tag available.
How can i reuse the Container
created in web project to get access to all of it's features, including InstancePerHttpRequest
and httpRequest
tag?
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.
Why I want to do this using static class is basically every entity
and aggregate
or service
needs to be able to raise domain event. Injecting EventManager
into every one of these would be troublesome and static class is exactly what would resolve all my problems.
If anyone could help me get my head around it I would be grateful.
Cheers, Pako