为分层数据设计域模型(在DDD中)

时间:2017-06-16 15:00:46

标签: c# domain-driven-design domain-model

我已经看过几个建议"要小心"在域模型中继承。在我的情况下,我翻转了继承方法。我想知道它是否可以优化(也许使用合成?)。我们假设我有两种类型的InterfaceConfiguration:

  • VistaLink:出站界面。必须指定EndPoint(另一个实体),即传出消息的位置。
  • HL7:可以入站(不需要EndPoint)或出站(需要EndPoint和FacilityID)

我想出了类似的东西(为简单起见省略了大多数属性和验证):

//Shared properties are in the base class
public class InterfaceConfiguration: IEntity<string>
{
   public InterfaceConfiguration(string id, string displayName, bool isActive)
   {
      Id = id;
      DisplayName = displayName;
      IsActive = isActive;
   }
   public string Id { get; protected set; }
   public string DisplayName { get; protected set; }
   public bool IsActive { get; protected set; }
}

//Inbound Hl7 interface with its properties
public class Hl7InterfaceConfiguration : InterfaceConfiguration
{
   public Hl7InterfaceConfiguration(string id, string displayName, 
      bool isActive, string fieldSeparator, char[] encodingCharacters, ...):  
    base(id, displayName, isActive)
   {
       FieldSeparator = fieldSeparator;
       EncodingCharacters = encodingCharacters;
       ProcessingId = processingId;
       VersionId = versionId;
   }
   public string FieldSeparator { get; protected set; }
   public char[] EncodingCharacters { get; protected set; }
   .....
}

public class Hl7OutboundInterfaceConfiguration : Hl7InterfaceConfiguration
{
   public Hl7OutboundInterfaceConfiguration(string id, string displayName, bool isActive, string fieldSeparator, char[] encodingCharacters, ..., IntrefaceEndPoint endPoint, string facilityId)
    : base(..........)
   {
       if (endPoint == null)
           throw new ArgumentException(nameof(endPoint));
       if (endPoint.AddressType != EndPointAddressTypeEnum.DnsHostName && endPoint.AddressType != EndPointAddressTypeEnum.IPAddress)
           throw new ArgumentException("Address type must be DnsHostName or IPAddress", nameof(endPoint));

       if (facilityId == null)
       {
           throw new ArgumentException("Facility ID is required for HL7 outbound interface", nameof(facilityId));
       }
   }
   public void AssignEndPoint(IntrefaceEndPoint endPoint)
   { .... }
   public IntrefaceEndPoint EndPoint { get; protected set; }
   public string FacilityId { get; protected set; }
}

public class VistaLinkInterfaceConfiguration : InterfaceConfiguration
{
   public VistaLinkInterfaceConfiguration(string id, string displayName, bool isActive, IntrefaceEndPoint endPoint)
    : base(id, displayName, isActive)
   {
       if (endPoint == null)
           throw new ArgumentException(nameof(endPoint));
       if (endPoint.AddressType != EndPointAddressTypeEnum.DnsHostName && endPoint.AddressType != EndPointAddressTypeEnum.IPAddress)
           throw new ArgumentException("Address type muct be DnsHostName or IPAddress", nameof(endPoint));
   }

   public IntrefaceEndPoint EndPoint { get; protected set; }
   public void AssignEndPoint(IntrefaceEndPoint endPoint)
   { .... }
}

我试图让它变得更加平坦&#34;但不确定如何。例如, EndPoint FacilityID Hl7OutboundInterfaceConfiguration 的属性)不适用于 Hl7InterfaceConfiguration (其中仅限入境)。那么这会使它们处于未分配状态,并可能混淆外部功能?我也试图遵循&#34;始终有效&#34;做法。感谢。

0 个答案:

没有答案