私はAggregateの設計原則に従おうとしていますが、助けが必要な状況を考え出しました。私の集約ルートはCustomer
オブジェクトです。Customer
オブジェクトには、オブジェクトの子コレクションとオブジェクトAddress
の子コレクションがありContact
ます。
Aは、集合体の下Contact
への参照を持つことができます。オブジェクトには一意のオブジェクトがあり、オブジェクトにはローカルIDがあるため、データベースの主キーはandになります。Address
Customer
Customer
ID
Address
Contact
CustomerId
AddressId
簡略化されたクラスは次のとおりです。
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
ため、AddressId
NHibernateはCustomerId
連絡先テーブルの列を再利用しません。集計を保存しようとすると、パラメーターよりも値が多いという例外が発生します。CustomerId
これは、Addressオブジェクトに複合IDがあり、オブジェクトと列を共有していないために発生しContact
ます。
これを回避するために私が見ることができる唯一の方法はAddressCustomerId
、テーブルに列を追加することですが、現在、とは同じ値であるContact
ため、重複する列があります。とにかくこの振る舞いの周りにありますか?CustomerId
AddressCustomerId