Creating an interface object in C #

I am learning to use entity framework (still I'm new to entity framework and C #),

While going through one of the tutorials, I came across the following piece of code:

 public class EntityF : IEntityWithChangeTracker,IEntityWithKey
    {
        private IEntityChangeTracker changetracker;

        public void SetChangeTracker(IEntityChangeTracker changetracker)
        {
            this.changetracker = changetracker;
        } 
    }

      

Can I use a reference to another interface inside a class?

 private IEntityChangeTracker changetracker;

      

And the methods defined in the IEntityChangeTracker interface are not even implemented .

What might be the purpose of using an instance of the IEntityChangeTracker interface in this example.

So far (with hands-on learning C #) I've defined some skeletal methods in the interface, and the class that inherits from it has to provide an implementation for them. This is a classic example, but something different here. Can someone please let me know the purpose of defining such interfaces?

+3


source to share


1 answer


What could be the purpose of using an instance of the IEntityChangeTracker interface in the example here.

By adopting an interface instead of a concrete implementation, you create a de-link between both classes. This means that the calling code can pass any object that implements the above interface, rather than a specific concrete type. You tell the caller "you can pass any object that implements the above interface, I only care about the members / methods it declares"



This way you also open the door to Dependency Injection and allow the client to support the Chain Segregation Principle

+4


source







All Articles