English 中文(简体)
C# - 更新来自基类的儿童班级
原标题: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使用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或甚至BusinessNameIndividualName,因为该物体是适当的类型。

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.

我认为,正是在<条码>/条码>和(或)<条码>中采用了“虚拟方法,但可以尽可能使用少量援助。

问题回答

为了直接回答你的问题,你可以这样做。 或者,我认为,这种情况应当问这里的目标是什么。

EF的确使你能够分享这一地图,尽管这似乎并不像你为了你的情况而重新获得最好的里程。 鉴于这一点,我认为,如果你在北美贸总会的表格中分享了信息,然后与其他拉美竞争和消费者保护局一样,你也许应该转向一个多层次的解决办法。 CUSTOMER或INDIVIDUAL_CUSTOMER. 当你想要完成转换时,你就可以简单地放弃表中的数值,并加入另一个表格。

从技术上来说,这将使某个特定北美贸总会能够进入每个类别,但只要有良好的商业规则,就可以轻易加以缓解。

如前所述,这是一个不同的解决办法,使你能够达到同样的目的。 回答你的问题的直接答案是,你不能再做什么。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签