Mercurial > silverbladetech
comparison Chronosv2/source/Services/WidgetConfigurationService.cs @ 10:443821e55f06
Initial cleaned up add from Codeplex files
author | stevenh7776 stevenhollidge@hotmail.com |
---|---|
date | Tue, 21 Feb 2012 17:25:44 +0700 |
parents | |
children | 741981715d94 |
comparison
equal
deleted
inserted
replaced
9:904a9faadf8b | 10:443821e55f06 |
---|---|
1 using System; | |
2 using System.Collections.Generic; | |
3 using Chronos.Configuration; | |
4 using Chronos.Presentation.Core.Configuration; | |
5 using Chronos.Presentation.Core.Widgets; | |
6 using NLog; | |
7 using nRoute.Components.Composition; | |
8 using nRoute.Services; | |
9 using System.Configuration; | |
10 | |
11 namespace Chronos.Services | |
12 { | |
13 [MapService(typeof(IWidgetConfigurationService), | |
14 InitializationMode = InitializationMode.OnDemand, | |
15 Lifetime = InstanceLifetime.Singleton)] | |
16 public sealed class WidgetConfigurationService | |
17 : IWidgetConfigurationService | |
18 { | |
19 #region · Logger · | |
20 | |
21 private static Logger Logger = LogManager.GetCurrentClassLogger(); | |
22 | |
23 #endregion | |
24 | |
25 #region · SyncObject · | |
26 | |
27 private static readonly object SyncObject = new object(); | |
28 | |
29 #endregion | |
30 | |
31 #region · Constants · | |
32 | |
33 private static readonly string RootConfigFile = "Widgets.config"; | |
34 private static readonly string RootSection = "chronoserp.widgets"; | |
35 | |
36 #endregion | |
37 | |
38 #region · Fields · | |
39 | |
40 private List<IWidget> widgets; | |
41 | |
42 #endregion | |
43 | |
44 #region · Constructors · | |
45 | |
46 public WidgetConfigurationService() | |
47 { | |
48 } | |
49 | |
50 #endregion | |
51 | |
52 #region · Methods · | |
53 | |
54 public IEnumerable<IWidget> GetWidgets() | |
55 { | |
56 lock (SyncObject) | |
57 { | |
58 if (this.widgets == null) | |
59 { | |
60 WidgetsSectionHandler section = ConfigurationManager.GetSection(RootSection) as WidgetsSectionHandler; | |
61 | |
62 if (section != null) | |
63 { | |
64 this.widgets = new List<IWidget>(); | |
65 | |
66 foreach (WidgetConfigurationElement widgetConfig in section.Widgets) | |
67 { | |
68 IWidget widget = this.CreateWidgetDefinition(widgetConfig); | |
69 | |
70 if (widget == null) | |
71 { | |
72 Logger.Debug("Widget not found {0} - {1}", widgetConfig.Id, widgetConfig.Type); | |
73 } | |
74 else | |
75 { | |
76 this.widgets.Add(widget); | |
77 } | |
78 } | |
79 } | |
80 } | |
81 } | |
82 | |
83 return this.widgets; | |
84 } | |
85 | |
86 public T GetWidgetConfigurationSection<T>(string sectionName) | |
87 where T: ConfigurationSection | |
88 { | |
89 throw new NotImplementedException(); | |
90 } | |
91 | |
92 #endregion | |
93 | |
94 #region · Private Methods · | |
95 | |
96 private IWidget CreateWidgetDefinition(WidgetConfigurationElement widgetConfig) | |
97 { | |
98 IWidget widget = null; | |
99 | |
100 try | |
101 { | |
102 widget = Activator.CreateInstance(Type.GetType(widgetConfig.Type)) as IWidget; | |
103 } | |
104 catch (Exception ex) | |
105 { | |
106 Logger.ErrorException | |
107 ( | |
108 String.Format("Widget not found {0} - {1}", widgetConfig.Id, widgetConfig.Type), | |
109 ex | |
110 ); | |
111 } | |
112 | |
113 return widget; | |
114 } | |
115 | |
116 #endregion | |
117 } | |
118 } |