BaseWareHouseService 类静态函数打回原形,改为延迟加载,以避免未登录使用用户情况

This commit is contained in:
alex
2023-10-09 09:32:34 +08:00
parent ac1c140e4a
commit 0300ea2598

View File

@@ -54,7 +54,7 @@ namespace Tnb.WarehouseMgr
private static Dictionary<string, IWHStorageService> _storeMap = new(StringComparer.OrdinalIgnoreCase); private static Dictionary<string, IWHStorageService> _storeMap = new(StringComparer.OrdinalIgnoreCase);
public OverideVisualDevFunc OverideFuncs { get; } = new OverideVisualDevFunc(); public OverideVisualDevFunc OverideFuncs { get; } = new OverideVisualDevFunc();
private readonly ChannelWriter<NotifyMessage> _channelWriter; private readonly ChannelWriter<NotifyMessage> _channelWriter;
public BaseWareHouseService(ChannelWriter<NotifyMessage>? channelWriter = default) public BaseWareHouseService(ChannelWriter<NotifyMessage>? channelWriter = default)
@@ -64,17 +64,23 @@ namespace Tnb.WarehouseMgr
static BaseWareHouseService() static BaseWareHouseService()
{ {
var serviceTypes = App.EffectiveTypes.Where(u => u.IsClass && !u.IsInterface && !u.IsAbstract && typeof(IWHStorageService).IsAssignableFrom(u)).ToList(); _stroageMapLazy = new Lazy<Dictionary<string, IWHStorageService>>(() =>
foreach (var serviceType in serviceTypes)
{ {
var callerName = serviceType.GetCustomAttribute<CallerAttribute>()?.Name ?? string.Empty; Dictionary<string, IWHStorageService> map = new();
if (!callerName.IsNullOrEmpty()) var serviceTypes = App.EffectiveTypes.Where(u => u.IsClass && !u.IsInterface && !u.IsAbstract && typeof(IWHStorageService).IsAssignableFrom(u)).ToList();
foreach (var serviceType in serviceTypes)
{ {
var obj = Activator.CreateInstance(serviceType) as IWHStorageService; var callerName = serviceType.GetCustomAttribute<CallerAttribute>()?.Name ?? string.Empty;
if (obj == null) continue; if (!callerName.IsNullOrEmpty())
_storeMap[callerName] = obj; {
var obj = Activator.CreateInstance(serviceType) as IWHStorageService;
if (obj == null) continue;
map[callerName] = obj;
}
} }
} return map;
});
} }
protected Task<ClaimsPrincipal> GetUserIdentity(string? asscessToken = null) protected Task<ClaimsPrincipal> GetUserIdentity(string? asscessToken = null)
@@ -140,9 +146,9 @@ namespace Tnb.WarehouseMgr
[NonAction] [NonAction]
protected async Task DoUpdate(WareHouseUpInput input) protected async Task DoUpdate(WareHouseUpInput input)
{ {
if (_storeMap.ContainsKey(input.loginType)) if (_stroageMapLazy.Value.ContainsKey(input.loginType))
{ {
await _storeMap[input.loginType].Do(input); await _stroageMapLazy.Value[input.loginType].Do(input);
} }
} }
[NonAction] [NonAction]
@@ -229,7 +235,7 @@ namespace Tnb.WarehouseMgr
} }
} }
if (0 != ret) if (0 != ret)
return ; return;
// sample setting. // sample setting.
PPLBUtility.B_Set_Originpoint(0, 0); PPLBUtility.B_Set_Originpoint(0, 0);
@@ -251,7 +257,7 @@ namespace Tnb.WarehouseMgr
PPLBUtility.B_Prn_Text(200, 50, 0, 2, 2, 2, 'N', code); PPLBUtility.B_Prn_Text(200, 50, 0, 2, 2, 2, 'N', code);
//barcode. //barcode.
PPLBUtility.B_Prn_Barcode(50, 100, 0, "1", 3, 5, 70, 'B', code);//have a counter PPLBUtility.B_Prn_Barcode(50, 100, 0, "1", 3, 5, 70, 'B', code);//have a counter
// output. // output.
PPLBUtility.B_Print_Out(1);// copy 2. PPLBUtility.B_Print_Out(1);// copy 2.
} }
} }