fastendpoint+maomi的一种实现原理

1 整个框架就是fastendpoint(api处理,鉴权授权,参数校验,对象映射等基础功能集成),maoni(Service注入,依赖关系处理,参考的是abp,比较轻量级,源码我放在附件里了,实现模块化注入)

fastendpoint:https://fast-endpoints.com/

maomi: https://maomi.whuanle.cn/1.module.html

2 项目截图:

 program.cs中

using FastEndpoints;
using FastEndpoints.Security;
using Maomi;
using MassTransit;
using NginxTest;
using NginxTest.HostServices;
using System.Reflection;

var bld = WebApplication.CreateBuilder();
bld.Services
   .AddAuthenticationJwtBearer(s => s.SigningKey = "PBKDF2WithHmacSHA51232wewrfwerwretrewe")  
   .AddAuthorization() 
   .AddFastEndpoints();

// 注册模块化服务,并设置 ApiModule 为入口,整个原理就是通过特性-》反射不断循环处理。
// 任何一个框架好像都是这样。
bld.Services.AddModule<ApiModule>();

var app = bld.Build();
app.UseAuthentication() 
   .UseAuthorization() 
   .UseFastEndpoints();
app.Run();

主模块ApiModule

using Maomi.Module;
using NginxTest_Service;

namespace NginxTest
{
    // 这里增加需要注入的其他模块
    [InjectModule<ServiceModule>]
    public class ApiModule : IModule
    {
        private readonly IConfiguration _configuration;
        public ApiModule(IConfiguration configuration)
        {
            _configuration = configuration;
        }

        public void ConfigureServices(ServiceContext context)
        {
            var configuration = context.Configuration;
            context.Services.AddCors();
        }
    }
}
ServiceModule如下:
using Maomi.Module;
using Microsoft.Extensions.Configuration;

namespace NginxTest_Service
{
    public class ServiceModule : IModule
    {
        // 模块类中可以使用依赖注入 必须的
        private readonly IConfiguration _configuration;

        public ServiceModule(IConfiguration configuration)
        {
            _configuration = configuration;
        }
        public void ConfigureServices(ServiceContext context)
        {
            //throw new NotImplementedException();
        }
    }
}

然后在需要注入的实现类加上特性如下:

namespace NginxTest_Service.Services
{
    [InjectOn(lifetime:ServiceLifetime.Scoped, Own = true)]
    public class Userservice : IUserService
    {
        public async Task SayHello()
        {
            await Console.Out.WriteLineAsync("你好,maomi!");
        }
    }
}

 

posted @ 2024-03-22 11:06  vba是最好的语言  阅读(261)  评论(0编辑  收藏  举报