Search code examples
c#abstract-class

C# - updating child class from base class


I have a situation where I need to differentiate different "types" of Customer objects.

I.e.:

public abstract class Customer
{    
    protected Customer(string customerType, string emailAddress, string phoneNUmber)
    {
        Type = customerType;
        ContactEmail = emailAddress;
        ContactPhone = phoneNUmber;
    }

    public string ContactEmail { get; private set; }

    public string ContactPhone { get; private set; }
}



internal class CorporateCustomer : Customer
{
    public CorporateCustomer(string customerType, string emailAddress, string phoneNumber, string businessName) : base(customerType, emailAddress, phoneNumber)
    {
        BusinessName = businessName;
    }

    public BusinessName BusinessName { get; private set; }
}


internal class IndividualCustomer : Customer
{
    public IndividualCustomer(string customerType, string emailAddress, string phoneNumber, string individualName) : base(customerType, emailAddress, phoneNumber)
    {
        IndividualName = individualName;
    }

    public string IndividualName { get; private set; }
}

I'm using ASP.NET EF Core and have this in my property configuration:

builder
    .HasDiscriminator(x => x.CustomerType)
    .HasValue<CorporateCustomer>("CORPORATE")
    .HasValue<IndividualCustomer>("INDIVIDUAL");

So, that the object comes out of the database properly as either an individual customer or a corporate customer.

I'm writing a command handler that wants to update the Customer object.

No problem if I want to update ContactEmailAddress or ContactPhoneNumber or even BusinessName or IndividualName because the object is of the proper type.

But, if the user wants to change the object from a CorporateCustomer to an IndividualCustomer or vice versa. Fundamentally, what I need to accomplish is to change the CustomerType, blank out the "Name" field that isn't used and populate the one that is and store it.

I believe this is where abstract and/or virtual methods come in, but could use a little assistance if possible.


Solution

  • To directly answer your question, you can't do that. Alternatively I think this is a situation where should ask what the goal is here.

    EF does allow you to have this shared mapping, although it doesn't seem like you're getting the best mileage out of it for your situation. Given the that, I think it might be advisable for you to switch to a multi-table solution where you have the shared info in a CUSTOMER table and then join with the other COMPANY_CUSTOMER or INDIVIDUAL_CUSTOMER tables. When you want to perform the switch you can simply drop the values in the table and add an entry into the other table.

    This would technically make it possible for a given CUSTOMER to have an entry per type, but with good business rules this could be easily mitigated.

    As mentioned before, this is a different solution that gets you to the same end. The direct answer to your question is that you cannot do what you're looking for.