添加特性自动依赖注入
This commit is contained in:
@@ -8,6 +8,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Yi.Framework.Common.Attribute;
|
||||
using Yi.Framework.Common.Helper;
|
||||
using Yi.Framework.Common.Models;
|
||||
using Yi.Framework.Interface;
|
||||
|
||||
@@ -1,42 +0,0 @@
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
using Microsoft.IdentityModel.JsonWebTokens;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
using Yi.Framework.Common.Const;
|
||||
using Yi.Framework.Common.Enum;
|
||||
|
||||
namespace Yi.Framework.WebCore.AttributeExtend
|
||||
{
|
||||
[AttributeUsage(AttributeTargets.Method)]
|
||||
public class LogAttribute : Attribute
|
||||
{
|
||||
/// <summary>
|
||||
/// 操作类型
|
||||
/// </summary>
|
||||
public OperEnum OperType { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 日志标题(模块)
|
||||
/// </summary>
|
||||
public string Title { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否保存请求数据
|
||||
/// </summary>
|
||||
public bool IsSaveRequestData { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// 是否保存返回数据
|
||||
/// </summary>
|
||||
public bool IsSaveResponseData { get; set; } = true;
|
||||
|
||||
public LogAttribute(string title, OperEnum operationType)
|
||||
{
|
||||
this.Title = title;
|
||||
this.OperType = operationType;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
|
||||
using Autofac;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Yi.Framework.Common.Attribute;
|
||||
|
||||
namespace Yi.Framework.WebCore.MiddlewareExtend
|
||||
{
|
||||
public static class AutoIocExtend
|
||||
{
|
||||
private static void RegIoc(ContainerBuilder build, Assembly assembly)
|
||||
{
|
||||
foreach (var type in assembly.GetTypes())
|
||||
{
|
||||
|
||||
var serviceAttribute = type.GetCustomAttribute<AppServiceAttribute>();
|
||||
if (serviceAttribute is not null)
|
||||
{
|
||||
//情况1:使用自定义[AppService(ServiceType = typeof(注册抽象或者接口))],手动去注册,放type即可
|
||||
var serviceType = serviceAttribute.ServiceType;
|
||||
//情况2 自动去找接口,如果存在就是接口,如果不存在就是本身
|
||||
if (serviceType == null)
|
||||
{
|
||||
//获取最靠近的接口
|
||||
var firstInter = type.GetInterfaces().LastOrDefault();
|
||||
if (firstInter is null)
|
||||
{
|
||||
serviceType = type;
|
||||
}
|
||||
else
|
||||
{
|
||||
serviceType = firstInter;
|
||||
}
|
||||
}
|
||||
|
||||
switch (serviceAttribute.ServiceLifetime)
|
||||
{
|
||||
case LifeTime.Singleton:
|
||||
if (type.IsGenericType)
|
||||
{
|
||||
build.RegisterGeneric(type).As(serviceType).SingleInstance();
|
||||
}
|
||||
else
|
||||
{
|
||||
build.RegisterType(type).As(serviceType).SingleInstance();
|
||||
}
|
||||
break;
|
||||
case LifeTime.Scoped:
|
||||
if (type.IsGenericType)
|
||||
{
|
||||
build.RegisterGeneric(type).As(serviceType).InstancePerLifetimeScope();
|
||||
}
|
||||
else
|
||||
{
|
||||
build.RegisterType(type).As(serviceType).InstancePerLifetimeScope();
|
||||
}
|
||||
break;
|
||||
case LifeTime.Transient:
|
||||
if (type.IsGenericType)
|
||||
{
|
||||
build.RegisterGeneric(type).As(serviceType).InstancePerDependency();
|
||||
}
|
||||
else
|
||||
{
|
||||
build.RegisterType(type).As(serviceType).InstancePerDependency();
|
||||
}
|
||||
break;
|
||||
default:
|
||||
if (type.IsGenericType)
|
||||
{
|
||||
build.RegisterGeneric(type).As(serviceType).InstancePerDependency();
|
||||
}
|
||||
else
|
||||
{
|
||||
build.RegisterType(type).As(serviceType).InstancePerDependency();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void AddAutoIocService(this ContainerBuilder build, params string[] assemblyStr)
|
||||
{
|
||||
foreach (var a in assemblyStr)
|
||||
{
|
||||
RegIoc(build, Assembly.Load(a));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -41,8 +41,8 @@ namespace Yi.Framework.WebCore.Utility
|
||||
|
||||
containerBuilder.RegisterType< HttpContextAccessor>().As<IHttpContextAccessor>().SingleInstance();
|
||||
|
||||
containerBuilder.RegisterGeneric(typeof(Repository<>)).As(typeof(IRepository<>)).InstancePerLifetimeScope();
|
||||
containerBuilder.RegisterGeneric(typeof(BaseService<>)).As(typeof(IBaseService<>)).InstancePerLifetimeScope();
|
||||
//containerBuilder.RegisterGeneric(typeof(Repository<>)).As(typeof(IRepository<>)).InstancePerLifetimeScope();
|
||||
//containerBuilder.RegisterGeneric(typeof(BaseService<>)).As(typeof(IBaseService<>)).InstancePerLifetimeScope();
|
||||
///反射注入服务层及接口层
|
||||
var assemblysServices = GetDll( "Yi.Framework.Service.dll");
|
||||
containerBuilder.RegisterAssemblyTypes(assemblysServices)
|
||||
@@ -60,6 +60,9 @@ namespace Yi.Framework.WebCore.Utility
|
||||
containerBuilder.Register(c => new CustomAutofacAop());//AOP注册
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//containerBuilder.RegisterType<A>().As<IA>().EnableInterfaceInterceptors();开启Aop
|
||||
|
||||
//将数据库对象注入
|
||||
|
||||
Reference in New Issue
Block a user