【问题标题】:WebAPI- How to implement layers?WebAPI-如何实现层?
【发布时间】:2014-06-20 09:11:17
【问题描述】:

这是我在Web API 中的简单customer 控制器。

public class CustomerController : ApiController
{
    BusinessLayer.ICustomerRepositary CustomerRepositary;

    public CustomerDTO Get()
    {
        BusinessLayer.Customer customer = CustomerRepositary.GetCustomers();

        // Map customer to customerDTO

        return customerDTO;
    }
}

我的项目组织:

BusinessLayer --> 模型 (Customer.cs)、CustomerRepository、ICustomerRepository

APILayer --> DTO (CustomerDTO.cs)、CustomerController

我的问题是:

  1. tightly coupling the business layer with the API 层是个好主意吗?

  2. 这里还有其他的模式要实现吗?

  3. 当我实现单元测试时,单元测试包含业务层的引用。这个设计正确吗?

【问题讨论】:

    标签: c# .net asp.net-mvc design-patterns asp.net-web-api


    【解决方案1】:

    与软件中的大多数事情一样,这取决于您要实现的目标。

    但是,通常,在较高层创建的类可以依赖于较低层的类。但反之则不然。

    所以,我认为您的代码前进的方向没有任何问题。

    CustomerController 编写测试时,您将引用BusinessLayer 类。但是,在为 BusinessLayer 类编写测试时,您不会期望引用 WebAPIApplication 类。

    【讨论】:

    • 再次,这取决于您是否正在转向 3 层架构或更多 Onion architecture。一开始,我认为将存储库放在业务层中很好。如果您有多个 repo 实现,您可能只想将接口保留在总线层(将来)。
    • 简单而伟大的解释。谢谢:)
    猜你喜欢
    • 1970-01-01
    • 2021-07-26
    • 2013-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多