locked
What's going on under hood when instantiating an Interface? RRS feed

  • Question

  • User248267340 posted

    I'm trying hard to understand interfaces beyond the basics. I have a tutorial that works great, but I don't understand something. First, a code snippet from the class:

    public class Repository : IRepository
    {
          private readonly ApplicationDbContext x;
    
          public Repository(ApplicationDbContext iapp)
          {
                x = iapp;
          }
    }

    Next is the code snippet for the Controller:

    public class MyController : ControllerBase
    {
        private readonly IRepository x2;
    
        public MyController(IRepository irep)
        {
            x2 = irep;
        }
    }
    

    Effectively, I now have two readonly variables, x, and x2.

    What's the point of creating the x2 variable from IRepository, since the class already has a variable "x" from the DbContext? In other words,

    it appears to me that data is being created twice, once by the class, and then by the controller. I'm sure I am missing something.

    What's going on under the hood in memory?

    I'd be grateful for any kind of explanation at all. You already know way more than I do!!

    Wednesday, August 19, 2020 8:59 PM

Answers

  • User-474980206 posted

    If you know c, then an interface is just a virtual (jump/pointer) table with defined offsets. There is an enter for each property. .When a class implements an interface, a property is defined for each table entry. When a class instance is created the table is filled in. 

    this is how c++ classes work. Your can also do the same in c, create a struct that is pointers to the “class” properties. You can think of a class as a c struct with an entry for each prop. In addition there is an array of interfaces which are structs with pointers back to properties in the struct. 

    the class is really broken into 2 structs. The static shared by all class instances via a pointer, and the instance with the instance properties. The static struct will also have arrays of type information. So a create a class allocates a new struct and initials all entries. As you can add the instance address to the virtual table address, the virtual tables are defined in the static structure.

    See

    https://en.wikipedia.org/wiki/Virtual_method_table

    note. instead of a heap (malloc), C# uses a tag and sweep GC with generations

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 20, 2020 2:39 PM

All replies

  • User-474980206 posted

    in C# there two general data types. Value types and reference types. primative data types like int, double are value types. classes are reference types.

    when a value type is set to another value type the storage is copied.

       int a = 1;  // copy the value (storage) of the literal 1 to  a

    example

       int a = 1;
       int b = a;  // b == 1
       a = 2;      //  a == 2; b == 1

    when a reference type  is assigned to another, they just point to the same object (storage)

       var a = new MyClass {Value = 1};
       var b = a;
       a.Value = 2; // a.value == 2, b.Value == 2

    there is a special case of immutable object like strings. these are reference types, but can not be modified. 

       var a = "hello";
       var b = a;       // both point to the same object
       a += "!";  // a is set to a new string object, so a == "hello!" and b points to the old "hello";

    so in your example, iapp is defined as a object (a reference  type of ApplicationDbContext). x is the same. the line

       x = iapp;

    sets x to reference to the same object. as x is defined as a read only field, it can only be set in the constructor.

    all this has nothing to do with interfaces, which are type contracts. an interface is an abstract class that defines properties and methods for that interface. 

    while inhertiance allows a new class to inherit the properties and methods of its base class.

     

    Wednesday, August 19, 2020 10:14 PM
  • User248267340 posted

    Thanks so much Bruce. This will sink in over time.

    I just have a tough time grasping interfaces. I know they don't implement, they are a contract. I have trouble because I grew up with ansi C, and I'm used to accessing memory through pointers where I can see the hex location and the values therein.

    Is the interface reference type of "x2" storing the same memory location of "x"?

    Wednesday, August 19, 2020 11:19 PM
  • User1120430333 posted

    A class implements an interface which is a contract between the class/object implementing the interface with the class/object that wants to use the class/object implementing the interface. The interface exposes the public properties or methods in the class/object that implements the interface that a class/object that wants to use the class/object can see and use. 

    You are using a repository over the repositroy EF is already using, which is not optimal programming.

    https://docs.microsoft.com/en-us/dotnet/api/system.data.entity.dbcontext?view=entity-framework-6.2.0

    Thursday, August 20, 2020 12:10 AM
  • User-821857111 posted

    Is the interface reference type of "x2" storing the same memory location of "x"?
    What difference does it make where the objects are stored in memory? That's a framework implementation detail. You don't have to worry about that. 

    it appears to me that data is being created twice, once by the class, and then by the controller
    When the repository is instantiated by the controller, it instantiates an instance of ApplicationDbContext. The controller doesn't instantiate ApplicationDbContext. Nothing is being instantiated twice in the example you posted.

    Thursday, August 20, 2020 7:09 AM
  • User-474980206 posted

    If you know c, then an interface is just a virtual (jump/pointer) table with defined offsets. There is an enter for each property. .When a class implements an interface, a property is defined for each table entry. When a class instance is created the table is filled in. 

    this is how c++ classes work. Your can also do the same in c, create a struct that is pointers to the “class” properties. You can think of a class as a c struct with an entry for each prop. In addition there is an array of interfaces which are structs with pointers back to properties in the struct. 

    the class is really broken into 2 structs. The static shared by all class instances via a pointer, and the instance with the instance properties. The static struct will also have arrays of type information. So a create a class allocates a new struct and initials all entries. As you can add the instance address to the virtual table address, the virtual tables are defined in the static structure.

    See

    https://en.wikipedia.org/wiki/Virtual_method_table

    note. instead of a heap (malloc), C# uses a tag and sweep GC with generations

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 20, 2020 2:39 PM
  • User248267340 posted

    Thanks to everyone who responded. That really helped - I am grateful.

    Bruce, that was what I was trying to ask, and you nailed it. Many tutorials show how to type the syntax without explaining what's being created underneath. I learn best when I know how these things are built.

    Again, thanks!

    Friday, August 21, 2020 5:16 PM