|
1 | | -using System; |
2 | | -using System.Collections.Generic; |
3 | | -using System.Linq; |
| 1 | +using System.Collections.Generic; |
4 | 2 | using System.Reflection; |
5 | 3 |
|
6 | 4 | namespace Autofac.Extras.IocManager |
7 | 5 | { |
8 | | - public static class AutofacExtensions |
9 | | - { |
10 | | - /// <summary> |
11 | | - /// Helper for anonymouse resolvings <see cref="IocManager.Resolve{T}(object)" /> |
12 | | - /// </summary> |
13 | | - /// <param name="this">The this.</param> |
14 | | - /// <returns></returns> |
15 | | - internal static IEnumerable<TypedParameter> GetTypedResolvingParameters(this object @this) |
16 | | - { |
17 | | - foreach (PropertyInfo propertyInfo in @this.GetType().GetTypeInfo().GetProperties()) |
18 | | - { |
19 | | - yield return new TypedParameter(propertyInfo.PropertyType, propertyInfo.GetValue(@this, null)); |
20 | | - } |
21 | | - } |
22 | | - |
23 | | - /// <summary> |
24 | | - /// Finds all types based <see cref="TLifetime" /> in given <see cref="Assembly" /> |
25 | | - /// </summary> |
26 | | - /// <typeparam name="TLifetime">Lifetime of dependencies</typeparam> |
27 | | - /// <param name="builder">Autofac's <see cref="ContainerBuilder" /></param> |
28 | | - /// <param name="assembly">Assemby to search</param> |
29 | | - internal static void RegisterDependenciesByAssembly<TLifetime>(this ContainerBuilder builder, Assembly assembly) where TLifetime : ILifetime |
30 | | - { |
31 | | - typeof(TLifetime) |
32 | | - .AssignedTypesInAssembly(assembly) |
33 | | - .ForEach(builder.RegisterApplyingLifetime<TLifetime>); |
34 | | - } |
35 | | - |
36 | | - /// <summary> |
37 | | - /// Registers given type according to it's lifetime. Type can be generic or not. |
38 | | - /// </summary> |
39 | | - /// <typeparam name="TLifetime">Lifetime of dependency</typeparam> |
40 | | - /// <param name="builder">Autofac's <see cref="ContainerBuilder" /></param> |
41 | | - /// <param name="typeToRegister">Type to register Autofac Container</param> |
42 | | - internal static void RegisterApplyingLifetime<TLifetime>(this ContainerBuilder builder, Type typeToRegister) where TLifetime : ILifetime |
43 | | - { |
44 | | - List<Type> defaultInterfaces = typeToRegister.GetDefaultInterfaces().ToList(); |
45 | | - |
46 | | - if (typeToRegister.GetTypeInfo().IsGenericTypeDefinition) |
47 | | - { |
48 | | - List<Type> defaultGenerics = defaultInterfaces.Where(t => t.GetTypeInfo().IsGenericType).ToList(); |
49 | | - AddStartableIfPossible(typeToRegister, defaultGenerics); |
50 | | - builder.RegisterGeneric(typeToRegister) |
51 | | - .As(defaultGenerics.ToArray()) |
52 | | - .AsSelf() |
53 | | - .WithPropertyInjection() |
54 | | - .ApplyLifeStyle(typeof(TLifetime)); |
55 | | - } |
56 | | - else |
57 | | - { |
58 | | - List<Type> defaults = defaultInterfaces.Where(t => !t.GetTypeInfo().IsGenericType).ToList(); |
59 | | - AddStartableIfPossible(typeToRegister, defaults); |
60 | | - builder.RegisterType(typeToRegister) |
61 | | - .As(defaults.ToArray()) |
62 | | - .AsSelf() |
63 | | - .WithPropertyInjection() |
64 | | - .ApplyLifeStyle(typeof(TLifetime)); |
65 | | - } |
66 | | - } |
67 | | - |
68 | | - private static void AddStartableIfPossible(Type typeToRegister, ICollection<Type> defaultInterfaces) |
69 | | - { |
70 | | - if (typeToRegister.IsAssignableTo<IStartable>()) |
71 | | - { |
72 | | - defaultInterfaces.Add(typeof(IStartable)); |
73 | | - } |
74 | | - } |
75 | | - } |
| 6 | + public static class AutofacExtensions |
| 7 | + { |
| 8 | + /// <summary> |
| 9 | + /// Helper for anonymouse resolvings <see cref="IocManager.Resolve{T}(object)" /> |
| 10 | + /// </summary> |
| 11 | + /// <param name="this">The this.</param> |
| 12 | + /// <returns></returns> |
| 13 | + internal static IEnumerable<TypedParameter> GetTypedResolvingParameters(this object @this) |
| 14 | + { |
| 15 | + foreach (PropertyInfo propertyInfo in @this.GetType().GetTypeInfo().GetProperties()) |
| 16 | + { |
| 17 | + yield return new TypedParameter(propertyInfo.PropertyType, propertyInfo.GetValue(@this, null)); |
| 18 | + } |
| 19 | + } |
| 20 | + } |
76 | 21 | } |
0 commit comments