|
| 1 | +namespace DotNetToolkit.Repository.Extensions.Ninject |
| 2 | +{ |
| 3 | + using Configuration.Interceptors; |
| 4 | + using Configuration.Options; |
| 5 | + using Configuration.Options.Internal; |
| 6 | + using Factories; |
| 7 | + using global::Ninject; |
| 8 | + using JetBrains.Annotations; |
| 9 | + using System; |
| 10 | + using System.Collections.Generic; |
| 11 | + using System.Linq; |
| 12 | + using System.Reflection; |
| 13 | + using Transactions; |
| 14 | + using Utility; |
| 15 | + |
| 16 | + /// <summary> |
| 17 | + /// Contains various extension methods for <see cref="IKernel" /> |
| 18 | + /// </summary> |
| 19 | + public static class KernelExtensions |
| 20 | + { |
| 21 | + /// <summary> |
| 22 | + /// Binds all the repositories services using the specified options builder. |
| 23 | + /// </summary> |
| 24 | + /// <param name="kernel">The kernel.</param> |
| 25 | + /// <param name="optionsAction">A builder action used to create or modify options for the repositories.</param> |
| 26 | + /// <param name="assembliesToScan">The assemblies to scan.</param> |
| 27 | + /// <remarks> |
| 28 | + /// This method will scan for repositories and interceptors from the specified assemblies collection, and will bind them to the container. |
| 29 | + /// </remarks> |
| 30 | + public static void BindRepositories([NotNull] this IKernel kernel, [NotNull] Action<RepositoryOptionsBuilder> optionsAction, [NotNull] params Assembly[] assembliesToScan) |
| 31 | + { |
| 32 | + Guard.NotNull(kernel, nameof(kernel)); |
| 33 | + Guard.NotNull(optionsAction, nameof(optionsAction)); |
| 34 | + Guard.NotEmpty(assembliesToScan, nameof(assembliesToScan)); |
| 35 | + |
| 36 | + var baseAssembly = Assembly.Load("DotNetToolkit.Repository"); |
| 37 | + |
| 38 | + var assToScan = !assembliesToScan.Any(x => x.FullName.Equals(baseAssembly.FullName)) |
| 39 | + ? assembliesToScan.Concat(new[] { baseAssembly }) |
| 40 | + : assembliesToScan; |
| 41 | + |
| 42 | + var types = assToScan.SelectMany(x => x.GetAccessibleTypes()); |
| 43 | + |
| 44 | + var interfaceTypesToScan = new[] |
| 45 | + { |
| 46 | + typeof(IService<>), |
| 47 | + typeof(IService<,>), |
| 48 | + typeof(IService<,,>), |
| 49 | + typeof(IService<,,,>), |
| 50 | + typeof(IRepository<>), |
| 51 | + typeof(IRepository<,>), |
| 52 | + typeof(IRepository<,,>), |
| 53 | + typeof(IRepository<,,,>), |
| 54 | + typeof(IReadOnlyService<>), |
| 55 | + typeof(IReadOnlyService<,>), |
| 56 | + typeof(IReadOnlyService<,,>), |
| 57 | + typeof(IReadOnlyService<,,,>), |
| 58 | + typeof(IReadOnlyRepository<>), |
| 59 | + typeof(IReadOnlyRepository<,>), |
| 60 | + typeof(IReadOnlyRepository<,,>), |
| 61 | + typeof(IReadOnlyRepository<,,,>), |
| 62 | + typeof(IRepositoryInterceptor) |
| 63 | + }; |
| 64 | + |
| 65 | + // Gets all the interfaces that inherent from IRepository<> or IRepositoryInterceptor |
| 66 | + var interfaceTypes = interfaceTypesToScan |
| 67 | + .SelectMany(interfaceType => types.Where(t => !t.IsClass && t.ImplementsInterface(interfaceType))) |
| 68 | + .Distinct(); |
| 69 | + |
| 70 | + var serviceTypesMapping = interfaceTypes |
| 71 | + .SelectMany(interfaceType => types.Where(t => t.IsClass && !t.IsAbstract && t.ImplementsInterface(interfaceType)) |
| 72 | + .GroupBy(t => interfaceType, t => t)); |
| 73 | + |
| 74 | + // Binds the repositories and interceptors that have been scanned |
| 75 | + var optionsBuilder = new RepositoryOptionsBuilder(); |
| 76 | + |
| 77 | + optionsAction(optionsBuilder); |
| 78 | + |
| 79 | + var registeredInterceptorTypes = new List<Type>(); |
| 80 | + |
| 81 | + foreach (var t in serviceTypesMapping) |
| 82 | + { |
| 83 | + var serviceType = t.Key; |
| 84 | + var implementationTypes = t.Where(x => x.IsGenericType == serviceType.IsGenericType && |
| 85 | + x.GetGenericArguments().Length == serviceType.GetGenericArguments().Length && |
| 86 | + x.IsVisible && !x.IsAbstract); |
| 87 | + |
| 88 | + foreach (var implementationType in implementationTypes) |
| 89 | + { |
| 90 | + if (serviceType == typeof(IRepositoryInterceptor)) |
| 91 | + { |
| 92 | + if (kernel.GetBindings(implementationType).Any() || optionsBuilder.Options.Interceptors.ContainsKey(implementationType)) |
| 93 | + continue; |
| 94 | + |
| 95 | + kernel.Bind(implementationType).ToSelf(); |
| 96 | + kernel.Bind(serviceType).To(implementationType); |
| 97 | + registeredInterceptorTypes.Add(implementationType); |
| 98 | + } |
| 99 | + else |
| 100 | + { |
| 101 | + kernel.Bind(serviceType).To(implementationType); |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + // Binds other services |
| 107 | + kernel.Bind<IRepositoryFactory>().To<RepositoryFactory>(); |
| 108 | + kernel.Bind<IUnitOfWork>().To<UnitOfWork>(); |
| 109 | + kernel.Bind<IUnitOfWorkFactory>().To<UnitOfWorkFactory>(); |
| 110 | + kernel.Bind<IRepositoryOptions>().ToMethod(c => |
| 111 | + { |
| 112 | + var options = new RepositoryOptions(optionsBuilder.Options); |
| 113 | + |
| 114 | + foreach (var interceptorType in registeredInterceptorTypes) |
| 115 | + { |
| 116 | + options.With(interceptorType, () => (IRepositoryInterceptor)c.Kernel.Get(interceptorType)); |
| 117 | + } |
| 118 | + |
| 119 | + return options; |
| 120 | + }); |
| 121 | + |
| 122 | + // Binds resolver |
| 123 | + RepositoryDependencyResolver.SetResolver(type => kernel.Get(type)); |
| 124 | + } |
| 125 | + |
| 126 | + /// <summary> |
| 127 | + /// Binds all the repositories services using the specified options builder. |
| 128 | + /// </summary> |
| 129 | + /// <param name="kernel">The kernel.</param> |
| 130 | + /// <param name="optionsAction">A builder action used to create or modify options for the repositories.</param> |
| 131 | + /// <remarks> |
| 132 | + /// This method will scan for repositories and interceptors from the assemblies that have been loaded into the |
| 133 | + /// execution context of this application domain, and will bind them to the container. |
| 134 | + /// </remarks> |
| 135 | + public static void RegisterRepositories([NotNull] this IKernel kernel, [NotNull] Action<RepositoryOptionsBuilder> optionsAction) |
| 136 | + { |
| 137 | + BindRepositories(kernel, optionsAction, AppDomain.CurrentDomain.GetAssemblies()); |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments