このアプリケーションでは、ビジネス ルールと現在のユーザーのコンテキストに基づいてプロパティの更新を検証する必要があるシナリオがあります。ドメインモデルは現在のユーザーを認識すべきではないと思うので、検証を行う最善の方法を決定しようとしています。通常の承認はドメインとは別のものであり、このシナリオとは異なります。
この検証はどこで行われるべきですか?それを処理するより良い方法はありますか? ドメイン モデルはユーザーについて認識している必要がありますか? 任意のヘルプまたは入力をいただければ幸いです。
簡単な例: 承認された数量の注文があります。特定のユーザー タイプのみが、特定の方向でのみ数量を更新できます。これは、ドメイン集約で検証する正しい方法ですか?
public enum UserType
{
ViewUserType,
RequesterUserType,
SupplierUserType
}
public class Order
{
public int OrderId {get; private set}
public int RequestedQuantity {get; private set}
public int ApprovedQuantity {get; private set}
public void RequestQuantity(int quantity, UserType userType)
{
if (userType == UserType.RequesterUserType)
{
this.RequestedQuantity = quantity;
}
}
// Question: The direction that the approved quantity can change is a business rule
// but directly deals with the context of the user. Should the model know about the user
// or should this validation be pulled out to either the application service, a model extension,
// or maybe a specification?
public void ApproveQuantity(int quantity, UserType userType)
{
if (userType == UserType.RequesterUserType)
{
if (quantity <= this.ApprovedQuantity)
{
// Requester type user can only update if lowering the approved quantity
this.ApprovedQuantity = quantity;
}
}
else if(userType == UserType.SupplierUserType)
{
if (quantity >= this.ApprovedQuantity)
{
// Supplier type user can only update if increasing the approved quantity
this.ApprovedQuantity = quantity;
}
}
}
}