/
Context.cs
183 lines (147 loc) · 6.54 KB
/
Context.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
179
180
181
182
183
using System;
using System.Collections.Generic;
using System.Linq;
using Glass.Mapper.CastleWindsor;
using Glass.Mapper.Configuration;
using Glass.Mapper.Pipelines.ConfigurationResolver;
using Glass.Mapper.Pipelines.ObjectConstruction;
using Glass.Mapper.Pipelines.ObjectConstruction.Tasks.CreateConcrete;
using Glass.Mapper.Pipelines.ObjectConstruction.Tasks.CreateInterface;
using Glass.Mapper.Pipelines.TypeResolver;
using Glass.Mapper.Pipelines.TypeResolver.Tasks.StandardResolver;
using Glass.Mapper.Pipelines.DataMapperResolver;
namespace Glass.Mapper
{
/// <summary>
/// The context contains the configuration of Glass.Mapper
/// </summary>
public class Context
{
public const string DefaultContextName = "Default";
#region STATICS
public static IDependencyResolverFactory ResolverFactory { get; set; }
static Context()
{
ResolverFactory = new CastleDependencyResolverFactory();
Contexts = new Dictionary<string, Context>();
}
/// <summary>
/// The default Context. Used by services if no Context is specified.
/// </summary>
public static Context Default { get; private set; }
/// <summary>
/// Contains the list of Contexts currently loaded.
/// </summary>
public static IDictionary<string, Context> Contexts { get; private set; }
/// <summary>
/// Creates a Context and creates it as the default Context. This is assigned to the Default static property.
/// </summary>
public static Context Create(IGlassConfiguration glassConfig)
{
return Context.Create(glassConfig, DefaultContextName, true);
}
/// <summary>
/// Creates a new context and adds it to the Contexts dictionary.
/// </summary>
/// <param name="contextName">The context name, used as the key in the Contexts dictionary.</param>
/// <param name="isDefault">Indicates if this is the default context. If it is the context is assigned to the Default static property.</param>
/// <returns></returns>
public static Context Create(IGlassConfiguration glassConfig, string contextName, bool isDefault = false)
{
if (glassConfig == null)
throw new NullReferenceException("No GlassConfig set. Set the static property Context.GlassConfig");
var context = new Context();
context.DependencyResolver = ResolverFactory.GetResolver();
context.DependencyResolver.Load(contextName, glassConfig);
context.ObjectCacheConfiguration = context.DependencyResolver.TryResolve<AbstractObjectCacheConfiguration>();
Contexts[contextName] = context;
if (isDefault)
Default = context;
return context;
}
/// <summary>
/// Clears all static and default contexts
/// </summary>
public static void Clear()
{
Default = null;
Contexts = new Dictionary<string, Context>();
}
#endregion
public AbstractObjectCacheConfiguration ObjectCacheConfiguration { get; private set; }
/// <summary>
/// List of the type configurations loaded by this context
/// </summary>
public IDictionary<Type, AbstractTypeConfiguration> TypeConfigurations { get; private set; }
/// <summary>
/// The dependency resolver used by services using the context
/// </summary>
public IDependencyResolver DependencyResolver { get; set; }
private Context()
{
TypeConfigurations = new Dictionary<Type, AbstractTypeConfiguration>();
}
/// <summary>
/// Gets a type configuration based on type
/// </summary>
/// <param name="type">The configuration </param>
/// <returns></returns>
public AbstractTypeConfiguration this[Type type]
{
get
{
if (TypeConfigurations.ContainsKey(type))
return TypeConfigurations[type];
else
return null;
}
}
/// <param name="loaders">The list of configuration loaders to load into the context.</param>
public void Load(params IConfigurationLoader[] loaders)
{
if (loaders.Any())
{
var typeConfigurations = loaders
.Select(loader => loader.Load()).Aggregate((x, y) => x.Union(y));
foreach (var typeConfig in typeConfigurations)
{
ProcessProperties(typeConfig.Properties);
TypeConfigurations.Add(typeConfig.Type, typeConfig);
}
}
}
private void ProcessProperties(IEnumerable<AbstractPropertyConfiguration> properties )
{
DataMapperResolver runner = new DataMapperResolver(DependencyResolver.ResolveAll<IDataMapperResolverTask>());
foreach(var property in properties)
{
DataMapperResolverArgs args = new DataMapperResolverArgs(this, property);
args.PropertyConfiguration = property;
args.DataMappers = DependencyResolver.ResolveAll<AbstractDataMapper>();
runner.Run(args);
if(args.Result == null)
{
throw new NullReferenceException(
"Could not find data mapper for property {0} on type {1}"
.Formatted(property.PropertyInfo.Name,property.PropertyInfo.ReflectedType.FullName));
}
property.Mapper = args.Result;
}
}
public AbstractTypeConfiguration GetTypeConfiguration(object obj)
{
var type = obj.GetType();
var config = TypeConfigurations.ContainsKey(type) ? TypeConfigurations[type] : null;
if (config != null) return config;
//check base type encase of proxy
config = TypeConfigurations.ContainsKey(type.BaseType) ? TypeConfigurations[type.BaseType] : null;
if (config != null) return config;
//check interfaces encase this is an interface proxy
string name = type.Name;
var interfaceType = type.GetInterfaces().FirstOrDefault(x => name.Contains(x.Name));
if (interfaceType != null)
config = TypeConfigurations.ContainsKey(interfaceType) ? TypeConfigurations[interfaceType] : null;
return config;
}
}
}