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使用ASP。 NET EF a. 核心,并在我的财产组合中这样做:
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.
我写了一名指挥手,希望更新<代码>Customer。
如果我希望更新<代码>ContactEmailAddress或,ContactPhoneNumber
或甚至BusinessName
或IndividualName
,因为该物体是适当的类型。
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.
我认为,正是在<条码>/条码>和(或)<条码>中采用了“虚拟条码>方法,但可以尽可能使用少量援助。