|
| 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.Provider; |
| 12 | +using UnityContainerAttributeRegistration.Attribute; |
| 13 | +using UnityContainerAttributeRegistration.Exention; |
| 14 | + |
| 15 | + |
| 16 | +namespace UnityContainerAttributeRegistration.Populator |
| 17 | +{ |
| 18 | + /// <summary> |
| 19 | + /// Populator for the <see cref="UnityContainerAttributeRegistration.Attribute.RegisterInstanceAttribute" />. |
| 20 | + /// </summary> |
| 21 | + internal class InstancePopulator : Populator |
| 22 | + { |
| 23 | + /// <summary> |
| 24 | + /// ctor |
| 25 | + /// </summary> |
| 26 | + /// <param name="appDomain">Used <see cref="IAssemblyProvider" /> for searching for candidates.</param> |
| 27 | + public InstancePopulator(IAssemblyProvider appDomain) : base(appDomain) |
| 28 | + { |
| 29 | + } |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// Populate the passed <paramref name="container" />. |
| 33 | + /// </summary> |
| 34 | + /// <param name="container"><see cref="IUnityContainer" /> to populate.</param> |
| 35 | + /// <returns>Passed <paramref name="container" />.</returns> |
| 36 | + /// <exception cref="InvalidOperationException">Class type must not be static or abstract.</exception> |
| 37 | + public override IUnityContainer Populate(IUnityContainer container) |
| 38 | + { |
| 39 | + IList<Type> typesWithAttribute = GetTypesWith<RegisterInstanceProviderAttribute>(TypeDefined.Inherit) |
| 40 | + .ToList(); |
| 41 | + |
| 42 | + IEnumerable<InstanceToRegister> instancesToRegister = |
| 43 | + typesWithAttribute.SelectMany(providerClassType => GetInstancesToRegisterFor(container, providerClassType)); |
| 44 | + |
| 45 | + foreach(InstanceToRegister instanceToRegister in instancesToRegister) |
| 46 | + { |
| 47 | + Type type = instanceToRegister.Type; |
| 48 | + object instance = instanceToRegister.Instance; |
| 49 | + IInstanceLifetimeManager lifetimeManager = instanceToRegister.LifetimeManager; |
| 50 | + container.RegisterInstance(type, instance, lifetimeManager); |
| 51 | + } |
| 52 | + |
| 53 | + return container; |
| 54 | + } |
| 55 | + |
| 56 | + /// <summary> |
| 57 | + /// Create a list of <see cref="InstanceToRegister" /> depending on class marked with <see cref="RegisterInstanceProviderAttribute" /> |
| 58 | + /// </summary> |
| 59 | + /// <param name="container"><see cref="IUnityContainer" /> to resolve <paramref name="providerClassType" /></param> |
| 60 | + /// <param name="providerClassType">Class type used to search for <see cref="RegisterInstanceAttribute" /></param> |
| 61 | + /// <returns>List of instances to register with all needed parameters</returns> |
| 62 | + /// <exception cref="InvalidOperationException"><paramref name="providerClassType" /> type must not be static or abstract.</exception> |
| 63 | + private IEnumerable<InstanceToRegister> GetInstancesToRegisterFor(IUnityContainer container, Type providerClassType) |
| 64 | + { |
| 65 | + if(providerClassType.IsStatic() || providerClassType.IsAbstract) |
| 66 | + { |
| 67 | + throw new InvalidOperationException( |
| 68 | + $"Class type must not be static or abstract to be used with RegisterTypeAttribute: {providerClassType.FullName}"); |
| 69 | + } |
| 70 | + |
| 71 | + object providerClassInstance = container.Resolve(providerClassType); |
| 72 | + PropertyInfo[] properties = providerClassType.GetProperties(); |
| 73 | + |
| 74 | + return properties |
| 75 | + .Where(info => info.CustomAttributes.Any(data => data.AttributeType == typeof(RegisterInstanceAttribute))) |
| 76 | + .Select(info => |
| 77 | + { |
| 78 | + object instance = info.GetValue(providerClassInstance); |
| 79 | + RegisterInstanceAttribute attribute = info.GetCustomAttribute<RegisterInstanceAttribute>(); |
| 80 | + Type from = attribute.From; |
| 81 | + IInstanceLifetimeManager lifetimeManager = |
| 82 | + attribute.LifetimeManager == null |
| 83 | + ? null |
| 84 | + : GetInstanceByType<IInstanceLifetimeManager>(attribute.LifetimeManager); |
| 85 | + |
| 86 | + return new InstanceToRegister(instance, from, lifetimeManager); |
| 87 | + }) |
| 88 | + .ToList(); |
| 89 | + } |
| 90 | + |
| 91 | + /// <summary> |
| 92 | + /// Wrapper to regsiter isntances |
| 93 | + /// </summary> |
| 94 | + private sealed class InstanceToRegister |
| 95 | + { |
| 96 | + public InstanceToRegister([CanBeNull] object instance, |
| 97 | + [CanBeNull] Type type, |
| 98 | + [CanBeNull] IInstanceLifetimeManager lifetimeManager) |
| 99 | + { |
| 100 | + Instance = instance; |
| 101 | + Type = type; |
| 102 | + LifetimeManager = lifetimeManager; |
| 103 | + } |
| 104 | + |
| 105 | + /// <summary> |
| 106 | + /// Concrete instance to register |
| 107 | + /// </summary> |
| 108 | + [CanBeNull] |
| 109 | + public object Instance { get; } |
| 110 | + |
| 111 | + /// <summary> |
| 112 | + /// Requested type |
| 113 | + /// </summary> |
| 114 | + [CanBeNull] |
| 115 | + public Type Type { get; } |
| 116 | + |
| 117 | + /// <summary> |
| 118 | + /// Used lifetime manager |
| 119 | + /// </summary> |
| 120 | + [CanBeNull] |
| 121 | + public IInstanceLifetimeManager LifetimeManager { get; } |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments