私はAggregateの設計原則に従おうとしていますが、助けが必要な状況を考え出しました。私の集約ルートはCustomerオブジェクトです。Customerオブジェクトには、オブジェクトの子コレクションとオブジェクトAddressの子コレクションがありContactます。
Aは、集合体の下Contactへの参照を持つことができます。オブジェクトには一意のオブジェクトがあり、オブジェクトにはローカルIDがあるため、データベースの主キーはandになります。AddressCustomerCustomerIDAddressContactCustomerIdAddressId
簡略化されたクラスは次のとおりです。
public class Customer : AggregateRoot {
public virtual int CustomerId { get; protected set; }
public virtual IList<Address> Addresses { get; protected set; }
public virtual IList<Contact> Contacts { get; protected set; }
}
public class Address : Entity {
public Address(Customer customer, int addressId) {
this.Customer = customer;
this.AddressId = addressId;
}
public virtual Customer Customer { get; protected set; }
public virtual int AddressId { get; protected set; }
}
public class Contact : Entity {
public Contact(Customer customer, int contactId) {
this.Customer = customer;
this.ContactId = contactId;
}
public virtual Customer Customer { get; protected set; }
public virtual int ContactId { get; protected set; }
public virtual Address Address { get; set; }
}
データベースには、次のようなテーブルがあります。
お客様
CustomerId int identity PK
住所
CustomerId int not null PK,FK
AddressId int not null PK
コンタクト
CustomerId int not null PK,FK
ContactId int not null PK
AddressId int null FK
私の問題は、FluentNHibernateを使用してエンティティをマッピングしようとしたときに発生します。Addressオブジェクトにはとの複合キーがあるCustomerIdため、AddressIdNHibernateはCustomerId連絡先テーブルの列を再利用しません。集計を保存しようとすると、パラメーターよりも値が多いという例外が発生します。CustomerIdこれは、Addressオブジェクトに複合IDがあり、オブジェクトと列を共有していないために発生しContactます。
これを回避するために私が見ることができる唯一の方法はAddressCustomerId、テーブルに列を追加することですが、現在、とは同じ値であるContactため、重複する列があります。とにかくこの振る舞いの周りにありますか?CustomerIdAddressCustomerId