Question

Let say I have abstract class called: Tenant and Customer. The tenant in this case is like owner of the application for multi tenant application model

The relationship between these 2 class are Many to One relationship.

public abstract class Tenant
{
  protected Int32 id;
  protected String name;

  public Int32 ID { get; set; }
  public String Name { get; set; }

  public abstract bool Add();
  public abstract bool Update();
  public abstract bool Delete(); 
}

public class ApplicationTenant: Tenant
{
  public ApplicationTenant() { }

  public override Int64 ID
  {
     get { return id; }
     set { id = value; }
  }

  public override String Name
  {
     get { return name; }
     set { name= value; }
  }

  ...

}

public abstract class Customer
{
  protected Int32 id;
  protected String name;
  protected Tenant tenant;

  public Int32 ID { get; set; }
  public String Name { get; set; }
  public Tenant Tenant { get; set; }

  public abstract bool Add();
  public abstract bool Update();
  public abstract bool Delete(); 
}

public class CorporateCustomer : Customer
{
  public CorporateCustomer () { }

  public override Int64 ID
  {
     get { return id; }
     set { id = value; }
  }

  public override String Name
  {
     get { return name; }
     set { name= value; }
  }

  public override Tenant Tenant
  {
     get { return tenant; }
     set { tenant= value; }
  }


  ...

}

The problem with this design is that when you do:

CorporateCustomer customer = new CorporateCustomer();
customer.Tenant.ID = xx

Tenant in this case is always referencing the public Tenant Tenant { get; set; } which is actually what I want is the one from ApplicationTenant NOT the one abstract one.

Should I remove any abstraction in this class Customer if it's relating other objects? What is your thought?

Was it helpful?

Solution

Or use generics in your Customer class.

public abstract class Customer<TTenant> where TTenant: Tenant
{
  protected Int32 id;
  protected String name;
  protected TTenant tenant;

  public Int32 ID { get; set; }
  public String Name { get; set; }
  public TTenant Tenant { get; set; }

  public abstract bool Add();
  public abstract bool Update();
  public abstract bool Delete(); 
}

OTHER TIPS

Do you mean you want to access the concrete implementation ApplicationTenant Properties rather than just getting the Tenant abstract class? If you are certain the Tenant is of type ApplicationTenant, you can just cast it i.e.

ApplicationTenant appTenant = (ApplicationTenant)customer.Tenant;

then use the features of ApplicationTenant.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top