优化注册服务

This commit is contained in:
不做码农
2023-03-18 07:58:39 +08:00
parent 1993299799
commit 5dc7545c12
8 changed files with 33 additions and 56 deletions

View File

@@ -0,0 +1,67 @@
using Infrastructure.Attribute;
using Microsoft.Extensions.DependencyInjection;
using System.Linq;
using System.Reflection;
namespace ZR.Admin.WebApi.Extensions
{
/// <summary>
/// App服务注册
/// </summary>
public static class AppServiceExtensions
{
/// <summary>
/// 注册引用程序域中所有有AppService标记的类的服务
/// </summary>
/// <param name="services"></param>
public static void AddAppService(this IServiceCollection services)
{
string[] cls = new string[] { "ZR.Repository", "ZR.Service", "ZR.Tasks" };
foreach (var item in cls)
{
Register(services, item);
}
}
private static void Register(IServiceCollection services, string item)
{
Assembly assembly = Assembly.Load(item);
foreach (var type in assembly.GetTypes())
{
var serviceAttribute = type.GetCustomAttribute<AppServiceAttribute>();
if (serviceAttribute != null)
{
var serviceType = serviceAttribute.ServiceType;
//情况1 适用于依赖抽象编程,注意这里只获取第一个
if (serviceType == null && serviceAttribute.InterfaceServiceType)
{
serviceType = type.GetInterfaces().FirstOrDefault();
}
//情况2 不常见特殊情况下才会指定ServiceType写起来麻烦
if (serviceType == null)
{
serviceType = type;
}
switch (serviceAttribute.ServiceLifetime)
{
case LifeTime.Singleton:
services.AddSingleton(serviceType, type);
break;
case LifeTime.Scoped:
services.AddScoped(serviceType, type);
break;
case LifeTime.Transient:
services.AddTransient(serviceType, type);
break;
default:
services.AddTransient(serviceType, type);
break;
}
//System.Console.WriteLine($"注册:{serviceType}");
}
}
}
}
}

View File

@@ -11,18 +11,9 @@ namespace ZR.Admin.WebApi.Extensions
var types = source?.GetType();
if (types == null) return source;
if (types.GetProperty("CreateTime") != null)
{
types.GetProperty("CreateTime")?.SetValue(source, DateTime.Now, null);
}
if (types.GetProperty("AddTime") != null)
{
types.GetProperty("AddTime")?.SetValue(source, DateTime.Now, null);
}
if (types.GetProperty("UpdateTime") != null)
{
types.GetProperty("UpdateTime")?.SetValue(source, DateTime.Now, null);
}
types.GetProperty("CreateTime")?.SetValue(source, DateTime.Now, null);
types.GetProperty("AddTime")?.SetValue(source, DateTime.Now, null);
types.GetProperty("UpdateTime")?.SetValue(source, DateTime.Now, null);
if (types.GetProperty("Create_by") != null && context != null)
{
types.GetProperty("Create_by")?.SetValue(source, context.GetName(), null);
@@ -47,22 +38,12 @@ namespace ZR.Admin.WebApi.Extensions
var types = source?.GetType();
if (types == null) return source;
if (types.GetProperty("UpdateTime") != null)
{
types.GetProperty("UpdateTime")?.SetValue(source, DateTime.Now, null);
}
if (types.GetProperty("Update_time") != null)
{
types.GetProperty("Update_time")?.SetValue(source, DateTime.Now, null);
}
if (types.GetProperty("UpdateBy") != null)
{
types.GetProperty("UpdateBy")?.SetValue(source, context?.GetName(), null);
}
if (types.GetProperty("Update_by") != null)
{
types.GetProperty("Update_by")?.SetValue(source, context?.GetName(), null);
}
types.GetProperty("UpdateTime")?.SetValue(source, DateTime.Now, null);
types.GetProperty("Update_time")?.SetValue(source, DateTime.Now, null);
types.GetProperty("UpdateBy")?.SetValue(source,context.GetName(), null);
types.GetProperty("Update_by")?.SetValue(source, context.GetName(), null);
return source;
}

View File

@@ -1,7 +1,5 @@
using Infrastructure;
using Infrastructure.Extensions;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
@@ -10,7 +8,6 @@ using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using UAParser;
using ZR.Model.System;
@@ -73,7 +70,10 @@ namespace ZR.Admin.WebApi.Extensions
public static long GetUId(this HttpContext context)
{
var uid = context.User.FindFirstValue(ClaimTypes.PrimarySid);
if (uid.IsEmpty() || uid.IsNullOrZero())
{
throw new CustomException(ResultCode.DENY, "未登录");
}
return !string.IsNullOrEmpty(uid) ? long.Parse(uid) : 0;
}