I heard one of the developers at work mention that the whole point of DI is supposed to be the use of interfaces. The usage of concrete classes should be kept to a minimum as a thumb rule. If there are more than a handful instances where we configure DI to use specific classes, then its a code smell.
Now I'm not sure If I can take this as a point blank rule. The one big reason for DI, that I always felt was the ease of Testing.
There may be other reasons to use an interface, over concrete classes as dependency in my code(say ease of plugging in other implementation of a logic, etc.), but I can't see how the use of Dependency Injection implies that.
EDIT: Assume I have a class BookingMapper which maps Booking objects from one domain to another. Let's say it has a Hotel object which it needs to map as a part of mapping the Booking. It uses HotelMapper class to do the same.
public class Booking
{
...
Hotel Hotel;
}
public class BookingMapper
{
private readonly HotelMapper hotelMapper;
public BookingMapper(HotelMapper hotelMapper)
{
this.hotelMapper = hotelMapper;
}
Map(){...}
}
public class HotelMapper
{
Map(){...}
}
In such use cases, I already know that at all points my Booking Mapper component will be at the same level as my HotelMapper, and it is separated out due to Single Responsibilty Principleenter link description here. Does it still make sense for me to extract out an interface like
public interface IHotelMapper
{
void Map();
}
public class BookingMapper
{
private readonly IHotelMapper hotelMapper;
public BookingMapper(IHotelMapper hotelMapper)
{
this.hotelMapper = hotelMapper;
}
Map(){...}
}
and then use that as the dependency in BookingMapper than using HotelMapper directly ?