|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Reflection; |
| 5 | + |
| 6 | +using JetBrains.Annotations; |
| 7 | + |
| 8 | +using Unity; |
| 9 | +using Unity.Lifetime; |
| 10 | + |
| 11 | +using UnityContainerAttributeRegistration.Attribute; |
| 12 | + |
| 13 | + |
| 14 | +namespace UnityContainerAttributeRegistration.Populator |
| 15 | +{ |
| 16 | + /// <summary> |
| 17 | + /// Populator for the <see cref="UnityContainerAttributeRegistration.Attribute.RegisterFactoryAttribute" />. |
| 18 | + /// </summary> |
| 19 | + internal class FactoryPopulator : Populator |
| 20 | + { |
| 21 | + /// <inheritdoc cref="Populator.Populate" /> |
| 22 | + /// <exception cref="InvalidOperationException">Invalid factory method.</exception> |
| 23 | + public override IUnityContainer Populate(IUnityContainer container, IList<Type> typesWithAttribute) |
| 24 | + { |
| 25 | + IEnumerable<FactoryToRegister> factoriesToRegister = |
| 26 | + typesWithAttribute.SelectMany(providerClassType => GetFactoryToRegisterFor(container, providerClassType)); |
| 27 | + |
| 28 | + foreach(FactoryToRegister factoryToRegister in factoriesToRegister) |
| 29 | + { |
| 30 | + Type returnType = factoryToRegister.ReturnType; |
| 31 | + Func<IUnityContainer, Type, string, object> factory = factoryToRegister.Factory; |
| 32 | + IFactoryLifetimeManager lifetimeManager = factoryToRegister.LifetimeManager; |
| 33 | + |
| 34 | + container.RegisterFactory(returnType, factory, lifetimeManager); |
| 35 | + } |
| 36 | + |
| 37 | + return container; |
| 38 | + } |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// Get all candidates to register. |
| 42 | + /// </summary> |
| 43 | + /// <param name="container"><see cref="IUnityContainer" /> to instantiate the provider class</param> |
| 44 | + /// <param name="providerClassType">Factory provider class</param> |
| 45 | + /// <returns>List of candidates to register</returns> |
| 46 | + /// <exception cref="InvalidOperationException">Invalid factory method.</exception> |
| 47 | + private IEnumerable<FactoryToRegister> GetFactoryToRegisterFor(IUnityContainer container, Type providerClassType) |
| 48 | + { |
| 49 | + object providerClassInstance = container.Resolve(providerClassType); |
| 50 | + MethodInfo[] methodInfos = providerClassType.GetMethods(); |
| 51 | + |
| 52 | + return methodInfos |
| 53 | + .Where(info => info.CustomAttributes.Any(data => data.AttributeType == typeof(RegisterFactoryAttribute))) |
| 54 | + .Select(info => CreateFactoryToRegisterFrom(info, providerClassInstance)) |
| 55 | + .ToList(); |
| 56 | + } |
| 57 | + |
| 58 | + /// <summary> |
| 59 | + /// Transform a <see cref="MethodInfo" /> and an instance to a <see cref="FactoryToRegister" /> object. |
| 60 | + /// </summary> |
| 61 | + /// <param name="info"><see cref="MethodInfo" /> of the factory method.</param> |
| 62 | + /// <param name="instance">Instance of the factory method.</param> |
| 63 | + /// <returns>Wrapper for easy registration.</returns> |
| 64 | + /// <exception cref="InvalidOperationException">Invalid factory method.</exception> |
| 65 | + private FactoryToRegister CreateFactoryToRegisterFrom(MethodInfo info, object instance) |
| 66 | + { |
| 67 | + RegisterFactoryAttribute attribute = info.GetCustomAttribute<RegisterFactoryAttribute>(); |
| 68 | + Type returnType = attribute.From ?? info.ReturnType; |
| 69 | + |
| 70 | + if(returnType == typeof(void)) |
| 71 | + { |
| 72 | + throw new InvalidOperationException("Return type must not be void."); |
| 73 | + } |
| 74 | + |
| 75 | + if(!IsUnityFactorySignature(info)) |
| 76 | + { |
| 77 | + throw new InvalidOperationException("Factory method signature does not match."); |
| 78 | + } |
| 79 | + |
| 80 | + IFactoryLifetimeManager lifetimeManager = |
| 81 | + attribute.LifetimeManager == null |
| 82 | + ? null |
| 83 | + : GetInstanceByType<IFactoryLifetimeManager>(attribute.LifetimeManager); |
| 84 | + |
| 85 | + return new FactoryToRegister(returnType, GetFactoryMethodFor(info, instance), lifetimeManager); |
| 86 | + } |
| 87 | + |
| 88 | + /// <summary> |
| 89 | + /// Verify that the parameters matches the requirements of Unity |
| 90 | + /// </summary> |
| 91 | + /// <param name="methodInfo"><see cref="MethodInfo" /> to check.</param> |
| 92 | + /// <returns>Whether <paramref name="methodInfo" /> matches the requirements or not.</returns> |
| 93 | + private bool IsUnityFactorySignature(MethodInfo methodInfo) |
| 94 | + { |
| 95 | + ParameterInfo[] parameters = methodInfo.GetParameters(); |
| 96 | + |
| 97 | + switch(parameters.Length) |
| 98 | + { |
| 99 | + case 1 when parameters[0] |
| 100 | + .ParameterType == typeof(IUnityContainer): |
| 101 | + case 3 when parameters[0] |
| 102 | + .ParameterType == typeof(IUnityContainer) && parameters[1] |
| 103 | + .ParameterType == typeof(Type) && parameters[2] |
| 104 | + .ParameterType == typeof(string): |
| 105 | + { |
| 106 | + return true; |
| 107 | + } |
| 108 | + default: |
| 109 | + { |
| 110 | + return false; |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + /// <summary> |
| 116 | + /// Create a wrapper function for a factory method. |
| 117 | + /// </summary> |
| 118 | + /// <param name="methodInfo"><see cref="MethodInfo" /> of the factory method.</param> |
| 119 | + /// <param name="instance">Instance of the factory method.</param> |
| 120 | + /// <returns><see cref="Func{TResult}" /> to call the factory method.</returns> |
| 121 | + private Func<IUnityContainer, Type, string, object> GetFactoryMethodFor(MethodInfo methodInfo, object instance) |
| 122 | + { |
| 123 | + return (container, typeValue, stringValue) => |
| 124 | + { |
| 125 | + IList<object> invokeParams = new List<object> {container}; |
| 126 | + |
| 127 | + if(methodInfo.GetParameters() |
| 128 | + .Length == 3) |
| 129 | + { |
| 130 | + invokeParams.Add(typeValue); |
| 131 | + invokeParams.Add(stringValue); |
| 132 | + } |
| 133 | + |
| 134 | + return methodInfo.Invoke(instance, invokeParams.ToArray()); |
| 135 | + }; |
| 136 | + } |
| 137 | + |
| 138 | + /// <summary> |
| 139 | + /// Wrapper to register factories |
| 140 | + /// </summary> |
| 141 | + private sealed class FactoryToRegister |
| 142 | + { |
| 143 | + public FactoryToRegister([NotNull] Type returnType, |
| 144 | + [NotNull] Func<IUnityContainer, Type, string, object> factory, |
| 145 | + [CanBeNull] IFactoryLifetimeManager lifetimeManager) |
| 146 | + { |
| 147 | + ReturnType = returnType; |
| 148 | + Factory = factory; |
| 149 | + LifetimeManager = lifetimeManager; |
| 150 | + } |
| 151 | + |
| 152 | + /// <summary> |
| 153 | + /// Requested type as return type of the factory |
| 154 | + /// </summary> |
| 155 | + [NotNull] |
| 156 | + public Type ReturnType { get; } |
| 157 | + |
| 158 | + /// <summary> |
| 159 | + /// Factory method |
| 160 | + /// </summary> |
| 161 | + [NotNull] |
| 162 | + public Func<IUnityContainer, Type, string, object> Factory { get; } |
| 163 | + |
| 164 | + /// <summary> |
| 165 | + /// Used lifetime manager |
| 166 | + /// </summary> |
| 167 | + [CanBeNull] |
| 168 | + public IFactoryLifetimeManager LifetimeManager { get; } |
| 169 | + } |
| 170 | + } |
| 171 | +} |
0 commit comments