How do I insert an object specific to an HTTP request into my object supplied by Unity?

For example, I save the "current user" in the session. The business layer object is created by Unity. How to make a business-level object aware of the "current user"?

+6
source share
1 answer

You must hide the "current user" behind the abstraction:

public interface ICurrentUser { string Name { get; } } 

This abstraction needs to be defined at the business level, and you need to create a specific ASP.NET implementation that you put in the Root of Composition :

 public class AspNetCurrentUser : ICurrentUser { public string Name { get { return HttpContext.Current.Session["user"]; } } } 

Now your business-level object may depend on the ICurrentUser interface, and in Unity you can register the implementation as follows:

 container.RegisterType<ICurrentUser, AspNetCurrentUser>(); 
+13
source

Source: https://habr.com/ru/post/944131/


All Articles