如何从基类的实例创建派生类的实例并包含私有字段? [英] How can I create an instance of a derived class from an instance of a base class and include private fields?

查看:75
本文介绍了如何从基类的实例创建派生类的实例并包含私有字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题与有关,但更具体一些.

My question is kind of related to this question but a bit more specific.

我有一个如下所示的域对象Customer:

I have a domain object Customer that looks like this:

public class Customer : Party
{
    public Identity Identity {get; protected set;}
    public bool IsOrganization {get; set;}
}

身份"如下所示:

public class Identity : PersistableModel
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string MiddleInitial { get; set; }
    public string Title { get; set; }
    public string BusinessName { get; set; }
    public string LegalName { get; set; }
    public bool IsSynchronized { get; private set; }
}

public abstract class PersistableModel : IPersistableModel
{
    public const long UnassignedId = 0;
    public static readonly DateTime MinimumDateTime = new DateTime(1900, 1, 1);

    private readonly List<string> modifiedProperties = new List<string>();
    public virtual ModelState State { get; set; }
    public IEnumerable<string> ModifiedProperties { get { return modifiedProperties; } }
    protected bool HasModifiedProperties { get { return 0 < modifiedProperties.Count; } }
    public bool WasModified(string propertyName)
    {
        return modifiedProperties.Contains(propertyName);
    }
    public void WasModified(string propertyName, bool modified)
    {
        if (modified)
        {
            if (!WasModified(propertyName)) modifiedProperties.Add(propertyName);
        }
        else 
        {
            modifiedProperties.Remove(propertyName);
        }
    }

    public virtual void OnPersisting()
    {
    }

    public abstract void Accept(Breadcrumb breadcrumb, IModelVisitor visitor);
}

现在,基于IsOrganization的值,需要更改Identity中的某些逻辑,特别是如果IsOrganization为true,则与个人相关的字段(名字,姓氏等)需要返回null,如果为false,则返回Organization字段需要返回null.

Now based on the value of IsOrganization some logic within Identity needs to change, specifically if IsOrganization is true the Individual related fields (first name, last name, etc...) need to return null and when it is false the Organization fields need to return null.

以前,这是通过具有不同的客户实现方式来完成的,这些实现会在其构造函数中将身份初始化为不同的基类,但是我正在进行的更改需要消除这两种客户类型的类分隔.

Previously this was done by having different implementations of the customer that would initialize the identity to different base class in their constructors however the change I'm working on needs to remove the class separation of these two customer types.

我当时想让Identity属性看起来像这样:

What I was thinking is for the Identity property to look something like this:

public override Identity Identity
{
    get
    {
         if (IsOrganization)
         {
             return OrgnaizationIdentity.FromIdentity(base.Identity);
         }
         else
         {
             return IndividualIdentity.FromIdentity(base.Identity);
         }
     } 
 }

来自身份"方法如下:

public static OrgnaizationIdentity FromIdentity(Identity identity)
{
    return new OrgnaizationIdentity
    {
        FirstName = identity.FirstName,
        LastName = identity.LastName,
        MiddleNameInitial = identity.MiddleNameInitial,
        Title = identity.Title
    };
}

这里的问题是原始身份对象具有一些私有字段,这些私有字段也需要返回.

The problem here is the original identity object has some private fields that need to be returned as well.

所以我的问题是,有做这种事情的公认方法吗?

So my question is, is there an accepted way of doing something like this?

推荐答案

如果可以添加并使用一个复制构造函数,则复制构造函数可以做到这一点:

A Copy constructor can do this, if you can add and use one:

class Identity
{
    private int x;
    public Identity(Identity that)
    {
        this.x = that.x;
    }
}

class OrgnaizationIdentity : Identity 
{
    public OrgnaizationIdentity(Identity that) : base(that) { ... }
}

这篇关于如何从基类的实例创建派生类的实例并包含私有字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆