My question relates to this question , but a little more specifically.
I have a Customer domain object that looks like this:
public class Customer : Party { public Identity Identity {get; protected set;} public bool IsOrganization {get; set;} }
and Identity is as follows:
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); }
Now, based on the value of IsOrganization, you need to change some logic in Identity, especially if IsOrganization is true. Separate related fields (first name, last name, etc.) must return null when it is false. Organization fields must return null.
Previously, this was done using various client implementations that would initialize the identification for another base class in their constructors, however, the change I'm working on requires removing the separation of the classes of the two client types.
I thought the Identity property looked something like this:
public override Identity Identity { get { if (IsOrganization) { return OrgnaizationIdentity.FromIdentity(base.Identity); } else { return IndividualIdentity.FromIdentity(base.Identity); } } }
and the From Identity method looks like this:
public static OrgnaizationIdentity FromIdentity(Identity identity) { return new OrgnaizationIdentity { FirstName = identity.FirstName, LastName = identity.LastName, MiddleNameInitial = identity.MiddleNameInitial, Title = identity.Title }; }
The problem is that the original identification object has some private fields that must also be returned.
So my question is: is there an accepted way to do something like this?