|
| 1 | +using System.Reflection; |
| 2 | +using Elastic.Clients.Elasticsearch; |
| 3 | +using FluentConfiguration.Configurations; |
| 4 | + |
| 5 | +namespace FluentConfiguration; |
| 6 | + |
| 7 | +public class ElasticsearchRegisterHelper |
| 8 | +{ |
| 9 | + /// <summary> |
| 10 | + /// Execute connection mapping config |
| 11 | + /// </summary> |
| 12 | + /// <param name="connectionSettings"></param> |
| 13 | + /// <param name="elsConfigs"></param> |
| 14 | + public static void ConfigureConnectionSettings( |
| 15 | + ref ElasticsearchClientSettings connectionSettings, |
| 16 | + IEnumerable<ElasticConfigureResult> configures |
| 17 | + ) |
| 18 | + { |
| 19 | + foreach (var configure in configures) |
| 20 | + { |
| 21 | + object? connectionSettingEvaluator = Activator.CreateInstance( |
| 22 | + typeof(ConnectionSettingEvaluator), |
| 23 | + [connectionSettings] |
| 24 | + ); |
| 25 | + |
| 26 | + var evaluateMethodInfo = typeof(ConnectionSettingEvaluator) |
| 27 | + .GetMethod(nameof(IEvaluatorSync.Evaluate))! |
| 28 | + .MakeGenericMethod(configure.Type); |
| 29 | + |
| 30 | + evaluateMethodInfo.Invoke(connectionSettingEvaluator, [configure.Configs]); |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// execute config classes by reflection |
| 36 | + /// </summary> |
| 37 | + /// <param name="elasticClient"></param> |
| 38 | + /// <param name="elsConfigs"></param> |
| 39 | + /// <returns></returns> |
| 40 | + public static async Task ElasticFluentConfigAsync( |
| 41 | + ElasticsearchClient elasticClient, |
| 42 | + IEnumerable<ElasticConfigureResult> configures |
| 43 | + ) |
| 44 | + { |
| 45 | + foreach (var configure in configures) |
| 46 | + { |
| 47 | + object? elasticsearchClientEvaluator = Activator.CreateInstance( |
| 48 | + typeof(ElasticsearchClientEvaluator), |
| 49 | + [elasticClient] |
| 50 | + ); |
| 51 | + |
| 52 | + var evaluateMethodInfo = typeof(ElasticsearchClientEvaluator) |
| 53 | + .GetMethod(nameof(IEvaluator.Evaluate))! |
| 54 | + .MakeGenericMethod(configure.Type); |
| 55 | + |
| 56 | + await (Task) |
| 57 | + evaluateMethodInfo.Invoke(elasticsearchClientEvaluator, [configure.Configs])!; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /// <summary> |
| 62 | + /// get all of config classes by reflection |
| 63 | + /// </summary> |
| 64 | + /// <param name="assembly"></param> |
| 65 | + /// <returns></returns> |
| 66 | + public static IEnumerable<ElasticConfigureResult> GetElasticsearchConfigBuilder( |
| 67 | + Assembly assembly, |
| 68 | + string prefix |
| 69 | + ) |
| 70 | + { |
| 71 | + var configuringTypes = GetConfiguringTypes(assembly); |
| 72 | + |
| 73 | + foreach (var (type, iType) in configuringTypes) |
| 74 | + { |
| 75 | + var method = GetConfigureMethod(type); |
| 76 | + if (method == null) |
| 77 | + continue; |
| 78 | + |
| 79 | + var elasticsearchConfigBuilder = CreateElasticsearchConfigBuilder(iType); |
| 80 | + var elsConfig = Activator.CreateInstance(type); |
| 81 | + |
| 82 | + method.Invoke(elsConfig, [elasticsearchConfigBuilder, prefix]); |
| 83 | + |
| 84 | + yield return new ElasticConfigureResult(elasticsearchConfigBuilder!, iType); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + private static IEnumerable<(Type type, Type iType)> GetConfiguringTypes(Assembly assembly) |
| 89 | + { |
| 90 | + return assembly |
| 91 | + .GetTypes() |
| 92 | + .Where(type => |
| 93 | + type.GetInterfaces().Any(@interface => IsElasticsearchDocumentConfigure(@interface)) |
| 94 | + ) |
| 95 | + .Select(type => |
| 96 | + ( |
| 97 | + type, |
| 98 | + iType: type.GetInterfaces() |
| 99 | + .First(@interface => IsElasticsearchDocumentConfigure(@interface)) |
| 100 | + .GenericTypeArguments[0] |
| 101 | + ) |
| 102 | + ); |
| 103 | + } |
| 104 | + |
| 105 | + private static bool IsElasticsearchDocumentConfigure(Type @interface) |
| 106 | + { |
| 107 | + return @interface.IsGenericType |
| 108 | + && @interface.GetGenericTypeDefinition() == typeof(IElasticsearchDocumentConfigure<>); |
| 109 | + } |
| 110 | + |
| 111 | + private static MethodInfo? GetConfigureMethod(Type type) |
| 112 | + { |
| 113 | + return type.GetMethod(nameof(IElasticsearchDocumentConfigure<object>.Configure)); |
| 114 | + } |
| 115 | + |
| 116 | + private static object CreateElasticsearchConfigBuilder(Type documentType) |
| 117 | + { |
| 118 | + var builderType = typeof(ElasticsearchConfigBuilder<>).MakeGenericType(documentType); |
| 119 | + return Activator.CreateInstance(builderType)!; |
| 120 | + } |
| 121 | +} |
0 commit comments