想知道如果我這樣做是正確的?TDD和業務規則 - 網絡API 2.0
業務規則
Customer
可以不在系統中存在W/OProperty
。- 當我們刪除
Property
時,Customer
也被刪除。 - 該只有時間我添加一個
Customer
是當我添加一個Property
。 (見下文)
的POST:JSON數據從客戶端
// http://localhost:3541/api/property
{
City: "Demo City",
Customer: {Id: 0, Name: "New Customer", Email: "[email protected]"},
Id: 0,
Name: "Custom Property Name",
State: "LA",
StreetAddress1: "123 Main St.",
StreetAddress2: "Apt 1",
Type: {Id: 15, Name: "Developer"},
UserId: 174,
Zip: "12345"
}
的控制器
[Route("api/property")]
[HttpPost]
public async Task<int> AddProperty(Property property)
{
return await propertyManager.AddProperty(property);
}
屬性管理器類
class PropertyManager{
PropertyManager(){
...
}
PropertyManager(PropertyRepo propertyRepo){
...
}
public int AddProperty(Property property){
int propId = await _propertyRepo.AddProperty(property);
// associate with customer
AddCustomer(property.Customer, propId); // here is my question (see below)
}
// again, here is my question (see below)
private int AddCustomer(Customer customer, int propId){
int custId = _propertyRepo.AddCustomer(customer, propId);
}
}
類財產
class Property{
public int Id {get; set;}
public string StreetAddress1 {get; set;}
...
public Customer Customer {get; set;}
}
在我的單元測試,我測試的業務邏輯的PropertyManager
類的,看看我是否能正確添加Property
。
因此,Customer
在該過程中創建。這必須在同一時間內完成 - 在同一個POST期間(如上面所描述的JSON)。現在
,我的問題是:
(或者我即將進行的方式是)
我測試的我添加Property
能力的業務邏輯。上述規則我永遠不會添加Customer
不添加第一個Property
。我的單元測試因此是Property
centric。
我斷言可以通過Property
和我的一天測試添加Customer
的能力。
正確?!?
或者你會建議在有一個強大的真實世界的時候重新設計系統來解耦到屬性和客戶這兩個類的耦合,後者不能與前者一起存在。