1
0
Quellcode durchsuchen

基础服务新增懒加载服务获取方法,主要用于业务服务接口通过属性懒加载获取

zhontai vor 3 Jahren
Ursprung
Commit
5d9d871342

+ 74 - 0
Admin.Core.Common/Extensions/DictionaryExtensions.cs

@@ -0,0 +1,74 @@
+using System;
+using System.Collections.Generic;
+
+namespace Admin.Core.Common.Extensions
+{
+    public static class DictionaryExtensions
+    {
+        public static TValue GetOrAdd<TKey, TValue>(
+           this IDictionary<TKey, TValue> dictionary,
+           TKey key,
+           Func<TKey, TValue> factory)
+        {
+            TValue value;
+            if (!dictionary.TryGetValue(key, out value))
+            {
+                value = factory(key);
+                dictionary.Add(key, value);
+            }
+            return value;
+        }
+
+        public static TValue GetOrAdd<TKey, TValue>(
+           this IDictionary<TKey, TValue> dictionary,
+           TKey key,
+           Func<TValue> factory)
+        {
+            TValue value;
+            if (!dictionary.TryGetValue(key, out value))
+            {
+                value = factory();
+                dictionary.Add(key, value);
+            }
+            return value;
+        }
+
+        public static TValue AddOrUpdate<TKey, TValue>(
+           this IDictionary<TKey, TValue> dictionary,
+           TKey key,
+           Func<TKey, TValue> addFactory,
+           Func<TKey, TValue, TValue> updateFactory)
+        {
+            TValue value;
+            if (dictionary.TryGetValue(key, out value))
+            {
+                value = updateFactory(key, value);
+            }
+            else
+            {
+                value = addFactory(key);
+            }
+            dictionary[key] = value;
+            return value;
+        }
+
+        public static TValue AddOrUpdate<TKey, TValue>(
+           this IDictionary<TKey, TValue> dictionary,
+           TKey key,
+           Func<TValue> addFactory,
+           Func<TValue, TValue> updateFactory)
+        {
+            TValue value;
+            if (dictionary.TryGetValue(key, out value))
+            {
+                value = updateFactory(value);
+            }
+            else
+            {
+                value = addFactory();
+            }
+            dictionary[key] = value;
+            return value;
+        }
+    }
+}

+ 13 - 0
Admin.Core.Service/Base/BaseService.cs

@@ -1,16 +1,19 @@
 using Admin.Core.Common.Auth;
 using Admin.Core.Common.Cache;
+using Admin.Core.Common.Extensions;
 using AutoMapper;
 using Microsoft.Extensions.DependencyInjection;
 using Microsoft.Extensions.Logging;
 using Microsoft.Extensions.Logging.Abstractions;
 using System;
+using System.Collections.Generic;
 
 namespace Admin.Core.Service
 {
     public abstract class BaseService: IBaseService
     {
         protected readonly object ServiceProviderLock = new object();
+        protected IDictionary<Type, object> CachedServices { get; set; }
         private ICache _cache;
         private ILoggerFactory _loggerFactory;
         private IMapper _mapper;
@@ -60,5 +63,15 @@ namespace Admin.Core.Service
 
             return reference;
         }
+
+        public virtual TService LazyGetRequiredService<TService>()
+        {
+            return (TService)LazyGetRequiredService(typeof(TService));
+        }
+
+        public virtual object LazyGetRequiredService(Type serviceType)
+        {
+            return CachedServices.GetOrAdd(serviceType, () => ServiceProvider.GetRequiredService(serviceType));
+        }
     }
 }