davioooh
davioooh

Reputation: 24706

How to design a more flexible structure for my service layer

Talking about Interface Segregation Principle I was evaluating the design of an interface that I often use in my service layer:

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'd like to make this structure more flexible.

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

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

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

// etc.

And then just implement desired methods in my concrete class:

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

But is it good design?

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

Upvotes: 3

Views: 119

Answers (1)

usr
usr

Reputation: 171206

It appears you are deriving from an abstract class to inherit default implementations. Don't misuse inheritance for code reuse. Inheritance is for creating substitutability. Create yourself a few helper methods. That way you can create just the methods you want to expose.

Upvotes: 2

Related Questions