Extending User Model with inheritance or association

May 2, 2012 at 2:57 PM

Looking to extend your User Model whereby a User can be associated/inherited by a Client (Model) or Supplier (Model). Not sure about the best approach.

The models are below

public class Client {
  public int ClientId { get; set; }
  ...
}

public class Supplier {
  public int SupplierId { get; set; }
}

Should I inherit from the User Model?

public Client : User {
  ...
}

Or should I use association?

public class User {
  ...
  public virtual Client Client { get; set; }
  public virtual Supplier Supplier { get; set; }
}
public class Client {
  public int UserId { get; set; }
  ...
  public virtual User User { get; set; }
}
Hope you point me in the right direction.

Coordinator
May 2, 2012 at 3:42 PM

It really depends on what inheritance strategy you intend on using, if you wish to use TPH and want to keep your users, clients, suppliers in a single database table, EF will add a delimiter to distinguish them, then just be sure to Create the users using standard memership and just edit the client/supplier extended fields. Also you dont need ClientID, SupplierID, just the properties that extend user.

You should take a look at inheritance strategies on EF (entity framework) blog. And decide which suits you best, there are performance considerations, extensibility considerations, and the plain fact which is easier.

But most definitely inheritance, also id use TPH.