我有一个情况,我需要区分Customer
个对象的不同"类型".
即:
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; }
}
我正在使用ASP.NET EF Core,并在我的属性配置中包含以下内容:
builder
.HasDiscriminator(x => x.CustomerType)
.HasValue<CorporateCustomer>("CORPORATE")
.HasValue<IndividualCustomer>("INDIVIDUAL");
因此,该对象从数据库中正确地输出为个人客户或公司客户.
我正在编写一个想要更新Customer
对象的命令处理程序.
如果我想更新ContactEmailAddress
或ContactPhoneNumber
,甚至BusinessName
或IndividualName
都没有问题,因为对象的类型是正确的.
但是,如果用户想要将对象从CorporateCustomer
改为IndividualCustomer
或反之亦然.从根本上说,我需要完成的是更改CustomerType
,清空未使用的"name"字段,填充已使用的字段并存储它.
我相信这就是abstract
和/或virtual
种方法的用武之地,但如果可能的话,可能需要一些帮助.