其他分享
首页 > 其他分享> > DIManager

DIManager

作者:互联网

using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Scg.Core.Extensions.DependencyInjection
{
    /// <summary>
    /// 依赖注入管理类
    /// </summary>
    public static class DIManager
    {
        private static IServiceProvider provider;

        /// <summary>
        /// 必须在所有Service注册完成之后调用,否则会导致手动Resolve某个Service的时候失败
        /// </summary>
        /// <param name="services"></param>
        public static IServiceProvider BuildProvider(IServiceCollection services)
        {
            return provider = services.BuildServiceProvider();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="serviceProvider"></param>
        /// <returns></returns>
        public static IServiceProvider SetProvider(IServiceProvider serviceProvider)
        {
            return provider = serviceProvider;
        }

        /// <summary>
        /// 获取已注册的服务
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static T GetService<T>()
        {
            return provider.GetService<T>();
        }

        public static T GetRequiredService<T>()
        {
            return provider.GetRequiredService<T>();
        }

        /// <summary>
        /// 获取已注册的服务
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="serviceNameStartsWith"></param>
        /// <returns></returns>
        public static T GetService<T>(string serviceNameStartsWith)
        {
            var services = GetService<IEnumerable<T>>();
            return services.FirstOrDefault(x => x.GetType().Name.StartsWith(serviceNameStartsWith));
        }

        /// <summary>
        /// 获取已注册的服务
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="services"></param>
        /// <param name="serviceNameStartsWith"></param>
        /// <returns></returns>
        public static T GetService<T>(IEnumerable<T> services, string serviceNameStartsWith)
        {
            return services.FirstOrDefault(x => x.GetType().Name.StartsWith(serviceNameStartsWith));
        }

        /// <summary>
        /// 获取服务,为空时使用给定值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="instance"></param>
        /// <returns></returns>
        public static T GetServiceOrDefault<T>(T instance)
        {
            var service = provider.GetService<T>();
            if (service == null)
            {
                return instance;
            }
            return service;
        }

        /// <summary>
        /// 获取已注册的服务(租户化实现,若未找到实现,将获取租户code为generic的实现)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="code">租户Code</param>
        /// <returns></returns>
        public static T GetTenantService<T>(string code) where T : class
        {
            //var services = provider.GetServices<T>();
            //foreach (var service in services)
            //{
            //    var attr = service.GetType().GetTypeInfo().GetCustomAttribute<TenantCodesAttribute>(false);
            //    if (attr != null && attr.Codes.Any(p => p.Equals(code, StringComparison.OrdinalIgnoreCase) || p == DIConstants.GenericTenantCode))
            //    {
            //        return service;
            //    }
            //}
            return null;
        }

        /// <summary>
        /// 创建实例(该实例初始化依赖于已注册的部分服务)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static T NewInstance<T>()
        {
            return ActivatorUtilities.CreateInstance<T>(provider);
        }

    }
}

 

startUp中ConfigureServices处 DIManager.BuildProvider(services);

标签:return,service,static,provider,services,DIManager,public
来源: https://www.cnblogs.com/wl-blog/p/16407344.html