views:

25

answers:

0

I have two entities that I need to treat polymorhically, each of which has a similar "business id" type of property. As you might expect, there is semantic meaning to each id in the domain, and there is an object type to represent it. In one entity, a Project, the domain language of this property is a ProjectCode. In the other entity, an Account, the domain language of the id is an AccountId. While both can be stored as a string, each must be unique, so I came up with the mapping below. The problem is that the BusinessId property in the base class is not getting hydrated.

I'm not mapping the BusinessId property in the base class, so I guess this isn't a surprising result. It seems like one option would be to try and map the BusinessId to both the ProjectCode and AccountId since they both can be stored as a string, although having the db enforce uniqueness is complicated if not impossible. Another option I can think of is to fix my object model somehow. And the last option I can think of is to carry a duplicate of the string value mapped in the BusinessId column.

How should I fix my mapping / object model?

Cheers,
Berryl

Mapping:

<id name="Id" type="System.Int32" unsaved-value="0">
  <column name="ActivitySubjectId" />
  <generator class="hilo" />
</id>

<discriminator column="ActivitySubjectType" type="System.String" />

<property name="Description" type="System.String">
  <column name="Description" length="75" not-null="true" />
</property>

<subclass name="Account" discriminator-value="ACCOUNT">

  <property name="AccountId" type="AccountIdUserType">
    <column name="AccountId" length="20" unique="true" unique-key="AccountDomainSignature" index="AccountDomainSignature" />
  </property>

</subclass>

<subclass name="Project" discriminator-value="PROJECT">

  <property name="ProjectCode"
            type="ProjectCodeUserType>
    <column name="ProjectCode" length="10" unique="true" unique-key="ProjectDomainSignature" index="ProjectDomainSignature"/>
  </property>

</subclass>

Here is the object model:

public class ActivitySubject : Entity, IActivitySubject
{
    public virtual string BusinessId { get; private set; }
    public virtual string Description { get; private set; }

    protected ActivitySubject(string businessId, string description) { _Initialize(businessId, description); }

    [UsedImplicitly]
    public ActivitySubject() { } 

    protected void _Initialize(string businessId, string description) {
        ...
        BusinessId = businessId;
        Description = description;
    }
}

public class Project : ActivitySubject
{
    public virtual ProjectCode ProjectCode { get; private set; }

    public Project(ProjectCode code, string description) {
        Check.RequireNotNull(code);
        ProjectCode = code;
         _Initialize(code, description);
    }

    [UsedImplicitly]
    protected Project() { }   // persistence tools required ctor
}

public class Account: ActivitySubject
{
    public virtual IdScheme IdScheme { get; private set; }

    public Account(ProjectCode code, string description) {
        IdScheme = code;
         _Initialize(code, description);
    }

    [UsedImplicitly]
    protected Account() { }   // persistence tools required ctor
}

** EDIT **

I modified my object model to set the business id through the setters in the subclasses:

    public virtual ProjectCode ProjectCode {
        get { return _projectCode; } 
        private set {
            ...
            _projectCode = value;
            base.BusinessId = _projectCode.ToString();
        }
    }
    private ProjectCode _projectCode;

This seems works and seems reasonable considering the uniqueness requirement.