Datei: UISupport/NDO.UISupport/DbUISupportBase.cs

Last Commit (f4949d4)
1 using NDOInterfaces;
2 using System;
3 using System.Collections.Generic;
4 using System.Data;
 
5 using System.Linq;
 
6 using System.Text;
7
8 namespace NDO.UISupport
9 {
10 ····public abstract class DbUISupportBase : IDbUISupport
11 ····{
12 ········private IProvider provider;
13
14 ········/// <summary>
15 ········/// Gets the NDO database provider instance
16 ········/// </summary>
17 ········public IProvider Provider => this.provider;
18
19 ········/// <summary>
20 ········/// Name of the provider
21 ········/// </summary>
22 ········/// <remarks>
23 ········/// Note that this property must be overriden
24 ········/// </remarks>
25 ········public virtual string Name => throw new NotImplementedException();
26
27 ········/// <summary>
28 ········/// Initializes the DbUISupportBase instance
29 ········/// </summary>
30 ········/// <param name="provider"></param>
31 ········public virtual void Initialize(IProvider provider)
32 ········{
33 ············this.provider = provider;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34 ········}
 
 
 
 
 
35
36 ········/// <summary>
37 ········/// See <see cref="IDbUISupport">IProvider interface</see>.
38 ········/// </summary>
39 ········public virtual NdoDialogResult ShowConnectionDialog( ref string connectionString )
40 ········{
41 ············GenericConnectionDialog dlg = new GenericConnectionDialog( connectionString );
42 ············NdoDialogResult result = (NdoDialogResult) dlg.ShowDialog();
43 ············if (result != NdoDialogResult.Cancel)
44 ················connectionString = dlg.ConnectionString;
45 ············return result;
46 ········}
47
48 ········/// <summary>
49 ········/// See <see cref="IDbUISupport">IProvider interface</see>.
50 ········/// </summary>
51 ········/// <remarks>
52 ········/// The parameter is an object since overridden implementations may use other data structures.
53 ········/// </remarks>
54 ········public virtual NdoDialogResult ShowCreateDbDialog( ref object necessaryData )
55 ········{
56 ············DefaultCreateDbDialog dlg = new DefaultCreateDbDialog( this, (NDOCreateDbParameter) necessaryData );
57 ············NdoDialogResult result = (NdoDialogResult) dlg.ShowDialog();
58 ············if (result != NdoDialogResult.Cancel)
59 ················necessaryData = dlg.NecessaryData;
60 ············return result;
61 ········}
62
63 ········/// <summary>
64 ········/// See <see cref="IDbUISupport">IProvider interface</see>.
65 ········/// </summary>
66 ········public virtual string CreateDatabase( object necessaryData )
67 ········{
68 ············NDOCreateDbParameter par = necessaryData as NDOCreateDbParameter;
69 ············if (par == null)
70 ················throw new ArgumentException( $"{nameof( DbUISupportBase )}: parameter type {necessaryData.GetType().FullName} is wrong.", nameof( necessaryData ) );
71 ············try
72 ············{
73 return this. provider. CreateDatabase( par. DatabaseName, par. ConnectionString, null ) ;
74 ············}
75 ············catch (Exception ex)
76 ············{
77 ················throw new NDOException( 19, "Error while attempting to create a database: Exception Type: " + ex.GetType().Name + " Message: " + ex.Message );
78 ············}
79 ········}
80 ····}
81 }
82
New Commit (947ce88)
1 using NDOInterfaces;
2 using System;
3 using System.Collections.Generic;
4 using System.Data;
5 using System.IO;
6 using System.Linq;
7 using System.Reflection;
8 using System.Text;
9
10 namespace NDO.UISupport
11 {
12 ····public abstract class DbUISupportBase : IDbUISupport
13 ····{
14 ········private IProvider provider;
15
16 ········/// <summary>
17 ········/// Gets the NDO database provider instance
18 ········/// </summary>
19 ········public IProvider Provider => this.provider;
20
21 ········/// <summary>
22 ········/// Name of the provider
23 ········/// </summary>
24 ········/// <remarks>
25 ········/// Note that this property must be overriden
26 ········/// </remarks>
27 ········public virtual string Name => throw new NotImplementedException();
28
29 ········/// <summary>
30 ········/// Initializes the DbUISupportBase instance
31 ········/// </summary>
32 ········/// <param name="provider"></param>
33 ········public virtual void Initialize(IProvider provider)
34 ········{
35 ············this.provider = provider;
36 ········}
37
38 ········/// <summary>
39 ········/// Tries to load a provider, if necessary
40 ········/// </summary>
41 ········protected IProvider EnsureProvider()
42 ········{
43 ············if (this.provider == null)
44 ············{
45 ················var dir = Path.GetDirectoryName( GetType().Assembly.Location );
46 ················var path = Path.Combine( dir, $"NDO.{Name}.dll" );
47 ················if (File.Exists(path))
48 ················{
49 ····················var asm = Assembly.LoadFrom( path );
50 ····················if (asm == null)
51 ························throw new Exception( $"Can't load provider DLL '{path}'" );
52 ····················var providerType = asm.GetExportedTypes()
53 ························.FirstOrDefault( t => t.GetInterface( "IProvider" ) != null );
54
55 ····················return this.provider = (IProvider) Activator.CreateInstance(providerType);
56 ················}
57 ················else
58 ················{
59 ····················throw new Exception( $"Can't find provider DLL '{path}'" );
60 ················}
61 ············}
62 ············else
63 ············{
64 ················return this.provider;
65 ············}
66 ········}
67
68 ········/// <summary>
69 ········/// See <see cref="IDbUISupport">IProvider interface</see>.
70 ········/// </summary>
71 ········public virtual NdoDialogResult ShowConnectionDialog( ref string connectionString )
72 ········{
73 ············GenericConnectionDialog dlg = new GenericConnectionDialog( connectionString );
74 ············NdoDialogResult result = (NdoDialogResult) dlg.ShowDialog();
75 ············if (result != NdoDialogResult.Cancel)
76 ················connectionString = dlg.ConnectionString;
77 ············return result;
78 ········}
79
80 ········/// <summary>
81 ········/// See <see cref="IDbUISupport">IProvider interface</see>.
82 ········/// </summary>
83 ········/// <remarks>
84 ········/// The parameter is an object since overridden implementations may use other data structures.
85 ········/// </remarks>
86 ········public virtual NdoDialogResult ShowCreateDbDialog( ref object necessaryData )
87 ········{
88 ············DefaultCreateDbDialog dlg = new DefaultCreateDbDialog( this, (NDOCreateDbParameter) necessaryData );
89 ············NdoDialogResult result = (NdoDialogResult) dlg.ShowDialog();
90 ············if (result != NdoDialogResult.Cancel)
91 ················necessaryData = dlg.NecessaryData;
92 ············return result;
93 ········}
94
95 ········/// <summary>
96 ········/// See <see cref="IDbUISupport">IProvider interface</see>.
97 ········/// </summary>
98 ········public virtual string CreateDatabase( object necessaryData )
99 ········{
100 ············NDOCreateDbParameter par = necessaryData as NDOCreateDbParameter;
101 ············if (par == null)
102 ················throw new ArgumentException( $"{nameof( DbUISupportBase )}: parameter type {necessaryData.GetType().FullName} is wrong.", nameof( necessaryData ) );
103 ············try
104 ············{
105 return EnsureProvider( ) . CreateDatabase( par. DatabaseName, par. ConnectionString, null ) ;
106 ············}
107 ············catch (Exception ex)
108 ············{
109 ················throw new NDOException( 19, "Error while attempting to create a database: Exception Type: " + ex.GetType().Name + " Message: " + ex.Message );
110 ············}
111 ········}
112 ····}
113 }
114