> For the complete documentation index, see [llms.txt](https://monksoul.gitbook.io/hoa/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://monksoul.gitbook.io/hoa/kongzhiqihefuwu.md).

# 七、控制器和服务

## 控制器

简单来说，控制器是一个承上启下的作用，根据用户输入，执行响应行为（动作方法），同时在行为中调用模型的业务逻辑，返回给用户结果（视图）。

![](/files/-M7lEz0sTK0JDpS4VxpK)

在ASP.NET Core 中，控制器有两种实现方式，一种是传统的MVC框架，一种是WEBAPI框架。

## 服务

在编程领域，服务指的是提供一系列功能的类/组件/模块，通常我们会将这些服务归类为两种：**第三方服务和业务服务（程序员编写的业务逻辑代码）。**

## 控制器和服务的关系

通常，在我们实际的开发过程中，控制器担任数据验证、调用**业务代码**及返回视图等中介角色，而这里的**业务代码指的就是服务。**&#x4E5F;就是说，控制器是业务代码调用的第一道关卡。

### 真的仅仅是这样吗？

实际上，在ASP.NET Core应用程序中，微软提供了一个新型的**控制器模型模式，**&#x8FD9;种模式模糊化了控制器和服务的关系，可以**将控制器充当服务使用，也可以将服务充当控制器使用**。😂

## 普通服务

指的就是封装了一系列功能的类，同时定义了该类的锲约。

在​Hoa Framework框架中，服务实现类没有继承 `IAppServiceDependency` 接口和贴了 `[HoaServiceController]` 特性，都属于普通服务。

注意：不管是普通服务还是其他服务，都需要继承（间接）`ITransientDependency`或`IScopeDependency`。

比如：

```csharp
using Hoa.Dependencies;
using System;

namespace Hoa.Application.SomeService
{
    // 没有贴特性
    public class SomeAppService : ISomeAppService, ITransientDependency // 只继承 ITransientDependency
    {
        // 普通服务
        privde readonly ISomeService _service;
        privde readonly ISomeService _service2;
        public SomeAppService(ISomeService service, ISomeService service2)
        {
            _service = service;
            _service2 = service;
        }

        // Other Codes
    }
}
```

## 动态控制器服务

在​Hoa Framework框架中，内置了一套可以将**服务映射到控制器**的引擎，也就是**将服务充当控制器使用，**&#x8FD9;样的动态控制器服务和普通服务最大的区别就是前者会生成该服务的控制器映射类，也就是会生成RESTFul风格的API接口。

动态控制器服务实际上是一种特殊的普通服务，主要区别在于该服务实现类是否继承了 `IAppServiceDependency` 接口和贴了 `[HoaServiceController]` 特性。

比如：

```csharp
using Hoa.Dependencies;
using Hoa.ServiceController.Attributes;
using System;

namespace Hoa.Application.Authorization
{
    [HoaServiceController]  // 贴了特性，动态控制器服务
    public class AuthorizationAppService : IAuthorizationAppService, IAppServiceDependency  // 继承了 IAppServiceDependency
    {
        // 普通服务
        privde readonly ISomeService _service;
        privde readonly ISomeService _service2;
        public AuthorizationAppService(ISomeService service, ISomeService service2)
        {
            _service = service;
            _service2 = service;
        }

        // Other Codes
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://monksoul.gitbook.io/hoa/kongzhiqihefuwu.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
