私は EF Code First と遅延読み込みを使用します。
私の問題は、孫コレクション内のエンティティを効率的に更新する方法に関連しています。まず第一に、これにより、実際には必要のないデータベースで多くの呼び出しが行われるのではないかと心配しています。しかし、私のドメイン クラスが持続性を気にしないのであれば、これを行う別の方法が思い浮かびません。
クラスは次のとおりです。
public class Supplier
{
public int Id {get;set;}
//...Supplier properties
public virtual ICollection<Contract> Contracts {get;set;}
//supplier methods
}
public class Contract
{
public int id {get;set;}
public int SupplierId{get;set;}
//---Contract properties
[ForeignKey("SupplierId")]
public virtual Supplier Supplier {get;set;}
public virtual ICollection<DeliveryContract> DeliveryContracts {get;set;}
}
public class DeliveryContract
{
public int Id {get;set;}
public bool DeliveryOnMonday{get;set;}
public bool DeliveryOnTuesday{get;set}
//...30 different Delivery terms properties
public Department Department {get;set;}
public int ContractId {get;set;}
[ForeignKey("ContractId")
public virtual Contract Contract {get;set;}
}
サプライヤーは集約ルートです。だから私は ChangeDeliveryContract であるサプライヤーにメソッドを持っています、そしてそれは現実の世界で起こることに対応しています。
public class Supplier
{
//properties
public void ChangeDeliveryContract (DeliveryContract cahangedDc)
{
//So from the supplier i have to find the contract to change
var dcToUpdate = Contracts
.SingleOrDefault(c=>c.Id == changedDc.ContractId)
.SingleOrDefalut(dc=>dc.Id == changedDc.Id);
//So... what do i do now? Map all 30 properties from changedDc to DcToUpdate
//Some business rules is also applied here i.e. there can only be one
// DeliveryContract between Supplier and Department
}
}
私は MVC を使用しているので、プログラムは次のようになります。
{
var Supplier = supplierRepository.GetById(supplierid);
supplier ChangeDeliveryContract (changedDc);
supplierRepository.Save();
//More code...
}
まず第一に、問題は ChangeDeliveryContract にあります。私はこれを機能させることができませんでした。また、私のようにコレクションを照会するのは非効率的かもしれません。第 3 に、30 以上のプロパティをマッピングするのも少し間違っているように感じます。
皆さんはどのようにそれを行いますか、そしてここにベストプラクティスはありますか?