Multiple Interface Dependency Injection

I just study the principle of separation of segregation. But after training, I got confused with the script in this example.

The concept divides interfaces into simple interfaces. This is good, but my question is hierarchy model or not?

Take the example that I studied in the book.

I have one interface for a 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 will simply simplify with a single class implementation 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 applying to other categories that do not have related properties. When you create a class for TShirt, there is no need for certification and RunningTime. Thus, in accordance with the principle of interface separability, the interface is partitioned as shown below.

Create a new interface, move the movie-related properties as shown below

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

So, IProduct does not have these properties and implementations, as shown 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; } } 

Finally, I'm fine with that. But if it is about implementing a real method like that. When I use dependency injection, which interface I used as the type for the DVD class.

Am I embarrassed or is something missing? If I apply the inheritance logic, we can use the lower level interface, so the base interface is also inherited. But if I use it like that, how to implement it?

+5
source share
1 answer

If you know that everything that a movie will always be a product, you can 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 simply 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 another example, perhaps your TShirt implements the 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 that you enter your dependencies, you can request an instance of IMovie or IClothing .

+4
source

Source: https://habr.com/ru/post/1200173/


All Articles