none
question regarding Generics RRS feed

  • Question

  • what is the meaning of IdentityDbContext<ApplicationUser>

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext(): base("DefaultConnection")
        {
        }
    
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Entity<IdentityUser>().ToTable("MyUsers");
        }
    }

    i am bit weak in generics. please tell me the meaning of this line public class ApplicationDbContext : IdentityDbContext<ApplicationUser>

    what the above line is doing?

    thanks
    Thursday, December 21, 2017 11:47 AM

Answers

  • Hi,

    May be best to start with some reading up on generics such as here:

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/generics/

    Or just search for "C# generics"

    Briefly, generics allows you to define a class that works with one or more 'generic parameters' that you do not know ahead of time. Its only when the class is actually declared and instantiated that you want to define what those generic parameters are.

    For example, say you want a simple class that holds two related strings.

    You could define the following:

    public class HoldsTwoStrings
    {
        public string ItemOne{get;set;}
        public string ItemTwo{get;set;}
    }

    But then you want a very similar class that holds two related integers. So you define the same thing, but this time with ints:

    public class HoldsTwoInts
    {
        public int ItemOne{get;set;}
        public int ItemTwo{get;set;}
    }

    In fact, you may want to do this for lots of different datatypes and it will obviously get very tedious and unmanageable to keep repeating code.

    So you define a generic class instead:

    public class HoldsTwo<T>
    {
        public T ItemOne{get;set;}
        public T ItemTwo{get;set;}
    }

    Here, the <T> indicates a "parameter of some type T that I don't know yet, but the two properties should be of this type T"

    Its only when you actually declare and use the class that you need to say what "T" is:

    HoldsTwo<string> holdsTwoStrings = HoldsTwo<string>();
    holdsTwoStrings.ItemOne = "This must be a string";
    
    HoldsTwo<int> holdsTwoStrings = HoldsTwo<int>();
    holdsTwoStrings.ItemOne = 42; // will be an int
    
    

    As you can see, I've defined the class once, but used it in two different ways.

    This is generics.

    And this is how the IdentityDbContext class has been defined.

    You can also define constraints on what T can be, which adds flexibility to what you can actually do with objects of type T within your class definition. As I said, read up on the basics of generics first.

    • Marked as answer by Sudip_inn Friday, January 5, 2018 2:49 PM
    Thursday, January 4, 2018 11:51 AM
  • design class like this way class EmployeeList<Employee>

    No, you don't design a class like this. Or rather, you can write this, but it doesn't do what you think it does. If you were to write a class EmpleyeeList<Employee>, then "Employee" does the function of the "T", i.e., it is a mute variable that will be replaced by something else wherever the class is used, it is not the same as a class Employee that you have written elsewhere.

    I think that you are confused with a method that returns a generic, or takes a generic as a parameter. For example:

    public List<Employee> MyMethod(IEnumerable<Employee> myParameter) {...}

    In this case it just indicates the type of parameters or results that you are passing.

    • Marked as answer by Sudip_inn Saturday, January 6, 2018 5:58 PM
    Friday, January 5, 2018 5:42 PM
    Moderator

All replies

  • This means that whoever wrote the class IdentityDbContext wrote it like this:

    public class IdentitiyDbContext<T> (...) { Do things with T }

    So when you call it as IdentityDbContext<ApplicationUser> it uses ApplicationUser in all the places where the class definition used T. It's a concept similar to passing arguments to a subroutine, except that in this case the T is a class instead of a value.

    Thursday, December 21, 2017 2:33 PM
    Moderator
  • IdentityDbContext is a generic class which means that it will take a type from the calle whoever will be using it will specify a type which should be used and framework and that class instance would be used inside the IdentityDbContext<T> whichever has been passed by the developer.

    Hope it gives you some idea.


    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, December 21, 2017 2:45 PM
  • Hello Sudip_inn,

    You could get more details from MSDN.

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/generics/

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, December 22, 2017 6:19 AM
    Moderator
  • Hello Sudip_inn,

    Is there any update or any other assistance I could provide? You could mark the helpful reply as answer if the issue has been solved. And if you have any concerns, please do not hesitate to let us know.

    Thank you for your understanding and patience!

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, December 27, 2017 7:36 AM
    Moderator
  • @Ehsan do not understand what you try to say. i am not advance developer. so please explain it with a nice example.

    Thursday, January 4, 2018 11:32 AM
  • Hi,

    May be best to start with some reading up on generics such as here:

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/generics/

    Or just search for "C# generics"

    Briefly, generics allows you to define a class that works with one or more 'generic parameters' that you do not know ahead of time. Its only when the class is actually declared and instantiated that you want to define what those generic parameters are.

    For example, say you want a simple class that holds two related strings.

    You could define the following:

    public class HoldsTwoStrings
    {
        public string ItemOne{get;set;}
        public string ItemTwo{get;set;}
    }

    But then you want a very similar class that holds two related integers. So you define the same thing, but this time with ints:

    public class HoldsTwoInts
    {
        public int ItemOne{get;set;}
        public int ItemTwo{get;set;}
    }

    In fact, you may want to do this for lots of different datatypes and it will obviously get very tedious and unmanageable to keep repeating code.

    So you define a generic class instead:

    public class HoldsTwo<T>
    {
        public T ItemOne{get;set;}
        public T ItemTwo{get;set;}
    }

    Here, the <T> indicates a "parameter of some type T that I don't know yet, but the two properties should be of this type T"

    Its only when you actually declare and use the class that you need to say what "T" is:

    HoldsTwo<string> holdsTwoStrings = HoldsTwo<string>();
    holdsTwoStrings.ItemOne = "This must be a string";
    
    HoldsTwo<int> holdsTwoStrings = HoldsTwo<int>();
    holdsTwoStrings.ItemOne = 42; // will be an int
    
    

    As you can see, I've defined the class once, but used it in two different ways.

    This is generics.

    And this is how the IdentityDbContext class has been defined.

    You can also define constraints on what T can be, which adds flexibility to what you can actually do with objects of type T within your class definition. As I said, read up on the basics of generics first.

    • Marked as answer by Sudip_inn Friday, January 5, 2018 2:49 PM
    Thursday, January 4, 2018 11:51 AM
  • can you show me with some code example that when people design class like this way

    i have seen people design class like this way when they use generic

    class EmployeeList<T> where T : Employee

    but when design class like this way class EmployeeList<Employee> ?

    please come with a nice example where you show generic used class1<class2> i need to know when people write code this way.....what is the significant of this code?

    looking for a good example. thanks

    Friday, January 5, 2018 2:53 PM
  • design class like this way class EmployeeList<Employee>

    No, you don't design a class like this. Or rather, you can write this, but it doesn't do what you think it does. If you were to write a class EmpleyeeList<Employee>, then "Employee" does the function of the "T", i.e., it is a mute variable that will be replaced by something else wherever the class is used, it is not the same as a class Employee that you have written elsewhere.

    I think that you are confused with a method that returns a generic, or takes a generic as a parameter. For example:

    public List<Employee> MyMethod(IEnumerable<Employee> myParameter) {...}

    In this case it just indicates the type of parameters or results that you are passing.

    • Marked as answer by Sudip_inn Saturday, January 6, 2018 5:58 PM
    Friday, January 5, 2018 5:42 PM
    Moderator