-1

Is it a good pattern to a have a Generic Interface to implement the Concrete classes and use the generic interface to resolve in container.
My concern is does it breaks the Single responsibility principle or is it tightly couples the implementation.

For example

  //Base Generic Interface
  public interface IBaseServiceCrud<T>
    {
    T Get(string key);
    bool Create (T entity);
    bool Delete(T Entity);
    }

   // Implement Concrete Class with Base Interface 
    public class Order : IBaseServiceCrud<Order>
    { }
    public class Product: IBaseServiceCrud<Order>
    { }


   //Or Should we have a interface specific to each service
   public interface IOrder: IBaseServiceCrud<Order>
   {}

 //And then Implement by Concrete Class
  public class Order : IOrder
  {}

In DI container support resolving Generic Interface, but my concern is it a good practice to resolve based on Generic interface.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Hafeez Khan
  • 427
  • 2
  • 6
  • 22

1 Answers1

1

Implementing the repository pattern this way is not uncommon and you probably won't have any problem doing it (see also Repository Pattern Standardization of methods).

As for dependency injection, it doesn't really matter, as long as your dependency injection framework supports it. What you want with dependency injection is to make the dependencies of your classes clear, and using generic interfaces does not go against that.

Phil-R
  • 2,193
  • 18
  • 21