我在尋找幫助,以決定我應該在哪裏寫我的邏輯。我也想編寫單元測試。哪裏應該是我的邏輯服務層或控制器
我得到一個訂單,我必須將此訂單插入到數據庫中。
我的訂貨型號:
public class CustomerView
{
public int id { get; set; }
public string first_name { get; set; }
public string last_name { get; set; }
public string email { get; set; }
public string primary_email_address { get; set; }
public string image { get; set; }
}
public class ProductView
{
public int id { get; set; }
public string name { get; set; }
}
public class LineView
{
public int number { get; set; }
public ProductView product { get; set; }
public int quantity { get; set; }
public double? price_variation { get; set; }
public List<int?> modifiers { get; set; }
public string notes { get; set; }
public double unit_price { get; set; }
public double unit_tax { get; set; }
}
public class MethodView
{
public int id { get; set; }
public string name { get; set; }
}
public class PaymentView
{
public int id { get; set; }
public int number { get; set; }
public MethodView method { get; set; }
public double amount { get; set; }
public double tip { get; set; }
public string created_at { get; set; }
}
public class Order
{
public int id { get; set; }
public string sale_number { get; set; }
public string status { get; set; }
public string notes { get; set; }
public double total { get; set; }
public double paid { get; set; }
public double tips { get; set; }
public int register_id { get; set; }
public int site_id { get; set; }
public List<LineView> lines { get; set; }
public double price_variation { get; set; }
public List<PaymentView> payments { get; set; }
public string callback_uri { get; set; }
// public List<string> @lock { get; set; }
public int staff_member_id { get; set; }
public string placed_at { get; set; }
public string fulfil_at { get; set; }
public string created_at { get; set; }
public string updated_at { get; set; }
public CustomerView Customer { get; set; }
}
從這個模型我要檢查:
- 如果SITEID是數據庫
- 如果不是在數據庫,獲取第三方完整的網站信息API並保存到我的數據庫中
- 如果網站存在,只需獲取詳細信息
- 檢查客戶是否存在我的數據庫。
- 如果NOT EXISTS - 獲得來自第三方API的客戶信息,並保存到我的數據庫
- 如果存在,就從我的DB
- 終於搞定,它保存該訂單
我使用存儲庫模式,EF代碼優先和我的項目中的IoC AutoFac和AutoMapper。我也想寫上述商業邏輯的單元測試。
我的困惑是:
我應該寫上面的邏輯我的控制器 - 例如:我應該將我的上述檢查建立從控制器我的DB模式,簡單地把它傳遞給訂購服務,我會簡單地救我模型
OR
我應該寫在業務層(訂購服務),以上所有的檢查 - 例如 - 從控制器傳遞DTO對象的服務和業務層中的所有其他檢查?
非常感謝,
Ivan - 謝謝你的回覆。我的理解是,即使我的代碼需要ifs和開關。如果不這樣做,我無法正確插入訂單。如果我必須從另一個地方訪問這個邏輯,我將不得不重寫它 - 特別是,如果有另一個地方接收除我的控制器以外的新訂單,我不希望任何人觸摸「插入新訂單」邏輯。我希望這次我更明確地要求我的要求。你現在有什麼想法? – jNet
那麼可能你需要一個OrderManager來從你的控制器和其他地方使用。 您可以向OrderManager注入其依賴關係(ICustomerRepository,ICustomerThirdPartyApi,IOrderRepository等) 並單元測試它。 –