DDDN.WebApi 1.0.0

dotnet new install DDDN.WebApi::1.0.0                
This package contains a .NET Template Package you can call from the shell/command line.

DDDN.webapi

介绍

三层架构(domain\ data\ controller) 脚手架 : web api 架构。 架构思想参考: https://github.com/cwoodruff/ChinookASPNETCore3APINTier

模板制作参考:

  1. https://docs.microsoft.com/zh-cn/dotnet/core/tutorials/cli-templates-create-template-pack
  2. https://zhuanlan.zhihu.com/p/355842016
repository address

https://gitee.com/wonlayhwan/dddn.webapi

软件架构
  1. Domain ,定义实体类型、方法、接口等;
  2. Data, 数据层的具体实现, 使用 Dapper; 数据层的接口集成到Supervisor中统一调用
  3. Controller, api 层, 数据层上的计算、逻辑处理,权限、详细日志、依赖注入等
命令行备注
  1. dotnet pack (then you can push to nuget server)
  2. nuget push "nupkg'path" "your nuget key" -Source https://api.nuget.org/v3/index.json
install

dotnet new --install DDDN.WebApi::1.0.0

uninstall

dotnet new -u DDDN.WebApi

usage

dotnet new DDDN-WebApi --name=yourSolutionName 说明:name 即是替换DDDN,如 dotnet new DDDN-WebApi --name=Orders, 则创建后的解决方案如下:

  1. Orders.WebApi.sln
  2. Orders.WebApi.Domain (.net standard 2.1)
  3. Orders.WebApi.Data (.net standard 2.1)
  4. Orders.WebApi.Web (.net 5)
  5. Orders.WebAPi.Test (xUnit test)
sample codes
    Route("api/[controller]/[action]")]
    [ApiController]
    public class SampleController : ControllerBase
    {
        private readonly ILogger log;
        private readonly ISupervisor supervisor;

        //cache
        private readonly IDistributedCache cache;
        private readonly RedisOption redisOption;

        public SampleController(
            ILogger log,
            ISupervisor supervisor,
            IDistributedCache cache,
            IOptions<RedisOption> redisOption)
        {
            this.log = log;
            this.supervisor = supervisor;
            this.cache = cache;
            this.redisOption = redisOption.Value;
        }

        /// <summary>
        /// get sample
        /// </summary> 
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetSample([FromQuery] DateTime? date)
        {
            try
            {
                log.Information("用户({@user})do some sample,参数:{@args}", User.Identity.Name, date);
                if (!date.HasValue)
                {
                    date = DateTime.Today;
                }

                var redisKey = $"{Request.Path}_{date.Value.ToString("yyyyMM")}";
                var redisText = await this.cache.GetStringAsync(redisKey);
                if (!string.IsNullOrWhiteSpace(redisText))
                {
                    var data = JsonConvert.DeserializeObject<DataResult<int>>(redisText);

                    log.Information("用户({@user})do some sample,从缓存获取结果:{@result}", User.Identity.Name, data);
                    return Ok(data);
                }
                else
                {
                    var response = await this.supervisor.GetSample();
                    log.Information("用户({@user})do some sample,结果:{@result}", User.Identity.Name, response);

                    //成功才缓存
                    if (response.success)
                    {
                        redisText = JsonConvert.SerializeObject(response, Formatting.Indented);
                        await this.cache.SetStringAsync(redisKey, value: redisText, new DistributedCacheEntryOptions
                        {
                            SlidingExpiration = TimeSpan.FromMinutes(this.redisOption.ExpireAfter)
                        });
                    }

                    return Ok(response);
                } 
            }
            catch (Exception ex)
            {
                log.Error(ex, Request.Path);
                return Ok(ResultFactory.New(ex));
            }
        }
    }
  • .NETStandard 2.1

    • No dependencies.
  • net5.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 2,493 5/25/2021