1

Suppose we have a abstract class EntityBase which is the base class for all our entities e.g.

public abstract class EntityBase {
  public Guid Id {get;set;}
}
public class Customer : EntityBase {
  public string Name {get;set;}
}
public class Orders : EntityBase {
  public string OrderNumber {get;set;}
}

Where the intent is to then be able to create extension method on EntityBase so it can be projected to Domain classes e.g.

public static class ProjectionExtensions {
  public static TProjection ProjectAs<TProjection>(this EntityBase item) where TProjection : class, new() {
    return Mapper.Map<object, TProjection>(item);
  }
}

Does this violate LSP? Is it better to implement an interface IEntity rather than an abstract base class EntityBase?

e.g.

public interface IEntity {
  public Guid Id {get;set;}
}
public class Customer : IEntity {
  public Guid Id {get;set;}
  public string Name {get;set;}
}
public class Orders : IEntity {
  public Guid Id {get;set;}
  public string OrderNumber {get;set;}
}

public static class ProjectionExtensions {
  public static TProjection ProjectAs<TProjection>(this IEntity item) where TProjection : class, new() {
    return Mapper.Map<object, TProjection>(item);
  }
}
1
  • mistakenly declared IEntity as class instead of interface Commented Jan 31, 2016 at 11:10

1 Answer 1

2

Liskov Substitution Principle is one of the simplest OOP design guidelines. It simply states that any child type must be substitutable for its parent without breaking the program.

So for example if you have a method signature like

assignPersonToDepartment(Person p)

and you have a bunch of subtypes of Person, such as Employee, or Manager, or BoardMember, or President or any other subtype, that you can pass any one of those subtypes into the assignPersonToDepartment method and have the program work correctly.

That's all it is: Subtypes are not allowed to break the contracts of their parents.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.