我在寻找帮助,以决定我应该在哪里写我的逻辑。我也想编写单元测试。哪里应该是我的逻辑服务层或控制器
我得到一个订单,我必须将此订单插入到数据库中。
我的订货型号:
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等) 并单元测试它。 –