Datei: NDOPackage/NdoUIProviderFactory.cs

Last Commit (946ad0e)
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 NETDataObjects. 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
New 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