asp.net mvc 3 - Use Container/DependencyResolver in other dll -


I try to familiarize myself with MVC3 and autofac But I had to face a small problem that I am having trouble solving the problem.

I am integrating autofac with MVC3 and all work well, pages are loading properly Dependencies are being injected and it is good. Do I have to provoke how to use autofac for container or MVC for DependencyResover class library in the project.
I am trying to create a stable class which will help me manage domain events. I just want to be able to call the method with the event parameter and everything should be handed over by this class. Here's the code:

  Public Fixed IContainer Container {get; Set; } Increase Public static void & lt; T & gt; (TE) where T: IDomainEvent {foreach (eventhandler; IEnumerable & lt; EventHandlers.Handles & lt; T & gt; & gt; & gt; ()) {eventHandler.Handle (e) on eventEdemainEvent {foreach (DomainEventManager.Container.Resolve & lt; ); }}   

As you can see it is very straightforward and everything will work very well if it was not MVC approach, some of my dependencies, Regions are registered as code> InstancePerHttpRequest ( NHibernate 'session), while others are registered in InstancePerDependency or SingleInstance . Thus, when I try to use the container in my UI project, I get an exception that no httpRequest tag is not available.

How can I reuse the container created in the web project to get access to all of InstancePerHttpRequest and HttpRequest tag?

Or maybe there is the facilities, what is the solution to my problem? I was thinking about using the delegate function to get the event handlers, but I can not (I?) Make a generic representative that I will not need to start with concrete type at the time of assignment.

Why should I use this stable class to basically increase every entity and total or service to create domain events should be there. In each of these, it will be difficult to inject the code and the stable class will actually solve all my problems.

If someone can help me get his head around it, then I will be grateful.
Cheers, Paco

You should not directly refer your container to your app code. This service looks like a locator anti-pattern. The correct verb is to pass the necessary services to their objects to do their job, usually through constructor parameters. But ... if you insist on accepting a global stable, at least the model EventManager as a singleton, then such an experiment looks:

  EventManager.Current.Raise & LT; SomeEvent & gt; (SomeObject);   

And then when you start your code you can set the EventManager.Current equal to the example that is prepared properly.

Comments

Popular posts from this blog

Python SQLAlchemy:AttributeError: Neither 'Column' object nor 'Comparator' object has an attribute 'schema' -

java - How not to audit a join table and related entities using Hibernate Envers? -

mongodb - CakePHP paginator ignoring order, but only for certain values -