添加特性自动依赖注入
This commit is contained in:
@@ -7,6 +7,7 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Yi.Framework.Common.Attribute;
|
||||
using Yi.Framework.Common.Const;
|
||||
using Yi.Framework.Common.Models;
|
||||
using Yi.Framework.Core;
|
||||
|
||||
@@ -5,6 +5,7 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Yi.Framework.Common.Attribute;
|
||||
using Yi.Framework.Common.Const;
|
||||
using Yi.Framework.Common.Enum;
|
||||
using Yi.Framework.Common.Helper;
|
||||
|
||||
@@ -31,10 +31,15 @@ builder.Host.ConfigureAppConfiguration((hostBuilderContext, configurationBuilder
|
||||
builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());
|
||||
builder.Host.ConfigureContainer<ContainerBuilder>(containerBuilder =>
|
||||
{
|
||||
|
||||
#region
|
||||
//<2F><><EFBFBD><EFBFBD>Module<6C><65><EFBFBD><EFBFBD>ע<EFBFBD><D7A2>
|
||||
#endregion
|
||||
containerBuilder.RegisterModule<CustomAutofacModule>();
|
||||
#region
|
||||
//ʹ<><CAB9>AppService<63><65><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ŵĽ<C5B5><C4BD><EFBFBD><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD>ע<EFBFBD><D7A2>,<2C>ִ<EFBFBD><D6B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD>ø<EFBFBD><C3B8>ַ<EFBFBD>ʽ<EFBFBD>Զ<EFBFBD>ע<EFBFBD><D7A2>
|
||||
#endregion
|
||||
containerBuilder.AddAutoIocService("Yi.Framework.Repository", "Yi.Framework.Service");
|
||||
});
|
||||
builder.Host.ConfigureLogging(loggingBuilder =>
|
||||
{
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
|
||||
using System;
|
||||
|
||||
namespace Yi.Framework.Common.Attribute
|
||||
{
|
||||
/// <summary>
|
||||
/// 参考地址:https://www.cnblogs.com/kelelipeng/p/10643556.html
|
||||
/// 1、[AppService]:自动去找接口,如果存在就是接口,如果不存在就是本身
|
||||
/// 2、[AppService(ServiceType = typeof(注册抽象或者接口或者本身))],手动去注册,放type即可
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Class, Inherited = false)]
|
||||
public class AppServiceAttribute : System.Attribute
|
||||
{
|
||||
/// <summary>
|
||||
/// 服务声明周期
|
||||
/// 不给默认值的话注册的是作用域
|
||||
/// </summary>
|
||||
public LifeTime ServiceLifetime { get; set; } = LifeTime.Scoped;
|
||||
/// <summary>
|
||||
/// 指定服务类型
|
||||
/// </summary>
|
||||
public Type ServiceType { get; set; }
|
||||
|
||||
}
|
||||
|
||||
public enum LifeTime
|
||||
{
|
||||
Transient, Scoped, Singleton
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
using Microsoft.IdentityModel.JsonWebTokens;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@@ -8,10 +7,10 @@ using System.Threading.Tasks;
|
||||
using Yi.Framework.Common.Const;
|
||||
using Yi.Framework.Common.Enum;
|
||||
|
||||
namespace Yi.Framework.WebCore.AttributeExtend
|
||||
namespace Yi.Framework.Common.Attribute
|
||||
{
|
||||
[AttributeUsage(AttributeTargets.Method)]
|
||||
public class LogAttribute : Attribute
|
||||
public class LogAttribute : System.Attribute
|
||||
{
|
||||
/// <summary>
|
||||
/// 操作类型
|
||||
@@ -1,6 +1,7 @@
|
||||
using SqlSugar;
|
||||
using System.Data;
|
||||
using System.Linq.Expressions;
|
||||
using Yi.Framework.Common.Attribute;
|
||||
using Yi.Framework.Common.Models;
|
||||
using Yi.Framework.Model.Models;
|
||||
using Yi.Framework.Model.Query;
|
||||
@@ -12,6 +13,7 @@ namespace Yi.Framework.Repository
|
||||
/// 仓储模式
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
[AppService]
|
||||
public class Repository<T> : SimpleClient<T>, IRepository<T> where T : class, new()
|
||||
{
|
||||
public ISugarQueryable<T> _DbQueryable { get { return base.Context.Queryable<T>(); } set { } }
|
||||
|
||||
@@ -3,12 +3,14 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Yi.Framework.Common.Attribute;
|
||||
using Yi.Framework.Interface;
|
||||
using Yi.Framework.Model.Models;
|
||||
using Yi.Framework.Repository;
|
||||
|
||||
namespace Yi.Framework.Service
|
||||
{
|
||||
[AppService]
|
||||
public class BaseService<T>:IBaseService<T> where T:class,new()
|
||||
{
|
||||
public IRepository<T> _repository { get; set; }
|
||||
|
||||
@@ -19,7 +19,7 @@ namespace Yi.Framework.Job
|
||||
|
||||
public Task Execute(IJobExecutionContext context)
|
||||
{
|
||||
return Task.Run(() =>
|
||||
return Task.Run(async () =>
|
||||
{
|
||||
var jobData = context.JobDetail.JobDataMap;
|
||||
string method= jobData[Common.Const.JobConst.method].ToString();
|
||||
@@ -28,10 +28,10 @@ namespace Yi.Framework.Job
|
||||
switch (method)
|
||||
{
|
||||
case "post":
|
||||
data = Common.Helper.HttpHelper.HttpPost(url);
|
||||
data =await Common.Helper.HttpHelper.Post(url);
|
||||
break;
|
||||
case "get":
|
||||
data = Common.Helper.HttpHelper.HttpGet(url);
|
||||
data =await Common.Helper.HttpHelper.Get(url);
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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