none
What's the meaning of these lines RRS feed

  • Question

  • Hi, I'm working on C# ASP.Net MVC, still a newbie...  and want to know the meaning of these below lines.

    I usually use it without knowing what exactly it does... and today want to know what it is.

    When I create default Microsoft template for ASP.Net MVC then I got these.

    also, is there another way to make it?

    Seems lile the last line is a property as I can see (set/get) !!! not sure.

    public AccountController()
                : this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())))
            {
            }
    
            public AccountController(UserManager<ApplicationUser> userManager)
            {
                UserManager = userManager;
            }
    
            public UserManager<ApplicationUser> UserManager { get; private set; }

    Saturday, August 10, 2019 12:19 PM

Answers

  • Hi Christine,

    // 1st constructor
    public AccountController(): 
        this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())))
    {
        // another initialisation code
        //
    }
        /* let start with the constructor line, the first line means that when calling the new() method 
    without arguments,it will then execute the second constructor with the parameters UserManager 
    for which the generic type argument is ApplicationUser which require an argument 
    of type UserStore based on the type of the ApplicationUser etc ...
    this is the same as the following statement,
    you can say the parameterless constructor call the 2nd constructor that takes a userManger as an argument.!!*/
    
    public AccountController() 
    {
        var appDbContext = new ApplicationDbContext();
        var userStore = new UserStore<ApplicationUser>(appDbContext);
        var userManager = new UserManager<ApplicationUser>(userStore);
        // here this(userManager) references the 2nd constructor
        this(userManger);
        // another initialisation code
        //
    }
    
    // 2nd constructor
    public AccountController(UserManager<ApplicationUser> userManager)
    {
         UserManager = userManager;
    }
    // for this part of the code you are right this is an auto-implemented property 
    // with a public getter and a private setter thus you can initialize or change the property state in the same scope.
    public UserManager<ApplicationUser> UserManager { get; private set; }
    

    Important: the article suggested by Karen is an amazing one that explains not just these lines of code, but the User Management mechanism that is implemented by default in an Mvc Application.

    Best Regards





    Saturday, August 10, 2019 1:09 PM

All replies

  • Hello,

    See the following blog post

    https://devblogs.microsoft.com/aspnet/per-request-lifetime-management-for-usermanager-class-in-asp-net-identity/


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Saturday, August 10, 2019 12:32 PM
    Moderator
  • Hi Christine,

    // 1st constructor
    public AccountController(): 
        this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())))
    {
        // another initialisation code
        //
    }
        /* let start with the constructor line, the first line means that when calling the new() method 
    without arguments,it will then execute the second constructor with the parameters UserManager 
    for which the generic type argument is ApplicationUser which require an argument 
    of type UserStore based on the type of the ApplicationUser etc ...
    this is the same as the following statement,
    you can say the parameterless constructor call the 2nd constructor that takes a userManger as an argument.!!*/
    
    public AccountController() 
    {
        var appDbContext = new ApplicationDbContext();
        var userStore = new UserStore<ApplicationUser>(appDbContext);
        var userManager = new UserManager<ApplicationUser>(userStore);
        // here this(userManager) references the 2nd constructor
        this(userManger);
        // another initialisation code
        //
    }
    
    // 2nd constructor
    public AccountController(UserManager<ApplicationUser> userManager)
    {
         UserManager = userManager;
    }
    // for this part of the code you are right this is an auto-implemented property 
    // with a public getter and a private setter thus you can initialize or change the property state in the same scope.
    public UserManager<ApplicationUser> UserManager { get; private set; }
    

    Important: the article suggested by Karen is an amazing one that explains not just these lines of code, but the User Management mechanism that is implemented by default in an Mvc Application.

    Best Regards





    Saturday, August 10, 2019 1:09 PM
  • Why are you concerned with this? Usermanger is part of the temple class that are used when you select sevuirty.
    Sunday, August 11, 2019 4:12 PM
  • I just started in ASP.Net MVC, so I thought it was something I needed to learn it as well.

    Thanks everybody.


    • Edited by Christine25 Tuesday, August 13, 2019 3:56 PM add "it"
    Tuesday, August 13, 2019 3:55 PM
  • You don't need to know anything about Usermanger, which is custom code implemented in the ASP.NET MVC  project when security is selectedu that will be using the Identity database.

    ASP.NET can be discussed at the ASP.NET forums.

    https://forums.asp.net/

    Tuesday, August 13, 2019 8:50 PM
  • Hi Christine,

    I'm glad that those little comments helped!

    Best Regards;


    Tuesday, August 13, 2019 10:04 PM