-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLocatorExtensions.cs
178 lines (165 loc) · 8.46 KB
/
LocatorExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
using System;
using System.Collections;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using ReactiveUI;
using Splat.ReactiveUIExtensions.Attributes;
using Splat.ReactiveUIExtensions.Helpers;
namespace Splat.ReactiveUIExtensions
{
public static class LocatorExtensions
{
public static void RegisterLazySingleton<TImpl, TAs>(this IMutableDependencyResolver resolver)
where TImpl : TAs
{
RegisterType(resolver, typeof(TImpl).GetTypeInfo(), typeof(TAs).GetTypeInfo(), singleInstance: true);
}
public static void RegisterType<TImpl, TAs>(this IMutableDependencyResolver resolver)
where TImpl : TAs
{
RegisterType(resolver, typeof(TImpl).GetTypeInfo(), typeof(TAs).GetTypeInfo());
}
/// <summary>
/// Registers the views and view models.
/// </summary>
/// <param name="resolver">Resolver.</param>
/// <param name="assembly">The assembly to scan for views and viewmodels.</param>
/// <typeparam name="TBaseViewModelType">The base viewmodel type</typeparam>
public static void RegisterViewsAndViewModels<TBaseViewModel>(this IMutableDependencyResolver resolver, Assembly assembly)
{
RegisterViewsAndViewModels<TBaseViewModel, IViewFor>(resolver, assembly);
}
/// <summary>
/// Registers the views and view models.
/// </summary>
/// <param name="resolver">Resolver.</param>
/// <param name="assembly">The assembly to scan for views and viewmodels.</param>
/// <typeparam name="TBaseViewModelType">The base viewmodel type</typeparam>
/// <typeparam name="TIViewFor">The basetype for views.</typeparam>
public static void RegisterViewsAndViewModels<TBaseViewModel, TIViewFor>(this IMutableDependencyResolver resolver, Assembly assembly)
{
foreach (TypeInfo ti in assembly.DefinedTypes.Where(ti => !ti.IsAbstract && !ti.IsGenericTypeDefinition))
{
RegisterView<TIViewFor>(resolver, ti);
RegisterViewModel<TBaseViewModel>(resolver, ti);
}
}
/// <summary>
/// Registers the views.
/// </summary>
/// <param name="resolver">Resolver.</param>
/// <param name="assembly">The assembly to scan for views.</param>
/// <param name="registerDirectAlso">If set to <c>true</c>, this will register the view on the container as an implementation of TIViewFor.
/// Otherwise, only second level implementations are registered. Eg, if TIViewFor is <c>IViewFor</c>, only <c>IViewFor<object></c> is registered, not IViewFor self.
/// Default is <c>false</c>.</param>
/// <typeparam name="TIViewFor">The basetype for views.</typeparam>
public static void RegisterViews<TIViewFor>(this IMutableDependencyResolver resolver, Assembly assembly, bool registerDirectAlso = false)
{
foreach (TypeInfo ti in assembly.DefinedTypes.Where(ti => !ti.IsAbstract && !ti.IsGenericTypeDefinition))
{
RegisterView<TIViewFor>(resolver, ti, registerDirectAlso);
}
}
/// <summary>
/// Registers the view models.
/// </summary>
/// <param name="resolver">Resolver.</param>
/// <param name="assembly">The assembly to scan for viewmodels.</param>
/// <typeparam name="TBaseViewModelType">The base viewmodel type</typeparam>
public static void RegisterViewModels<TBaseViewModelType>(this IMutableDependencyResolver resolver, Assembly assembly)
{
foreach (TypeInfo ti in assembly.DefinedTypes.Where(ti => !ti.IsAbstract && !ti.IsGenericTypeDefinition))
{
RegisterViewModel<TBaseViewModelType>(resolver, ti);
}
}
/// <summary>
/// Registers a view, if the criteria are met
/// </summary>
/// <param name="resolver">Resolver.</param>
/// <param name="ti">Typeinfo for this view</param>
/// <param name="registerDirectAlso">If set to <c>true</c>, this will register the view on the container as an implementation of TIViewFor.
/// Otherwise, only second level implementations are registered. Eg, if TIViewFor is <c>IViewFor</c>, only <c>IViewFor<object></c> is registered, not IViewFor self.
/// Default is <c>false</c>.</param>
/// <typeparam name="TIViewFor">The basetype for views.</typeparam>
public static void RegisterView<TIViewFor>(this IMutableDependencyResolver resolver, TypeInfo ti, bool registerDirectAlso = false)
{
if (ti.ImplementedInterfaces.Contains(typeof(TIViewFor)))
{
// grab the first _implemented_ interface that also implements IViewFor, this should be the expected IViewFor<>
foreach (Type ivf in ti.ImplementedInterfaces.Where(t =>
t.GetTypeInfo().ImplementedInterfaces.Contains(typeof(TIViewFor))))
{
RegisterType(resolver, ti, ivf);
}
}
}
public static void RegisterViewModel<TBaseViewModelType>(this IMutableDependencyResolver resolver, TypeInfo ti)
{
if (typeof(TBaseViewModelType).IsAssignableFrom(ti))
{
RegisterType(resolver, ti, ti);
RegisterType(resolver, ti, typeof(TBaseViewModelType), ti.FullName);
if (typeof(TBaseViewModelType).IsInterface)
{
foreach (Type vmt in ti.ImplementedInterfaces.Where(t =>
t.GetTypeInfo().ImplementedInterfaces.Contains(typeof(TBaseViewModelType))))
{
RegisterType(resolver, ti, vmt);
}
}
}
}
private static void RegisterType(IMutableDependencyResolver resolver, TypeInfo ti, Type serviceType, string contract = null, bool singleInstance = false)
{
if (string.IsNullOrEmpty(contract))
{
ViewContractAttribute contractSource = ti.GetCustomAttribute<ViewContractAttribute>();
contract = contractSource != null ? contractSource.Contract : string.Empty;
}
Func<object> factory = TypeFactoryCreator(ti);
if (singleInstance
|| ti.GetCustomAttribute<SingleInstanceViewAttribute>() != null
|| ti.GetCustomAttribute<SingleInstanceAttribute>() != null)
{
resolver.RegisterLazySingleton(factory, serviceType, contract);
}
else
{
resolver.Register(factory, serviceType, contract);
}
}
public static Func<TypeInfo, Func<object>> TypeFactoryCreator { get; set; } =
(TypeInfo typeInfo) =>
{
ConstructorInfo constructorInfo = typeInfo.DeclaredConstructors.Where(ci => ci.IsPublic)
.OrderByDescending(ci => ci.GetParameters().Count())
.First();
ParameterInfo[] parameters = constructorInfo.GetParameters();
if (!parameters.Any())
{
return Expression.Lambda<Func<object>>(Expression.New(constructorInfo)).Compile();
}
Func<object> func = () =>
{
return constructorInfo.Invoke(
parameters.Select(p =>
{
if (typeof(string) == p.ParameterType || p.ParameterType.IsPrimitive)
{
return null;//we don't do non-class injections
}
if (typeof(IEnumerable).IsAssignableFrom(p.ParameterType))
{
Type elementType = ReflectionHelpers.GetElementType(p.ParameterType);
return Locator.Current.GetServices(elementType).ToArrayOfType(elementType);
}
return Locator.Current.GetService(p.ParameterType);
})
.ToArray());
};
return func;
};
}
}