更新「ドメイン オブジェクト」の使用を推奨するのではなく、メッセージング ベースのドメイン モデルの使用を推奨します。例については、こちらを参照してください。
#1 に対する答えは、場合によるということです。どのエンタープライズ アプリケーションでも、ドメイン内に次の 2 つの主要なカテゴリがあります。
ストレートCRUD
オブジェクトの次の状態はオブジェクトの前の状態に依存しないため、ここではドメイン オブジェクトは必要ありません。それはすべてデータであり、動作ではありません。この場合、どこでも同じクラス (つまり、EF POCO) を使用してもかまいません: 編集、永続化、表示。
この例は、注文に請求先住所を保存することです。
public class BillingAddress {
public Guid OrderId;
public string StreetLine1;
// etc.
}
一方、私たちは...
ステートマシン
ドメインの動作と状態の永続性(および作業を行うためのリポジトリ) には、個別のオブジェクトが必要です。ドメイン オブジェクトのパブリック インターフェイスは、ほとんどの場合、すべてが void メソッドであり、パブリック ゲッターはありません。この例は、注文ステータスです。
public class Order { // this is the domain object
private Guid _id;
private Status _status;
// note the behavior here - we throw an exception if it's not a valid state transition
public void Cancel() {
if (_status == Status.Shipped)
throw new InvalidOperationException("Can't cancel order after shipping.")
_status = Status.Cancelled;
}
// etc...
}
public class Data.Order { // this is the persistence (EF) class
public Guid Id;
public Status Status;
}
public interface IOrderRepository {
// The implementation of this will:
// 1. Load the EF class if it exists or new it up with the ID if it doesn't
// 2. Map the domain class to the EF class
// 3. Save the EF class to the DbContext.
void Save(Order order);
}
#2 に対する答えは、DbContext が EF クラスへの変更を自動的に追跡することです。