Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dependency Injection with Multiple Interfaces

I am just learning Interface Segregation Principle. But after learning i am confused with the scenario in the example.

The concept is separating the interfaces into simple interfaces. That is well but my question is with hierarchy model or not?

Take the example i studied in the book.

I have one Interface for product with the following properties

public interface IProduct
{
decimal Price { get; set; }
decimal WeightInKg { get; set; }
int Stock { get; set; }
int Certification { get; set; }
int RunningTime { get; set; }
}

I just simplify with one class implmentation from the interface

public class DVD : IProduct
{
public decimal Price { get; set; }
public decimal WeightInKg { get; set; }
public int Stock { get; set; }
public int Certification { get; set; }
public int RunningTime { get; set; }
}

The problem is when apply to the other categories which have not related properties. When create a class for TShirt, there is no needed for Certification and RunningTime. So as per the Interface Segregation Principle, the interface is separated like below

Create a new interface, move the movie related properties to this one like below

public interface IMovie
{
int Certification { get; set; }
int RunningTime { get; set; }
}

So the IProduct does not have these properties and the implementation like below

public class TShirt : IProduct
{
public decimal Price { get; set; }
public decimal WeightInKg { get; set; }
public int Stock { get; set; }
}

public class DVD : IProduct, IMovie
{
public decimal Price { get; set; }
public decimal WeightInKg { get; set; }
public int Stock { get; set; }
public int Certification { get; set; }
public int RunningTime { get; set; }
}

Conceptionally i am ok with this. But if this about real method implementation like this. When i use dependency injection which interface i used as the type for DVD class.

I am confused or i missing something ? If i apply the inheritance logic we can use the lower level interface so base interface also inherited. But if i use like this how can implement?

like image 725
Akhil Avatar asked Aug 13 '14 02:08

Akhil


People also ask

Does dependency injection need interface?

To use DI, we must inject dependencies into other classes that depend upon them. These dependencies need an abstraction (which is usually an interface), and the dependent class will invoke that abstraction when it needs the corresponding functionality.

What is difference between Addtransient and AddScoped and Addsingleton?

Singleton is a single instance for the lifetime of the application domain. Scoped is a single instance for the duration of the scoped request, which means per HTTP request in ASP.NET. Transient is a single instance per code request.

CAN interface have multiple implementations C#?

Using the delegate func Introduce three implementation classes as below- three different classes where we have implemented the same interface. ASP.NET Core has built-in support for dependency injection. However, multiple implementations of an interface in ASP.NET Core is tricky.

What is AddHttpClient?

AddHttpClient(IServiceCollection) Adds the IHttpClientFactory and related services to the IServiceCollection. AddHttpClient(IServiceCollection, String) Adds the IHttpClientFactory and related services to the IServiceCollection and configures a named HttpClient.


1 Answers

If you know that anything that's a movie is always going to also be a product, you could define your interfaces like this, where IMovie extends IProduct:

public interface IProduct
{
    decimal Price { get; set; }
    decimal WeightInKg { get; set; }
    int Stock { get; set; }
}

public interface IMovie : IProduct
{
    int Certification { get; set; }
    int RunningTime { get; set; }
}

Then your DVD class just implements the IMovie interface:

public class DVD : IMovie
{
    public decimal Price { get; set; }
    public decimal WeightInKg { get; set; }
    public int Stock { get; set; }
    public int Certification { get; set; }
    public int RunningTime { get; set; }
}

Using your other example, perhaps your TShirt implements an IClothing interface, which is also a product:

public class IClothing : IProduct
{
    int Size { get; set; }
    Color Color { get; set; }
}

public class TShirt : IClothing
{
    public decimal Price { get; set; }
    public decimal WeightInKg { get; set; }
    public int Stock { get; set; }
    public int Size { get; set; }
    public Color Color { get; set; }
}

Now when you inject your dependencies, you can ask for an instance of IMovie or IClothing.

like image 176
Grant Winney Avatar answered Oct 18 '22 01:10

Grant Winney