添加属性依赖注入、添加自定义日志扩展

This commit is contained in:
陈淳
2022-11-07 01:31:37 +08:00
parent 86e869ff16
commit d6ca4429d5
13 changed files with 218 additions and 21 deletions

View File

@@ -0,0 +1,21 @@
using Castle.DynamicProxy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Yi.Framework.WebCore.AutoFacExtend
{
public class CustomAutofacAop : IInterceptor
{
public void Intercept(IInvocation invocation)
{
Console.WriteLine($"invocation.Methond={invocation.Method}");
Console.WriteLine($"invocation.Arguments={string.Join(",", invocation.Arguments)}");
invocation.Proceed(); //继续执行
Console.WriteLine($"方法{invocation.Method}执行完成了");
}
}
}

View File

@@ -0,0 +1,93 @@
using Autofac;
using Autofac.Extras.DynamicProxy;
using Castle.DynamicProxy;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.ApplicationParts;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Yi.Framework.Interface;
using Yi.Framework.Job;
using Yi.Framework.Repository;
using Yi.Framework.Service;
using Yi.Framework.WebCore.AutoFacExtend;
using Module = Autofac.Module;
namespace Yi.Framework.WebCore.AutoFacExtend
{
public class CustomAutofacModule : Module
{
private Assembly GetDll(string ass)
{
var basePath = AppContext.BaseDirectory;
var servicesDllFile = Path.Combine(basePath, ass);
if (!(File.Exists(servicesDllFile)))
{
var msg = "service.dll 丢失,请编译后重新生成。";
throw new Exception(msg);
}
return Assembly.LoadFrom(servicesDllFile); ;
}
protected override void Load(ContainerBuilder containerBuilder)
{
//containerBuilder.RegisterType<DbContextFactory>().As<IDbContextFactory>().InstancePerDependency().EnableInterfaceInterceptors();
containerBuilder.RegisterType< HttpContextAccessor>().As<IHttpContextAccessor>().SingleInstance();
//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)
.AsImplementedInterfaces()
.InstancePerLifetimeScope()
.EnableInterfaceInterceptors();
///反射注册任务调度层
var assemblysJob = GetDll("Yi.Framework.Job.dll");
containerBuilder.RegisterAssemblyTypes(assemblysJob)
.InstancePerDependency();
containerBuilder.Register(c => new CustomAutofacAop());//AOP注册
//containerBuilder.RegisterType<A>().As<IA>().EnableInterfaceInterceptors();开启Aop
//将数据库对象注入
//containerBuilder.RegisterType<DataContext>().As<DbContext>().InstancePerLifetimeScope().EnableInterfaceInterceptors();
//containerBuilder.RegisterGeneric(typeof(BaseService<>)).As(typeof(IBaseService<>)).InstancePerDependency().EnableInterfaceInterceptors();
}
}
}
public interface IAutofacTest
{
void Show(int id, string name);
}
[Intercept(typeof(CustomAutofacAop))]
public class AutofacTest : IAutofacTest
{
public void Show(int id, string name)
{
Console.WriteLine($"This is {id} _ {name}");
}
}

View File

@@ -0,0 +1,51 @@
using Autofac;
using Autofac.Core;
using Autofac.Extras.DynamicProxy;
using Castle.DynamicProxy;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ApplicationParts;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Yi.Framework.Interface;
using Yi.Framework.Job;
using Yi.Framework.Repository;
using Yi.Framework.Service;
using Module = Autofac.Module;
namespace Yi.Framework.WebCore.AutoFacExtend
{
public class PropertiesAutowiredModule : Module
{
protected override void Load(ContainerBuilder containerBuilder)
{
//这里设置哪些程序集可以进行属性注入,默认控制器即可
// 获取所有控制器类型并使用属性注入
var controllerBaseType = typeof(ControllerBase);
//先暂时获取这里的程序集之后改造通过option进行配置
containerBuilder.RegisterAssemblyTypes( Assembly.Load("Yi.Framework.ApiMicroservice"))
.Where(t => controllerBaseType.IsAssignableFrom(t) && t != controllerBaseType)
.PropertiesAutowired(new AutowiredPropertySelector());
}
}
public class AutowiredPropertySelector : IPropertySelector
{
public bool InjectProperty(PropertyInfo propertyInfo, object instance)
{
return propertyInfo.CustomAttributes.Any(it => it.AttributeType == typeof(AutowiredAttribute));
}
}
}