Yuebon.NetCore/Yuebon.Commons/Extensions/AppServiceCollectionExtensions.cs

(开头部分) 3KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

using Microsoft.Extensions.DependencyInjection;
using System.Linq;
using Yuebon.Commons.DependencyInjection;
using Yuebon.Commons.Helpers;

namespace Yuebon.Commons.Extensions
{
    /// <summary>
    /// IServiceCollection自定义扩展
    /// </summary>
    public static class AppServiceCollectionExtensions
    {
        /// <summary>
        /// 添加自动扫描注入Service服务和Respository仓储
        /// <para>
        /// 需要注意的是,遵循如下约定:
        /// IUserService --> UserService, IUserRepository --> UserRepository.
        /// </para>
        /// </summary>
        /// <param name="services">服务集合</param>
        /// <returns>服务集合</returns>
        public static IServiceCollection AddAutoScanInjection(this IServiceCollection services)
        {
            RuntimeHelper.GetAllYuebonAssemblies().ToList().ForEach(a =>
            {
                ConsoleHelper.WriteInfoLine($"处理{a.FullName}.dll 注入");
                a.GetTypes().Where(t => typeof(IPrivateDependency).IsAssignableFrom(t) && t.IsClass).ToList().ForEach(t =>
                {
                    var serviceType = t.GetInterface($"I{t.Name}");
                    //if (serviceType != null)
                    //{
                    //    services.AddScoped(serviceType, t);
                    //}
                    //else
                    //{
                    //    services.AddScoped(t);
                    //}
                    if ((serviceType ?? t).GetInterface(typeof(ISingletonDependency).Name) != null)
                    {
                        if (serviceType != null)
                        {
                            services.AddSingleton(serviceType, t);
                        }
                        else
                        {
                            services.AddSingleton(t);
                        }
                    }
                    else if ((serviceType ?? t).GetInterface(typeof(IScopedDependency).Name) != null)
                    {
                        if (serviceType != null)
                        {
                            services.AddScoped(serviceType, t);
                        }
                        else
                        {
                            services.AddScoped(t);
                        }
                    }
                    else if ((serviceType ?? t).GetInterface(typeof(ITransientDependency).Name) != null)
后面还有 28 行代码,解锁后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 算力/个。 规则说明

AI 解读 AI

登录后可用,每次 10 算力,解读结果公开显示在下面。

还没有人解读过这个文件。