unitycontainer / aspnet-mvc

ASP.NET MVC 5 Adapter
Apache License 2.0
13 stars 10 forks source link

No support for HierarchicalLifetimeManager #8

Closed nikaburu closed 5 years ago

nikaburu commented 6 years ago

PerRequestLifetimeManager.cs does not meet criteria of using scoped dependencies.

Unity container has well established HierarchicalLifetimeManager and MVC integration should just create a child container on each request to make it work. In this way HierarchicalLifetimeManager can be used instead of PerRequestLifetimeManager and for other advanced scenarios.

Here is a integration lib that supports this scenario: https://github.com/devtrends/Unity.Mvc5/blob/master/Unity.Mvc5/UnityDependencyResolver.cs.

I'd say you should contact @devtrends and integrate both libs under official umbrella.

ENikS commented 6 years ago

What exactly is wrong with it?

nikaburu commented 6 years ago

It simply does not work, each request runs under an application container (instead of own child container).

One way to fix for me is to use PerRequestLifetimeManager in my code, but I don't want dependency on this mvc lib out of my web layer. And it seems wrong do not use build-in HierarchicalLifetimeManager.

The other way is to have child container created for each requests by default with this lib, even if PerRequestLifetimeManager is not used. Then HierarchicalLifetimeManager may be used.

Btw, it works in this way with your integration to asp.net core, where each request is scoped and child container created.

Todo: 1) mark PerRequestLifetimeManager as obsolete (and remove in future versions) 2) create a child container per each request (see link above how it may be done)

nikaburu commented 6 years ago

Btw, one of the useful feature of Unity.Mvc5 that is does not require to add asp net module manually on appstart but nicely hidden in the lib: https://github.com/devtrends/Unity.Mvc5/blob/master/Unity.Mvc5/Properties/AssemblyInfo.cs

Just less code to carry with your own app.

nikaburu commented 6 years ago

wrong button..

ENikS commented 5 years ago

This library has nothing to do with https://github.com/devtrends/Unity.Mvc5 You might want to contact @devtrends to address it.

I would be open to merging functionality of both into this project but I would need help because I have very limited knowledge of MVC.