mirror of
https://gitee.com/dotnetchina/anno.core.git
synced 2025-12-06 08:48:50 +08:00
微软DI 依赖注入
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
<PropertyGroup>
|
||||
<DeveloperBuildCoreTfms>net7.0</DeveloperBuildCoreTfms>
|
||||
<StandardTfms>netstandard2.0;net45;</StandardTfms>
|
||||
<Version>2.1.0.1</Version>
|
||||
<Version>2.1.0.4</Version>
|
||||
<Authors>Du yanming</Authors>
|
||||
<Company>Du yanming</Company>
|
||||
<PackageIcon>logo.png</PackageIcon>
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
using Anno.EngineData;
|
||||
using Anno.Loader;
|
||||
using SoEasy.Application.Po;
|
||||
using SoEasy.Application.Repositories;
|
||||
using System;
|
||||
|
||||
namespace SoEasy.Application
|
||||
@@ -7,12 +10,14 @@ namespace SoEasy.Application
|
||||
{
|
||||
public void ConfigurationBootstrap()
|
||||
{
|
||||
Console.WriteLine("ConfigurationBootstrap:" + this.GetType().FullName);
|
||||
var userRepository = IocLoader.Resolve<IBaseRepository<UserEntity>>();
|
||||
Console.WriteLine($"ConfigurationBootstrap:{GetType().FullName},{userRepository.GetType().FullName}");
|
||||
}
|
||||
|
||||
public void PreConfigurationBootstrap()
|
||||
{
|
||||
Console.WriteLine("PreConfigurationBootstrap:" + this.GetType().FullName);
|
||||
//var services = IocLoader.GetServiceDescriptors();
|
||||
Console.WriteLine($"PreConfigurationBootstrap:{GetType().FullName}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,8 +41,8 @@ namespace AnnoService
|
||||
* Install-Package Anno.Rpc.ServerGrpc -Version 1.0.1.5 Grpc
|
||||
* 此处为 Thrift
|
||||
*/
|
||||
//var service = IocLoader.GetServiceDescriptors();
|
||||
var autofac = IocLoader.GetAutoFacContainerBuilder();
|
||||
var service = IocLoader.GetServiceDescriptors();
|
||||
//var autofac = IocLoader.GetAutoFacContainerBuilder();
|
||||
#region 自带依赖注入过滤器 true 注入 false 不注入
|
||||
//IocLoader.AddFilter((type) =>
|
||||
// {
|
||||
@@ -55,8 +55,8 @@ namespace AnnoService
|
||||
* IRpcConnector 接口用户可以自己实现也可以使用 Thrift或者Grpc Anno内置的实现
|
||||
* 此处使用的是Thrift的实现
|
||||
*/
|
||||
//service.AddSingleton<IRpcConnector,RpcConnectorImpl>();
|
||||
autofac.RegisterType(typeof(RpcConnectorImpl)).As(typeof(IRpcConnector)).SingleInstance();
|
||||
service.AddSingleton<IRpcConnector, RpcConnectorImpl>();
|
||||
//autofac.RegisterType(typeof(RpcConnectorImpl)).As(typeof(IRpcConnector)).SingleInstance();
|
||||
}
|
||||
, () =>//服务启动后的回调方法
|
||||
{
|
||||
@@ -64,7 +64,7 @@ namespace AnnoService
|
||||
* 服务Api文档写入注册中心
|
||||
*/
|
||||
Bootstrap.ApiDoc();
|
||||
});
|
||||
},IocType.DependencyInjection);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,14 +23,14 @@ namespace Anno.EngineData
|
||||
)
|
||||
{
|
||||
Const.SettingService.InitConfig();
|
||||
PreConfigurationBootstrap();
|
||||
|
||||
DependsOnInit();
|
||||
Loader.IocLoader.RegisterIoc(iocType
|
||||
#if NETSTANDARD
|
||||
, services
|
||||
#endif
|
||||
);
|
||||
iocAction?.Invoke();
|
||||
PreConfigurationBootstrap();
|
||||
Loader.IocLoader.Build();
|
||||
var bootstraps = Loader.IocLoader.Resolve<IEnumerable<IPlugsConfigurationBootstrap>>();
|
||||
if (bootstraps != null)
|
||||
@@ -42,6 +42,7 @@ namespace Anno.EngineData
|
||||
}
|
||||
BuilderRouterInfo();
|
||||
}
|
||||
#region DependsOn+查找依赖项目
|
||||
/// <summary>
|
||||
/// IOC注入之前插件事件
|
||||
/// </summary>
|
||||
@@ -49,15 +50,15 @@ namespace Anno.EngineData
|
||||
{
|
||||
foreach (var svc in Const.Assemblys.Dic)
|
||||
{
|
||||
GetDependedTypesAssemblies(svc.Value);
|
||||
InvokeDependedTypesAssemblies(svc.Value);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 查找依赖
|
||||
/// 调用依赖
|
||||
/// </summary>
|
||||
/// <param name="assembly"></param>
|
||||
/// <returns></returns>
|
||||
static void GetDependedTypesAssemblies(Assembly assembly)
|
||||
static void InvokeDependedTypesAssemblies(Assembly assembly)
|
||||
{
|
||||
List<Assembly> assemblies = new List<Assembly>();
|
||||
var type = assembly.GetTypes().FirstOrDefault(t => typeof(IPlugsConfigurationBootstrap).IsAssignableFrom(t));
|
||||
@@ -69,6 +70,39 @@ namespace Anno.EngineData
|
||||
type.GetMethod("PreConfigurationBootstrap")?.Invoke(obj, null);
|
||||
var dependsOn = type.GetCustomAttributes<DependsOnAttribute>().FirstOrDefault();
|
||||
if (dependsOn != null)
|
||||
{
|
||||
foreach (var module in dependsOn.DependedTypes)
|
||||
{
|
||||
InvokeDependedTypesAssemblies(module.Assembly);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 加载插件依赖
|
||||
/// </summary>
|
||||
private static void DependsOnInit()
|
||||
{
|
||||
foreach (var svc in Const.Assemblys.Dic)
|
||||
{
|
||||
DependsOnAssemblies(svc.Value);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 查找依赖
|
||||
/// </summary>
|
||||
/// <param name="assembly"></param>
|
||||
/// <returns></returns>
|
||||
static void DependsOnAssemblies(Assembly assembly)
|
||||
{
|
||||
List<Assembly> assemblies = new List<Assembly>();
|
||||
var type = assembly.GetTypes().FirstOrDefault(t => typeof(IPlugsConfigurationBootstrap).IsAssignableFrom(t));
|
||||
if (type == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
var dependsOn = type.GetCustomAttributes<DependsOnAttribute>().FirstOrDefault();
|
||||
if (dependsOn != null)
|
||||
{
|
||||
foreach (var module in dependsOn.DependedTypes)
|
||||
{
|
||||
@@ -84,10 +118,11 @@ namespace Anno.EngineData
|
||||
{
|
||||
foreach (var module in assemblies)
|
||||
{
|
||||
GetDependedTypesAssemblies(module);
|
||||
DependsOnAssemblies(module);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
/// <summary>
|
||||
/// 构建路由信息
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user