1 Star 3 Fork 2

james li / Cike.AutoApi

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
README.md 2.64 KB
一键复制 编辑 原始数据 按行查看 历史
黎明 提交于 2023-02-08 19:40 . 修复文档名称

Cike.AutoApi

介绍

🔥自动api,让你的代码更加简洁🔥。如果你的控制器层只是像以下这种,转发了业务层的代码,那么自动api将非常适合您的项目。自动api会直接根据你业务层的方法,结合restful规范动态生成控制器。

控制器只做转发,没有做任何事情,形成了大量的冗余代码

public class UserController:ControllerBase
{
    private readonly IUserAppService _userAppService;
    public UserController(IUserAppService userAppService)
    {
        _userAppService=userAppService;
    }

    [HttpGet]
    public async Task<PageResult<List<xxxDto>>> GetListAsync(xxxDto input)
    {
        return await _userAppService.GetListAsync(input);
    }
    [HttpPost]
    public async Task<xxxDto> CreateAsync(xxxDto input)
    {
        return await _userAppService.CreateAsync(input);
    }
    [HttpPost]
    public async Task<xxxDto> UpdateAsync(Guid id,xxxDto input)
    {
        return await _userAppService.UpdateAsync(id,input);
    }
}

软件架构

  • 本项目依赖于.net6

安装教程

dotnet add package Cike.AutoApi

使用说明

  • Program.cs 中添加以下两段代码
//放在AddController或者AddMvc后面
builder.Services.AddAutoApiService(opt =>
{
    //NETServiceTest所在程序集添加进动态api配置
    opt.CreateConventional(typeof(NETServiceTest).Assembly);
});
  1. 业务层代码,只需要继承 IAutoApiService 接口就行
    public class TestService : IAutoApiService
    {
        public async Task<List<string>> CreateAsync(TestCreateUpdateInput input)
        {
            return new List<string>
            {
                $"{input.Code}|{input.Name}"
            };
        }

        public async Task<string> GetListAsync(string keyword)
        {
            return keyword;
        }

        public async Task<List<string>> UpdateAsync(Guid id, TestCreateUpdateInput input)
        {
            return new List<string>
            {
                $"{id}|{input.Code}|{input.Name}"
            };
        }
        
        /// <summary>
        /// 文件流使用,上传的时候正常传到报文体内就行了
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        public async Task<string> ImportAsync(IAutoApiStreamContent file)
        {
            using var fileStream = file.GetStream();


            return file.FileName;
        }
    }
  1. 最终效果

运行效果图

使用本项目的框架

  1. Yi.Framework
C#
1
https://gitee.com/fengwuyan/auto-web-api.git
git@gitee.com:fengwuyan/auto-web-api.git
fengwuyan
auto-web-api
Cike.AutoApi
develop

搜索帮助