Skip to content
  • P
    Projects
  • G
    Groups
  • S
    Snippets
  • Help

丁松杰 / Pole

  • This project
    • Loading...
  • Sign in
Go to a project
  • Project
  • Repository
  • Issues 0
  • Merge Requests 0
  • Pipelines
  • Wiki
  • Snippets
  • Members
  • Activity
  • Graph
  • Charts
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
  • Files
  • Commits
  • Branches
  • Tags
  • Contributors
  • Graph
  • Compare
  • Charts
Switch branch/tag
  • Pole
  • src
  • Pole.Application
  • PoleOptionsExtensions.cs
Find file
BlameHistoryPermalink
  • dingsongjie's avatar
    application 紧密集成 可靠消息 · 7b305c9c
    dingsongjie committed 5 years ago
    7b305c9c
PoleOptionsExtensions.cs 3.49 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
using MediatR;
using Microsoft.Extensions.DependencyInjection;
using Pole.Application;
using Pole.Application.MediatR;
using Pole.Core.DependencyInjection;
using Pole.ReliableMessage;
using System;
using System.Linq;
using System.Reflection;

namespace Microsoft.Extensions.DependencyInjection
{
    public static class PoleOptionsExtensions
    {
        public static PoleOptions AddManageredAssemblies(this PoleOptions options, params Assembly[] assemblies)
        {
            options.ApplicationAssemblies = assemblies;
            return options;
        }
        public static PoleOptions AutoInjectionDependency(this PoleOptions options)
        {
            var assemblies = options.ApplicationAssemblies ?? throw new Exception("Cant't find ApplicationAssemblies,You must Run  PoleOptions.AddManageredAssemblies First");

            foreach (var assembly in assemblies)
            {
                AddScoped(options, assembly);
                AddTransient(options, assembly);
                AddSingleton(options, assembly);
            }
            return options;
        }

        public static PoleOptions AutoInjectionCommandHandlersAndDomainEventHandlers(this PoleOptions options, ServiceLifetime lifetime = ServiceLifetime.Scoped)
        {
            var assemblies = options.ApplicationAssemblies ?? throw new Exception("Cant't find ApplicationAssemblies,You must Run  PoleOptions.AddManageredAssemblies First");
            options.Services.AddMediatR(config =>
            {
                config.AddServiceLifetime(lifetime);
            }, assemblies.ToArray());
            return options;
        }
        public static PoleOptions AddPoleReliableMessage(this PoleOptions options, Action<ReliableMessageOption> optionConfig)
        {
            options.Services.AddPoleReliableMessage(optionConfig);
            return options;
        }
        #region Internal
        private static void AddScoped(PoleOptions options, Assembly assembly)
        {
            var implements = assembly.GetTypes().Where(m => typeof(IScopedDenpendency).IsAssignableFrom(m) && m.IsClass && !m.IsAbstract);
            foreach (var implement in implements)
            {
                var services = implement.GetInterfaces();
                foreach (var queriesService in services)
                {
                    options.Services.AddScoped(queriesService, implement);
                }
            }
        }

        private static void AddTransient(PoleOptions options, Assembly assembly)
        {
            var implements = assembly.GetTypes().Where(m => typeof(ITransientDependency).IsAssignableFrom(m) && m.IsClass && !m.IsAbstract);
            foreach (var implement in implements)
            {
                var services = implement.GetInterfaces();
                foreach (var queriesService in services)
                {
                    options.Services.AddTransient(queriesService, implement);
                }
            }
        }

        private static void AddSingleton(PoleOptions options, Assembly assembly)
        {
            var implements = assembly.GetTypes().Where(m => typeof(ISingleDependency).IsAssignableFrom(m) && m.IsClass && !m.IsAbstract);
            foreach (var implement in implements)
            {
                var services = implement.GetInterfaces();
                foreach (var queriesService in services)
                {
                    options.Services.AddSingleton(queriesService, implement);
                }
            }
        }
        #endregion
    }
}