BFS+倒置遍历完成模块化依赖功能
This commit is contained in:
@@ -2,11 +2,16 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using StartupModules;
|
||||
using Yi.Framework.Autofac.Extensions;
|
||||
using Yi.Framework.Core.Attributes;
|
||||
|
||||
namespace Yi.Framework.Core.AutoMapper
|
||||
{
|
||||
[DependsOn(
|
||||
typeof(YiFrameworkCoreModule)
|
||||
)]
|
||||
public class YiFrameworkCoreAutoMapperModule : IStartupModule
|
||||
{
|
||||
|
||||
public void Configure(IApplicationBuilder app, ConfigureMiddlewareContext context)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ namespace Yi.Framework.Core.Autofac.Extensions
|
||||
switch (autoFacModuleEnum)
|
||||
{
|
||||
case AutoFacModuleEnum.PropertiesAutowiredModule:
|
||||
Console.WriteLine($"意框架添加AutoFac模块:{nameof(PropertiesAutowiredModule)}-属性注入模块");
|
||||
new PropertiesAutowiredModule().Load(builder, assemblies);
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Yi.Framework.Core.Attributes
|
||||
{
|
||||
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
|
||||
public class DependsOnAttribute : Attribute
|
||||
{
|
||||
public Type[] DependedTypes { get; }
|
||||
|
||||
public DependsOnAttribute(params Type[] dependedTypes)
|
||||
{
|
||||
DependedTypes = dependedTypes ?? new Type[0];
|
||||
}
|
||||
|
||||
public virtual Type[] GetDependedTypes()
|
||||
{
|
||||
return DependedTypes;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -10,6 +10,7 @@ using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Yi.Framework.Core.Module;
|
||||
|
||||
namespace Yi.Framework.Core.Extensions
|
||||
{
|
||||
@@ -19,172 +20,17 @@ namespace Yi.Framework.Core.Extensions
|
||||
/// </summary>
|
||||
public static class ModuleExtensions
|
||||
{
|
||||
//
|
||||
// 摘要:
|
||||
// Configures startup modules and automatically discovers StartupModules.IStartupModule's
|
||||
// from the applications entry assembly.
|
||||
//
|
||||
// 参数:
|
||||
// builder:
|
||||
// The Microsoft.AspNetCore.Hosting.IWebHostBuilder instance.
|
||||
//
|
||||
// 返回结果:
|
||||
// The Microsoft.AspNetCore.Hosting.IWebHostBuilder instance.
|
||||
public static IWebHostBuilder UseYiModules(this IWebHostBuilder builder)
|
||||
public static WebApplicationBuilder UseYiModules(this WebApplicationBuilder builder, Type startType)
|
||||
{
|
||||
return builder.UseStartupModules(delegate (StartupModulesOptions options)
|
||||
{
|
||||
options.DiscoverStartupModules();
|
||||
});
|
||||
}
|
||||
|
||||
//
|
||||
// 摘要:
|
||||
// Configures startup modules and automatically discovers StartupModules.IStartupModule's
|
||||
// from the specified assemblies.
|
||||
//
|
||||
// 参数:
|
||||
// builder:
|
||||
// The Microsoft.AspNetCore.Hosting.IWebHostBuilder instance.
|
||||
//
|
||||
// assemblies:
|
||||
// The assemblies to discover startup modules from.
|
||||
//
|
||||
// 返回结果:
|
||||
// The Microsoft.AspNetCore.Hosting.IWebHostBuilder instance.
|
||||
public static IWebHostBuilder UseYiModules(this IWebHostBuilder builder, params Assembly[] assemblies)
|
||||
{
|
||||
Assembly[] assemblies2 = assemblies;
|
||||
var moduleManager = new ModuleManager(startType);
|
||||
moduleManager.Invoker();
|
||||
|
||||
Assembly[] assemblies2 = moduleManager.ToAssemblyArray();
|
||||
return builder.UseStartupModules(delegate (StartupModulesOptions options)
|
||||
{
|
||||
options.DiscoverStartupModules(assemblies2);
|
||||
});
|
||||
}
|
||||
|
||||
//
|
||||
// 摘要:
|
||||
// Configures startup modules with the specified configuration for StartupModules.StartupModulesOptions.
|
||||
//
|
||||
// 参数:
|
||||
// builder:
|
||||
// The Microsoft.AspNetCore.Hosting.IWebHostBuilder instance.
|
||||
//
|
||||
// configure:
|
||||
// A callback to configure StartupModules.StartupModulesOptions.
|
||||
//
|
||||
// 返回结果:
|
||||
// The Microsoft.AspNetCore.Hosting.IWebHostBuilder instance.
|
||||
public static IWebHostBuilder UseYiModules(this IWebHostBuilder builder, Action<StartupModulesOptions> configure)
|
||||
{
|
||||
Action<StartupModulesOptions> configure2 = configure;
|
||||
return builder.ConfigureServices(delegate (WebHostBuilderContext hostContext, IServiceCollection services)
|
||||
{
|
||||
services.AddStartupModules(hostContext.Configuration, hostContext.HostingEnvironment, configure2);
|
||||
});
|
||||
}
|
||||
|
||||
//
|
||||
// 摘要:
|
||||
// Configures startup modules with the specified configuration for StartupModules.StartupModulesOptions.
|
||||
//
|
||||
// 参数:
|
||||
// builder:
|
||||
// The Microsoft.AspNetCore.Builder.WebApplicationBuilder instance.
|
||||
//
|
||||
// 返回结果:
|
||||
// The Microsoft.AspNetCore.Builder.WebApplicationBuilder instance.
|
||||
public static WebApplicationBuilder UseYiModules(this WebApplicationBuilder builder)
|
||||
{
|
||||
return builder.UseStartupModules(delegate (StartupModulesOptions options)
|
||||
{
|
||||
options.DiscoverStartupModules();
|
||||
});
|
||||
}
|
||||
|
||||
//
|
||||
// 摘要:
|
||||
// Configures startup modules with the specified configuration for StartupModules.StartupModulesOptions.
|
||||
//
|
||||
// 参数:
|
||||
// builder:
|
||||
// The Microsoft.AspNetCore.Builder.WebApplicationBuilder instance.
|
||||
//
|
||||
// assemblies:
|
||||
// The assemblies to discover startup modules from.
|
||||
//
|
||||
// 返回结果:
|
||||
// The Microsoft.AspNetCore.Builder.WebApplicationBuilder instance.
|
||||
public static WebApplicationBuilder UseYiModules(this WebApplicationBuilder builder, params Assembly[] assemblies)
|
||||
{
|
||||
Assembly[] assemblies2 = assemblies;
|
||||
return builder.UseStartupModules(delegate (StartupModulesOptions options)
|
||||
{
|
||||
options.DiscoverStartupModules(assemblies2);
|
||||
});
|
||||
}
|
||||
|
||||
//
|
||||
// 摘要:
|
||||
// Configures startup modules with the specified configuration for StartupModules.StartupModulesOptions.
|
||||
//
|
||||
// 参数:
|
||||
// builder:
|
||||
// The Microsoft.AspNetCore.Builder.WebApplicationBuilder instance.
|
||||
//
|
||||
// configure:
|
||||
// A callback to configure StartupModules.StartupModulesOptions.
|
||||
//
|
||||
// 返回结果:
|
||||
// The Microsoft.AspNetCore.Builder.WebApplicationBuilder instance.
|
||||
public static WebApplicationBuilder UseYiModules(this WebApplicationBuilder builder, Action<StartupModulesOptions> configure)
|
||||
{
|
||||
builder.Services.AddStartupModules(builder.Configuration, builder.Environment, configure);
|
||||
return builder;
|
||||
}
|
||||
|
||||
//
|
||||
// 摘要:
|
||||
// Configures startup modules with the specified configuration for StartupModules.StartupModulesOptions.
|
||||
//
|
||||
// 参数:
|
||||
// services:
|
||||
// The service collection to add the StartupModules services to.
|
||||
//
|
||||
// configuration:
|
||||
// The application's configuration.
|
||||
//
|
||||
// environment:
|
||||
// The application's environment information.
|
||||
//
|
||||
// configure:
|
||||
// A callback to configure StartupModules.StartupModulesOptions.
|
||||
public static void AddYiModules(this IServiceCollection services, IConfiguration configuration, IWebHostEnvironment environment, Action<StartupModulesOptions> configure)
|
||||
{
|
||||
if (services == null)
|
||||
{
|
||||
throw new ArgumentNullException("services");
|
||||
}
|
||||
|
||||
if (configuration == null)
|
||||
{
|
||||
throw new ArgumentNullException("configuration");
|
||||
}
|
||||
|
||||
if (environment == null)
|
||||
{
|
||||
throw new ArgumentNullException("environment");
|
||||
}
|
||||
|
||||
StartupModulesOptions startupModulesOptions = new StartupModulesOptions();
|
||||
configure(startupModulesOptions);
|
||||
if (startupModulesOptions.StartupModules.Count != 0 || startupModulesOptions.ApplicationInitializers.Count != 0)
|
||||
{
|
||||
StartupModuleRunner runner = new StartupModuleRunner(startupModulesOptions);
|
||||
services.AddSingleton((Func<IServiceProvider, IStartupFilter>)((IServiceProvider sp) => ActivatorUtilities.CreateInstance<ModulesStartupFilter>(sp, new object[1] { runner })));
|
||||
new ConfigureServicesContext(configuration, environment, startupModulesOptions);
|
||||
runner.ConfigureServices(services, configuration, environment);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Yi.Framework.Core.Attributes;
|
||||
|
||||
namespace Yi.Framework.Core.Module
|
||||
{
|
||||
|
||||
internal class ModuleManager
|
||||
{
|
||||
private List<Type> ResultType = new List<Type>();
|
||||
private Type StartType;
|
||||
public ModuleManager(Type startType)
|
||||
{
|
||||
StartType = startType;
|
||||
}
|
||||
|
||||
public List<Type> Invoker()
|
||||
{
|
||||
StartBFSNodes(StartType);
|
||||
var result= RemoveDuplicate(ResultType);
|
||||
|
||||
foreach (var r in result)
|
||||
{
|
||||
|
||||
Console.WriteLine($"意框架正在加载模块:{r.Name}");
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private Type[]? GetDependsOnType(Type type)
|
||||
{
|
||||
var dependsOnbuild = type.GetCustomAttributes(typeof(DependsOnAttribute), false).FirstOrDefault() as DependsOnAttribute;
|
||||
if (dependsOnbuild is null)
|
||||
{
|
||||
return new Type[0];
|
||||
}
|
||||
return dependsOnbuild.GetDependedTypes();
|
||||
|
||||
}
|
||||
|
||||
private void StartBFSNodes(Type node)
|
||||
{
|
||||
ResultType.Add(node);
|
||||
var nodes = GetDependsOnType(node);
|
||||
if (nodes is not null && nodes.Count() != 0)
|
||||
{
|
||||
foreach (var n in nodes)
|
||||
{
|
||||
StartBFSNodes(n);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private List<Type> RemoveDuplicate(List<Type> array)
|
||||
{
|
||||
HashSet<Type> s = new HashSet<Type>();
|
||||
List<Type> list = new List<Type>();
|
||||
for (int i = array.Count - 1; i >= 0; i--)
|
||||
{
|
||||
if (!s.Contains(array[i]))
|
||||
{
|
||||
s.Add(array[i]);
|
||||
list.Add(array[i]);
|
||||
}
|
||||
}
|
||||
ResultType = list;
|
||||
return list;
|
||||
}
|
||||
|
||||
public List<Assembly> ToAssemblyList()
|
||||
{
|
||||
return ResultType.Select(a => a.Assembly).ToList();
|
||||
}
|
||||
public Assembly[] ToAssemblyArray()
|
||||
{
|
||||
return ResultType.Select(a => a.Assembly).ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,12 +6,6 @@
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Remove="Module\**" />
|
||||
<EmbeddedResource Remove="Module\**" />
|
||||
<None Remove="Module\**" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Options\" />
|
||||
</ItemGroup>
|
||||
|
||||
@@ -6,9 +6,14 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Yi.Framework.Core.Attributes;
|
||||
using Yi.Framework.Core.AutoMapper;
|
||||
|
||||
namespace Yi.Framework.Ddd
|
||||
{
|
||||
[DependsOn(
|
||||
typeof(YiFrameworkCoreAutoMapperModule)
|
||||
)]
|
||||
public class YiFrameworkDddModule:IStartupModule
|
||||
{
|
||||
public void Configure(IApplicationBuilder app, ConfigureMiddlewareContext context)
|
||||
|
||||
@@ -8,9 +8,14 @@ using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Yi.Framework.Caching.MemoryCache;
|
||||
using Yi.Framework.Caching;
|
||||
using Yi.Framework.Core.Attributes;
|
||||
using Yi.Framework.Core;
|
||||
|
||||
namespace Yi.Framework.Ddd
|
||||
{
|
||||
[DependsOn(
|
||||
typeof(YiFrameworkCoreModule)
|
||||
)]
|
||||
public class YiFrameworkCachingMemoryCacheModule:IStartupModule
|
||||
{
|
||||
public void Configure(IApplicationBuilder app, ConfigureMiddlewareContext context)
|
||||
|
||||
@@ -1,11 +1,15 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using StartupModules;
|
||||
using Yi.Framework.Core;
|
||||
using Yi.Framework.Core.Attributes;
|
||||
using Yi.Framework.Core.Configuration;
|
||||
|
||||
namespace Yi.Framework.Caching.Redis
|
||||
{
|
||||
|
||||
[DependsOn(
|
||||
typeof(YiFrameworkCoreModule)
|
||||
)]
|
||||
public class YiFrameworkCachingRedisModule : IStartupModule
|
||||
{
|
||||
public void Configure(IApplicationBuilder app, ConfigureMiddlewareContext context)
|
||||
|
||||
@@ -6,9 +6,14 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Yi.Framework.Core;
|
||||
using Yi.Framework.Core.Attributes;
|
||||
|
||||
namespace Yi.Framework.Office.Excel
|
||||
{
|
||||
[DependsOn(
|
||||
typeof(YiFrameworkCoreModule)
|
||||
)]
|
||||
public class YiFrameworkOfficeExcelModule : IStartupModule
|
||||
{
|
||||
public void Configure(IApplicationBuilder app, ConfigureMiddlewareContext context)
|
||||
|
||||
@@ -8,7 +8,7 @@ using Yi.Framework.WeChatPay.Helper;
|
||||
|
||||
namespace Yi.Framework.WeChatPay.Domain
|
||||
{
|
||||
public interface IPayInvoker
|
||||
public interface IPayManager
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取WX支付链接的方法
|
||||
|
||||
@@ -11,13 +11,13 @@ using Yi.Framework.WeChatPay.Options;
|
||||
|
||||
namespace Yi.Framework.WeChatPay.Domain
|
||||
{
|
||||
public class PayInvoker : IPayInvoker
|
||||
public class PayManager : IPayManager
|
||||
{
|
||||
private readonly PayHelper _payHelper;
|
||||
private readonly ILogger<PayInvoker> _logger;
|
||||
private readonly ILogger<PayManager> _logger;
|
||||
private readonly AbstractNotify _notify;
|
||||
|
||||
public PayInvoker(PayHelper payHelper, ILogger<PayInvoker> logger, AbstractNotify notify)
|
||||
public PayManager(PayHelper payHelper, ILogger<PayManager> logger, AbstractNotify notify)
|
||||
{
|
||||
_payHelper = payHelper;
|
||||
_logger = logger;
|
||||
|
||||
@@ -40,8 +40,8 @@ namespace Yi.Framework.WeChatPay.Extensions
|
||||
throw new PayException("NotifyUrl为空值");
|
||||
}
|
||||
services.AddSingleton(option);
|
||||
services.AddTransient<PayInvoker>();
|
||||
services.AddTransient<IPayInvoker, PayInvoker>();
|
||||
services.AddTransient<PayManager>();
|
||||
services.AddTransient<IPayManager, PayManager>();
|
||||
services.AddTransient<IPayConfig, PayConfig>();
|
||||
services.AddTransient<PayHelper>();
|
||||
services.AddTransient<PayApi>();
|
||||
|
||||
@@ -1,12 +1,17 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using StartupModules;
|
||||
using Yi.Framework.Core;
|
||||
using Yi.Framework.Core.Attributes;
|
||||
using Yi.Framework.Core.Configuration;
|
||||
using Yi.Framework.WeChatPay.Extensions;
|
||||
using Yi.Framework.WeChatPay.Options;
|
||||
|
||||
namespace Yi.Framework.WeChatPay
|
||||
{
|
||||
[DependsOn(
|
||||
typeof(YiFrameworkCoreModule)
|
||||
)]
|
||||
public class YiFrameworkWeChatPayModule : IStartupModule
|
||||
{
|
||||
public void Configure(IApplicationBuilder app, ConfigureMiddlewareContext context)
|
||||
|
||||
Reference in New Issue
Block a user