【发布时间】:2014-01-11 10:19:42
【问题描述】:
我有一个具有以下业务规则的项目分配域
- 当新员工被分配到一个项目时,总支出不应超过预算金额。
- 对于员工,总分配百分比不应超过 100%
我在C# 中创建了如下所示的实体。
问题
Allocate 逻辑分为两个类 - Project 和 Employee..List<Allocation> 作为参数传递给 Allocate 方法,而不是作为类的属性添加......这是正确的方法还是我做需要在这两个类中添加List<Allocation>作为属性吗?
注意:
数据库
授权
代码
项目
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;
}
}
参考文献
以下来自Repository Pattern without an ORM
有什么行为需要客户有订单列表?当您更多地考虑您的域的行为(即在什么时候需要什么数据)时,您可以根据用例对聚合进行建模,并且事情变得更加清晰和容易,因为您只需跟踪一小部分对象的更改在聚合边界。
我怀疑 Customer 应该是没有订单列表的单独聚合,而 Order 应该是带有订单行列表的聚合。如果您需要对客户的每个订单执行操作,请使用 orderRepository.GetOrdersForCustomer(customerID);进行更改,然后使用 orderRespository.Save(order);
【问题讨论】:
标签: oop design-patterns domain-driven-design ooad grasp