asp.net mvc - How do I inject an HTTP-Request-specific object into my Unity supplied object? -
for example, store "current user" in session. business-layer object being instantiated unity. how make business-layer object aware of "current user"?
you should hide "current user" behind abstraction:
public interface icurrentuser { string name { get; } }
this abstraction should defined in business layer , need create asp.net specific implementation place in composition root:
public class aspnetcurrentuser : icurrentuser { public string name { { return httpcontext.current.session["user"]; } } }
now business-layer object can depend on icurrentuser
interface, , in unity can register implementation follows:
container.registertype<icurrentuser, aspnetcurrentuser>();
Comments
Post a Comment