forked from ucsbfinaid/Financial-Aid-Estimator-.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CostOfAttendanceEstimatorConfigurationManager.cs
122 lines (104 loc) · 5.21 KB
/
CostOfAttendanceEstimatorConfigurationManager.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
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Web.Hosting;
namespace Ucsb.Sa.FinAid.AidEstimation.Utility
{
/// <summary>
/// Constructs <see cref="CostOfAttendanceEstimator"/>s using the paths to XML files specified within
/// the appSettings configuration section
/// </summary>
public static class CostOfAttendanceEstimatorConfigurationManager
{
/// <summary>
/// Template used when locating a source path within the appSettings configuration section.
/// The placeholder "{key}" in this string will be replaced with the key value provided
/// to <see cref="GetCostOfAttendanceEstimator"/> method below
/// </summary>
public static string AppSettingKeyTemplate { get; set; }
private const string DefaultAppSettingsKeyTemplate = "AidEstimation.Constants.{key}";
private const string KeyPlaceholder = "{key}";
private const string RelativePathPlaceholder = "~/";
private static readonly Dictionary<string, CostOfAttendanceEstimator> _cache = new Dictionary<string, CostOfAttendanceEstimator>();
static CostOfAttendanceEstimatorConfigurationManager()
{
AppSettingKeyTemplate = DefaultAppSettingsKeyTemplate;
}
/// <summary>
/// Constructs <see cref="CostOfAttendanceEstimator"/>s using the paths to XML files specified within
/// the appSettings configuration section. By default, this class will search for the source
/// paths in appSettings with keys set to "AidEstimation.Constants.{key}" where "{key}"
/// is replaced with the key being passed into this method.
///
/// For example, if "1011" was passed as the key into this method, the following appSettings value would
/// be used:
///
/// <add key="AidEstimation.Constants.1011" value="~/Constants/AidEstimationConstants.1011.xml"/>
///
/// To reduce the number of times files are read, constructed <see cref="CostOfAttendanceEstimator"/>s are cached
/// for the duration of this <see cref="CostOfAttendanceEstimatorConfigurationManager"/>'s lifetime
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static CostOfAttendanceEstimator GetCostOfAttendanceEstimator(string key)
{
if (String.IsNullOrEmpty(key))
{
throw new ArgumentException("No Cost of Attendance Estimator key provided");
}
if (_cache.ContainsKey(key))
{
return _cache[key];
}
if (String.IsNullOrEmpty(AppSettingKeyTemplate))
{
throw new ArgumentException("No appSettings Cost Of Attendance Estimator key template was specified");
}
string appSettingsKey = AppSettingKeyTemplate.Replace(KeyPlaceholder, key);
string xmlSourcePath = ConfigurationManager.AppSettings[appSettingsKey];
if (String.IsNullOrEmpty(xmlSourcePath))
{
throw new ArgumentException("No source path was specified for the Cost of Attendance Estimator in appSettings");
}
// If a relative web path is used, resolve the application's physical path
if (xmlSourcePath.StartsWith(RelativePathPlaceholder))
{
xmlSourcePath = xmlSourcePath.Replace(RelativePathPlaceholder, HostingEnvironment.ApplicationPhysicalPath);
}
CostOfAttendanceEstimatorFactory factory = new CostOfAttendanceEstimatorFactory(xmlSourcePath);
CostOfAttendanceEstimator estimator = factory.GetCostOfAttendanceEstimator();
_cache[key] = estimator;
return estimator;
}
/// <summary>
/// Overload version that constructs <see cref="EfcCalculator"/>s using the paths to XML files passed.
/// </summary>
/// <param name="key"></param>
/// <param name="xmlSourcePath"></param>
/// <returns></returns>
public static CostOfAttendanceEstimator GetCostOfAttendanceEstimator(string key, string xmlSourcePath)
{
if (String.IsNullOrEmpty(key))
{
throw new ArgumentException("No Cost of Attendance Estimator key provided");
}
if (_cache.ContainsKey(key))
{
return _cache[key];
}
if (String.IsNullOrEmpty(xmlSourcePath))
{
throw new ArgumentException("No source path was specified for the Cost of Attendance Estimator in appSettings");
}
// If a relative web path is used, resolve the application's physical path
if (xmlSourcePath.StartsWith(RelativePathPlaceholder))
{
xmlSourcePath = xmlSourcePath.Replace(RelativePathPlaceholder, HostingEnvironment.ApplicationPhysicalPath);
}
CostOfAttendanceEstimatorFactory factory = new CostOfAttendanceEstimatorFactory(xmlSourcePath);
CostOfAttendanceEstimator estimator = factory.GetCostOfAttendanceEstimator();
_cache[key] = estimator;
return estimator;
}
}
}