-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContainerConfigFactory.cs
More file actions
237 lines (210 loc) · 10.1 KB
/
ContainerConfigFactory.cs
File metadata and controls
237 lines (210 loc) · 10.1 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*************************************************************************************************
*
* THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
* KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
* PARTICULAR PURPOSE.
*
*************************************************************************************************/
using System;
using System.Configuration;
using System.Linq;
namespace Figaro.Configuration
{
/// <summary>
/// Factory object for creating new <see cref="ContainerConfig"/> instances from configuration.
/// </summary>
public class ContainerConfigFactory
{
/// <summary>
/// Creates a new <see cref="ContainerConfig"/> instance using the <see cref="DefaultContainerElement"/> set at the section root level.
/// </summary>
/// <returns>A new <see cref="ContainerConfig"/> instance, or <c>null</c> if it doesn't exist.</returns>
public static ContainerConfig Create()
{
FigaroSection section;
try
{
section = (FigaroSection)ConfigurationManager.GetSection("Figaro");
}
catch (Exception)
{
return null;
}
return section?.DefaultContainerSettings == null ? null : Create(section.DefaultContainerSettings);
}
/// <summary>
/// Creates a new <see cref="ContainerConfig"/> instance from configuration.
/// </summary>
/// <param name="element">The configuration element.</param>
/// <returns>A new <see cref="ContainerConfig"/> instance.</returns>
public static ContainerConfig Create(DefaultContainerElement element)
{
var cfg = new ContainerConfig
{
ConfigurationName = "Default Container",
AllowCreate = element.AllowCreate,
AllowValidation = element.AllowValidation,
Checksum = element.Checksum,
CompressionEnabled = element.Compression,
ExclusiveCreate = element.ExclusiveCreate,
ReadOnly = element.ReadOnly,
#if CDS || TDS || HA
Threaded = element.Threaded,
NoMMap = element.NoMMap,
Encrypted = element.Encrypted,
#elif TDS || HA
MultiVersion = element.Multiversion,
ReadUncommitted = element.ReadUncommitted,
Transactional = element.Transactional,
TransactionNotDurable = element.TransactionNotDurable
#endif
};
#if TDS || HA
#endif
if (!string.IsNullOrEmpty(element.ContainerType))
cfg.ContainerType =
(XmlContainerType) Enum.Parse(typeof (XmlContainerType), element.ContainerType, true);
if (!string.IsNullOrEmpty(element.IndexNodes))
cfg.IndexNodes = (ConfigurationState)Enum.Parse(typeof(ConfigurationState),element.IndexNodes,true);
if (element.PageSize > 0)
cfg.PageSize = element.PageSize;
if (element.SequenceIncrement >0)
cfg.SequenceIncrement = element.SequenceIncrement;
if (!string.IsNullOrEmpty(element.Statistics))
cfg.Statistics = (ConfigurationState)Enum.Parse(typeof (ConfigurationState), element.Statistics, true);
return cfg;
}
/// <summary>
/// Creates a new <see cref="ContainerConfig"/> instance from default container settings
/// specified in the named <see cref="ManagerElement"/>.
/// </summary>
/// <param name="managerName">The named <see cref="ManagerElement"/> instance.</param>
/// <returns>A new <see cref="ContainerConfig"/> instance, or <c>null</c> if no default container settings
/// exist for the manager.</returns>
public static ContainerConfig Create(string managerName)
{
if (string.IsNullOrEmpty(managerName)) return null;
FigaroSection section;
try
{
section = (FigaroSection)ConfigurationManager.GetSection("Figaro");
}
catch (Exception)
{
return null;
}
if (section == null) return null;
if (section.Managers == null || section.Managers.Count == 0) return null;
var managerElement =
section.Managers.Cast<ManagerElement>().FirstOrDefault(manager => manager.Name.Equals(managerName));
if (managerElement == null) return null;
// if a default container setting exists for the manager, use it first.
// if a default container setting exists for the section, use it second.
// if neither exist, return null.
if (managerElement.DefaultContainerSettings != null)
{
return Create(managerElement.DefaultContainerSettings);
}
return section.DefaultContainerSettings != null ? Create(section.DefaultContainerSettings) : null;
}
/// <summary>
/// Creates a new <see cref="ContainerConfig"/> instance from the <see cref="ManagerElement"/> default container settings.
/// </summary>
/// <param name="managerName">The named <see cref="ManagerElement"/> instance.</param>
/// <returns>A new <see cref="ContainerConfig"/> instance, or <c>null</c> if no instance exists.</returns>
public static ContainerConfig CreateDefault(string managerName)
{
if (string.IsNullOrEmpty(managerName)) return null;
FigaroSection section;
try
{
section = (FigaroSection)ConfigurationManager.GetSection("Figaro");
}
catch (Exception)
{
return null;
}
if (section == null) return null;
if (section.Managers == null || section.Managers.Count == 0) return null;
ManagerElement managerElement =
section.Managers.Cast<ManagerElement>().FirstOrDefault(manager => manager.Name.Equals(managerName));
if (managerElement == null) return null;
return managerElement.DefaultContainerSettings == null
? null
: Create(managerElement.DefaultContainerSettings);
}
/// <summary>
/// Creates a new <see cref="ContainerConfig"/> instance from configuration.
/// </summary>
/// <param name="managerName">The named <see cref="ManagerElement"/> instance.</param>
/// <param name="containerName">The named Container element.</param>
/// <returns>A new <see cref="ContainerConfig"/> instance, or <c>null</c> if it doesn't exist.</returns>
public static ContainerConfig Create(string managerName , string containerName)
{
if (string.IsNullOrEmpty(managerName)) return null;
FigaroSection section;
try
{
section = (FigaroSection)ConfigurationManager.GetSection("Figaro");
}
catch (Exception)
{
return null;
}
if (section == null) return null;
if (section.Managers == null || section.Managers.Count == 0 ) return null;
var managerElement =
section.Managers.Cast<ManagerElement>().FirstOrDefault(manager => manager.Name.Equals(managerName));
if (managerElement == null) return null;
if (managerElement.Containers == null || managerElement.Containers.Count == 0) return null;
var containerElement =
managerElement.Containers.Cast<ContainerElement>().FirstOrDefault(
element => element.Name.Equals(containerName));
return containerElement == null ? null : Create(containerElement);
}
/// <summary>
/// Creates a new <see cref="ContainerConfig"/> instance from configuration.
/// </summary>
/// <param name="element">The configuration element.</param>
/// <returns>A new <see cref="ContainerConfig"/> instance.</returns>
public static ContainerConfig Create(ContainerElement element)
{
var cfg = new ContainerConfig
{
ConfigurationName = element.Name,
Path = element.Path,
AllowCreate = element.AllowCreate,
AllowValidation = element.AllowValidation,
Checksum = element.Checksum,
CompressionEnabled = element.Compression,
ExclusiveCreate = element.ExclusiveCreate,
ReadOnly = element.ReadOnly,
#if CDS || TDS || HA
NoMMap = element.NoMMap,
Threaded = element.Threaded,
Encrypted = element.Encrypted,
#elif TDS || HA
MultiVersion = element.Multiversion,
ReadUncommitted = element.ReadUncommitted,
Transactional = element.Transactional,
TransactionNotDurable = element.TransactionNotDurable
#endif
};
#if TDS || HA
#endif
if (!string.IsNullOrEmpty(element.ContainerType))
cfg.ContainerType =
(XmlContainerType)Enum.Parse(typeof(XmlContainerType), element.ContainerType, true);
if (!string.IsNullOrEmpty(element.IndexNodes))
cfg.IndexNodes = (ConfigurationState)Enum.Parse(typeof(ConfigurationState), element.IndexNodes, true);
if (element.PageSize > 0)
cfg.PageSize = element.PageSize;
if (element.SequenceIncrement > 0)
cfg.SequenceIncrement = element.SequenceIncrement;
if (!string.IsNullOrEmpty(element.Statistics))
cfg.Statistics = (ConfigurationState)Enum.Parse(typeof(ConfigurationState), element.Statistics, true);
return cfg;
}
}
}