Reputation: 55
I have just started an ASP.NET 4.6 (MVC 5.2.3) using Visual Studio 2015 using the template, One thing that I saw was this code:
[Authorize]
public class AccountController : Controller
{
private ApplicationSignInManager _signInManager;
private ApplicationUserManager _userManager;
public AccountController()
{
}
public AccountController(ApplicationUserManager userManager, ApplicationSignInManager signInManager )
{
UserManager = userManager;
SignInManager = signInManager;
}
}
As you can see along with default constructor we have second constructor that injects two dependencies, I would like to know, How does ASP.NET 4.6 do that? I didn't see any IoC container configuration
Upvotes: 1
Views: 1696
Reputation: 16358
You are correct. It doesn't.
By default, that constructor isn't being used. Instead, it instantiates those objects when they are accessed.
public ApplicationSignInManager SignInManager
{
get
{
return _signInManager ?? HttpContext.GetOwinContext().Get<ApplicationSignInManager>();
}
private set
{
_signInManager = value;
}
}
public ApplicationUserManager UserManager
{
get
{
return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
}
private set
{
_userManager = value;
}
}
In this case, the getter is using the Null coalescing operator ??
to create the object if it doesn't exist.
The MVC team has put that constructor there for you to use if you want.
Upvotes: 1