Datei: NDOPackage/NdoUIProviderFactory.cs

Last Commit (33e9857)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1 using NDO.UISupport;
2 using System;
3 using System.Collections.Generic;
4 using System.Diagnostics;
5 using System.IO;
6 using System.Linq;
7 using System.Reflection;
8 using System.Text;
9 using System.Threading.Tasks;
10
11 namespace NDOVsPackage
12 {
13 ····internal class NdoUIProviderFactory
14 ····{
15 ········private Dictionary<string, IDbUISupport> uiSupportProviders;
16 ········private static object lockObject = new object();
17 ········private NdoUIProviderFactory()
18 ········{
19 ········}
20
21 ········public static NdoUIProviderFactory Instance = new NdoUIProviderFactory();
22 ········public IDbUISupport this[string name]
23 ········{
24 ············get
25 ············{
26 ················var px = NDOProviderFactory.Instance[""];··// Make sure to fetch the providers first
27
28 ················if (this.uiSupportProviders == null)
29 ················{
30 ····················FetchProviders();
31 ················}
32
33 ················IDbUISupport result;
34 ················uiSupportProviders.TryGetValue(name, out result);
35 ················return result;
36 ············}
37 ········}
38
39 ········private void FetchProviders()
40 ········{
41 ············lock (lockObject)
42 ············{
43 ················if (this.uiSupportProviders == null) // double check
44 ················{
45 ····················this.uiSupportProviders = new Dictionary<string, IDbUISupport>();
46 ····················string path = Path.GetDirectoryName(GetType().Assembly.Location);
47
48 ····················var ifc = typeof(IDbUISupport);
49
50 ····················foreach (var fileName in Directory.GetFiles(path, "*UISupport.dll"))
51 ····················{
52 ························if (fileName.EndsWith("NDO.UISupport.dll"))
53 ····························continue;
54
55 ························try
56 ························{
57 ····························var assembly = Assembly.LoadFrom(fileName);
58 ····························foreach (var type in assembly.ExportedTypes.Where(t => ifc.IsAssignableFrom(t)))
59 ····························{
60 ································var p = (IDbUISupport)Activator.CreateInstance(type);
61 ································this.uiSupportProviders.Add(p.Name, p);
62 ································var provider = NDOProviderFactory.Instance[p.Name];
63 ································if (provider == null)
64 ····································Debug.WriteLine($"No NDO provider for UI provider {p.Name}");
65 ································else
66 ····································p.Initialize(provider);
67 ····························}
68 ························}
69 ························catch (Exception ex)
70 ························{
71 ····························Debug.WriteLine(fileName + ": " + ex.ToString());
72 ························}
73 ····················}
74 ················}
75 ············}
76 ········}
77
78 ········public string[] Keys
79 ········{
80 ············get
81 ············{
82 ················if (this.uiSupportProviders == null)
83 ····················FetchProviders();
84
85 ················return (from s in this.uiSupportProviders.Keys select s).ToArray();
86 ············}
87 ········}
88 ····}
89 }
90 ··}
91 ····}
92 }
93
New Commit (aa458ff)
1 //
2 // Copyright (c) 2002-2022 Mirko Matytschak
3 // (www.netdataobjects.de)
4 //
5 // Author: Mirko Matytschak
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
8 // documentation files (the "Software"), to deal in the Software without restriction, including without limitation
9 // the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
10 // Software, and to permit persons to whom the Software is furnished to do so, subject to the following
11 // conditions:
12
13 // The above copyright notice and this permission notice shall be included in all copies or substantial portions
14 // of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
17 // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
19 // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 // DEALINGS IN THE SOFTWARE.
21
22
23 using NDO.UISupport;
24 using System;
25 using System.Collections.Generic;
26 using System.Diagnostics;
27 using System.IO;
28 using System.Linq;
29 using System.Reflection;
30 using System.Text;
31 using System.Threading.Tasks;
32
33 namespace NDOVsPackage
34 {
35 ····internal class NdoUIProviderFactory
36 ····{
37 ········private Dictionary<string, IDbUISupport> uiSupportProviders;
38 ········private static object lockObject = new object();
39 ········private NdoUIProviderFactory()
40 ········{
41 ········}
42
43 ········public static NdoUIProviderFactory Instance = new NdoUIProviderFactory();
44 ········public IDbUISupport this[string name]
45 ········{
46 ············get
47 ············{
48 ················var px = NDOProviderFactory.Instance[""];··// Make sure to fetch the providers first
49
50 ················if (this.uiSupportProviders == null)
51 ················{
52 ····················FetchProviders();
53 ················}
54
55 ················IDbUISupport result;
56 ················uiSupportProviders.TryGetValue(name, out result);
57 ················return result;
58 ············}
59 ········}
60
61 ········private void FetchProviders()
62 ········{
63 ············lock (lockObject)
64 ············{
65 ················if (this.uiSupportProviders == null) // double check
66 ················{
67 ····················this.uiSupportProviders = new Dictionary<string, IDbUISupport>();
68 ····················string path = Path.GetDirectoryName(GetType().Assembly.Location);
69
70 ····················var ifc = typeof(IDbUISupport);
71
72 ····················foreach (var fileName in Directory.GetFiles(path, "*UISupport.dll"))
73 ····················{
74 ························if (fileName.EndsWith("NDO.UISupport.dll"))
75 ····························continue;
76
77 ························try
78 ························{
79 ····························var assembly = Assembly.LoadFrom(fileName);
80 ····························foreach (var type in assembly.ExportedTypes.Where(t => ifc.IsAssignableFrom(t)))
81 ····························{
82 ································var p = (IDbUISupport)Activator.CreateInstance(type);
83 ································this.uiSupportProviders.Add(p.Name, p);
84 ································var provider = NDOProviderFactory.Instance[p.Name];
85 ································if (provider == null)
86 ····································Debug.WriteLine($"No NDO provider for UI provider {p.Name}");
87 ································else
88 ····································p.Initialize(provider);
89 ····························}
90 ························}
91 ························catch (Exception ex)
92 ························{
93 ····························Debug.WriteLine(fileName + ": " + ex.ToString());
94 ························}
95 ····················}
96 ················}
97 ············}
98 ········}
99
100 ········public string[] Keys
101 ········{
102 ············get
103 ············{
104 ················if (this.uiSupportProviders == null)
105 ····················FetchProviders();
106
107 ················return (from s in this.uiSupportProviders.Keys select s).ToArray();
 
 
 
108 ············}
109 ········}
110 ····}
111 }
112