vipwan/Biwen.QuickApi

提供模块化装载功能

vipwan opened this issue · 1 comments

提供模块化功能,方便业务模块化开发, 只需要继承ModularBase即可:

public abstract class ModularBase : IStartup
{
    public virtual int Order { get; } = 0;
    public virtual Func<bool> IsEnable => () => true;
    public virtual void ConfigureServices(IServiceCollection services)
    {
    }
    public virtual void Configure(IApplicationBuilder app, IEndpointRouteBuilder routes, IServiceProvider serviceProvider)
    {
    }
}

  /// <summary>
  /// 前置模块
  /// </summary>
  [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
  public class PreModularAttribute<T> : Attribute
      where T : ModularBase
  {
  }
  [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
  public class PreModularAttribute<T, T2> : Attribute
      where T : ModularBase
      where T2 : ModularBase
  {
  }
  [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
  public class PreModularAttribute<T, T2, T3> : Attribute
      where T : ModularBase
      where T2 : ModularBase
      where T3 : ModularBase
  {
  }
  [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
  public class PreModularAttribute<T, T2, T3, T4> : Attribute
      where T : ModularBase
      where T2 : ModularBase
      where T3 : ModularBase
      where T4 : ModularBase
  {
  }
  [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
  public class PreModularAttribute<T, T2, T3, T4, T5> : Attribute
      where T : ModularBase
      where T2 : ModularBase
      where T3 : ModularBase
      where T4 : ModularBase
      where T5 : ModularBase
  {
  }
 // T7~

用例:

/// <summary>
/// 前置的模块
/// </summary>
public class PreModular1 : ModularBase
{
    public override void ConfigureServices(IServiceCollection services)
    {
        // Add ScheduleTaskStore
        services.AddScheduleMetadataStore<DemoStore>();
    }
    public override void Configure(IApplicationBuilder app, IEndpointRouteBuilder routes, IServiceProvider serviceProvider)
    {
        base.Configure(app, routes, serviceProvider);
    }
}

/// <summary>
/// Demo模块
/// </summary>
/// <param name="environment"></param>
[PreModular<PreModular1>]
public class DemoModular(IHostEnvironment environment) : ModularBase
{
    /// <summary>
    /// 测试模块仅用于开发测试
    /// </summary>
    public override Func<bool> IsEnable => () => environment.IsDevelopment();

    public override void ConfigureServices(IServiceCollection services)
    {
        // Add services to the container.
        services.AddScoped<HelloService>();
        // keyed services
        //builder.Services.AddKeyedScoped<HelloService>("hello");
    }

    public override void Configure(IApplicationBuilder app, IEndpointRouteBuilder routes, IServiceProvider serviceProvider)
    {
        routes.MapGroup("root", x =>
        {
            x.MapGet("/binder", (HttpContext context, BindRequest request) =>
            {
                //测试默认绑定器
                return Results.Content(request.Hello);
            });
            x.MapComponent<HelloWorld>("/razor/{key}",
                context =>
                {
                    return new { Key = context.Request.RouteValues["key"] };
                });
        });


        //提供IQuickEndpoint支持:
        routes.MapGroup("endpoints", x =>
        {
            //~/endpoints/hello/hello?key=world
            x.MapMethods<HelloEndpoint>("hello/{hello}");
            x.MapMethods<PostDataEndpoint>("hello/postdata");
        });

        // Identity API {"email" : "vipwan@co.ltd","password" : "*******"}
        // ~/account/register    
        // ~/account/login 

        if (environment.IsDevelopment())
        {
            //当前preview4 BUG因此必须:ExcludeFromDescription()
            routes.MapGroup("account").MapIdentityApi<IdentityUser>().ExcludeFromDescription();
        }
        else
        {
            routes.MapGroup("account").MapIdentityApi<IdentityUser>();
        }
    }
}