How to create a more flexible structure for my service level

Talking about the principle of dividing segments, I evaluated the interface design that I often use in my service:

public interface ICrudService<D>
{
    IList<D> GetAll();

    bool Exists(int id);

    D GetById(int id);

    D NewInstance();

    D Create(D dto);

    D Update(D dto);

    void Delete(int id);
}

      

I usually use an abstract implementation of this interface and inherit my concrete services from the abstract class.

But obviously I don't always need all of these methods in my service classes, so I would like to make this structure more flexible.

An option could be to split my interface (and my abstract class) like this:

public interface IGetAllService<D>
{
    IList<D> GetAll();
}

public interface IExistsService<D>
{
    bool Exists(int id);
}

// etc.

      

And then just implement the methods you want in my concrete class:

public class ConcreteService : IGetAllService<ConcreteEntity>, IExistsService<ConcreteEntity>
{
    // implemented methods
}

      

But is this good design?

Is there a better way to make my application structure more flexible and reusable?

+3


source to share


1 answer


It looks like you get from an abstract class to inherit the default implementations. Don't overuse inheritance for code reuse. Inheritance is meant to create interchangeability. Create yourself some helper methods. This way, you can only create methods that you want to expose.



+2


source







All Articles