次のビジネス ルールを持つプロジェクト割り当てドメインがあります。
- 新しい従業員がプロジェクトに割り当てられるとき、総支出は予算額を超えてはなりません。
- 従業員の場合、割り当て率の合計は 100% を超えてはなりません
で作成した以下のエンティティを作成しましたC#
。
質問
ロジックは、Allocate
Project と Employee の 2 つのクラスに分割さList<Allocation>
れます。クラスのプロパティとして追加するのではなく、パラメーターとして Allocate メソッドに渡されます...正しいアプローチですかList<Allocation>
、これら 2 つのクラスにプロパティとして追加する必要がありますか? ?
ノート:
データベース
資格
コード
計画
public class Project
{
public int ProjectID { get; set; }
public int BudgetAmount { get; set; }
public string ProjectName { get; set; }
public void Allocate(Role newRole, int newPercentage, Employee newEmployee, List<Allocation> existingAllocationsInProject)
{
int currentTotalExpenditure = 0;
if (existingAllocationsInProject != null)
{
foreach (Allocation alloc in existingAllocationsInProject)
{
int allocationExpenditure = alloc.Role.BillRate * alloc.PercentageAllocation / 100;
currentTotalExpenditure = currentTotalExpenditure + allocationExpenditure;
}
}
int newAllocationExpenditure = newRole.BillRate * newPercentage / 100;
if (currentTotalExpenditure + newAllocationExpenditure <= BudgetAmount)
{
List<Allocation> existingAllocationsOfEmployee = GetAllocationsForEmployee(newEmployee.EmployeeID);
bool isValidAllocation= newEmployee.Allocate(newRole, newPercentage, existingAllocationsOfEmployee);
if (isValidAllocation)
{
//Do allocation
}
else
{
throw new Exception("Employee is not avaiable for allocation");
}
}
else
{
throw new Exception("Budget Exceeded");
}
}
}
従業員
public class Employee
{
public int EmployeeID { get; set; }
public string EmployeeName { get; set; }
public bool Allocate(Role newRole, int newPercentage, List<Allocation> existingAllocationsOfEmployee)
{
int currentTotalAllocation = 0;
if (existingAllocationsOfEmployee != null)
{
foreach (Allocation alloc in existingAllocationsOfEmployee)
{
currentTotalAllocation = currentTotalAllocation + alloc.PercentageAllocation;
}
}
if (currentTotalAllocation + newPercentage <= 100)
{
return true;
}
return false;
}
}
参考文献
顧客が注文のリストを持っている必要があるのはどのような行動ですか? ドメインの動作 (つまり、どの時点でどのデータが必要か) をさらに検討すると、ユース ケースに基づいて集計をモデル化でき、オブジェクトの小さなセットの変更追跡のみを行うため、物事がより明確になり、はるかに簡単になります。集約境界で。
Customer は注文のリストを含まない別の集計であり、 Order は注文明細のリストを含む集計である必要があると思います。顧客の注文ごとに操作を実行する必要がある場合は、 orderRepository.GetOrdersForCustomer(customerID); を使用します。変更を加えてから orderRespository.Save(order); を使用します。