I am learning to use entity framework( still I am novice in 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 we make use of referencing one more interface within a class like this?:
private IEntityChangeTracker changetracker;
And not even implemented the methods defined in the IEntityChangeTracker interface.
What could be the purpose of using instance of an interface IEntityChangeTracker in the example here.
Until now (while practising C# tutorials), I was defining some skeleton methods in the interface and the class which inherits it needs to provide the implementation for them . This is a classic example, but the code here is something different . Can anybody please let me know the purpose of defining interfaces like this?
When accepting an interface instead of a concrete implementation, you're creating de-coupling between both classes. That means, the caller of the code is free to pass any object which implements the above interface, instead of a specific concrete type. You're saying to the caller "you may pass any object implementing the above interface, i only care about the members/methods declared by it"
This way you're also opening a door to Dependency Injection and allowing the client to support Interface Segregation Principle