Revisão | a3ce170d1e898fecf17babb0d2be480b6753bebe (tree) |
---|---|
Hora | 2022-11-16 00:36:14 |
Autor | yoshy <yoshy.org.bitbucket@gz.j...> |
Commiter | yoshy |
[MOD] DBコネクションラッパーを抽象化し、単純実装したクラスとそのファクトリを追加(各RDMBS固有の機能が必要な場合はアプリ側で実装する)
@@ -49,7 +49,8 @@ | ||
49 | 49 | <Compile Include="OuterEdge\Repository\Db\Tx\AbstractConnectionFactory.cs" /> |
50 | 50 | <Compile Include="OuterEdge\Repository\Db\Tx\ConnectoinFactoryMap.cs" /> |
51 | 51 | <Compile Include="OuterEdge\Repository\Db\Tx\DataSource.cs" /> |
52 | - <Compile Include="OuterEdge\Repository\Db\Tx\DbConnectionWrapper.cs" /> | |
52 | + <Compile Include="OuterEdge\Repository\Db\Tx\AbstractDbConnectionWrapper.cs" /> | |
53 | + <Compile Include="OuterEdge\Repository\Db\Tx\DbConnectionHelper.cs" /> | |
53 | 54 | <Compile Include="OuterEdge\Repository\Db\Tx\DbTransactionWrapper.cs" /> |
54 | 55 | <Compile Include="OuterEdge\Repository\Db\Tx\IConnectionFactory.cs" /> |
55 | 56 | <Compile Include="OuterEdge\Repository\Db\Tx\IConnectionFactoryMap.cs" /> |
@@ -58,6 +59,7 @@ | ||
58 | 59 | <Compile Include="OuterEdge\Repository\Db\Tx\IDbTransactionWrapper.cs" /> |
59 | 60 | <Compile Include="OuterEdge\Repository\Db\Tx\ITransactionManager.cs" /> |
60 | 61 | <Compile Include="OuterEdge\Repository\Db\Tx\SimpleConnectionFactory.cs" /> |
62 | + <Compile Include="OuterEdge\Repository\Db\Tx\SimpleDbConnectionWrapper.cs" /> | |
61 | 63 | <Compile Include="OuterEdge\Repository\Db\Tx\TransactionManager.cs" /> |
62 | 64 | <Compile Include="Properties\AssemblyInfo.cs" /> |
63 | 65 | </ItemGroup> |
@@ -5,12 +5,12 @@ using System.Text; | ||
5 | 5 | |
6 | 6 | namespace CleanAuLait48.OuterEdge.Repository.Db.Tx |
7 | 7 | { |
8 | - public class DbConnectionWrapper : IDbConnectionWrapper | |
8 | + public abstract class AbstractDbConnectionWrapper : IDbConnectionWrapper | |
9 | 9 | { |
10 | 10 | private const string MANAGED_ERROR = "The transaction is managed by the transaction manager. So, we can't "; |
11 | 11 | |
12 | - private readonly IDbConnection conn; | |
13 | - private readonly string name; | |
12 | + protected readonly IDbConnection conn; | |
13 | + protected readonly string name; | |
14 | 14 | |
15 | 15 | public string Database => this.conn.Database; |
16 | 16 |
@@ -22,7 +22,7 @@ namespace CleanAuLait48.OuterEdge.Repository.Db.Tx | ||
22 | 22 | |
23 | 23 | public IDbConnection GetRawConnection() => this.conn; |
24 | 24 | |
25 | - public DbConnectionWrapper(IDbConnection conn, string name) | |
25 | + public AbstractDbConnectionWrapper(IDbConnection conn, string name) | |
26 | 26 | { |
27 | 27 | this.conn = conn; |
28 | 28 | this.name = name; |
@@ -81,21 +81,17 @@ namespace CleanAuLait48.OuterEdge.Repository.Db.Tx | ||
81 | 81 | return buf.ToString(); |
82 | 82 | } |
83 | 83 | |
84 | - public static IDbConnection GetRawConnection(IDbConnection conn) | |
84 | + public abstract void TestConnection(); | |
85 | + | |
86 | + protected void ExecuteTestQuery(string query) | |
85 | 87 | { |
86 | - if (conn is IDbConnectionWrapper connWrapper) | |
88 | + using (IDbCommand cmd = this.GetRawConnection().CreateCommand()) | |
87 | 89 | { |
88 | - conn = connWrapper.GetRawConnection(); | |
90 | + cmd.CommandText = query; | |
91 | + _ = cmd.ExecuteNonQuery(); | |
89 | 92 | } |
90 | - | |
91 | - return conn; | |
92 | 93 | } |
93 | -#if false | |
94 | - public static IDatabase CreateDatabaseFromRawConnection(IDbConnection conn) | |
95 | - { | |
96 | - return new Database((DbConnection)GetRawConnection(conn)); | |
97 | - } | |
98 | -#endif | |
94 | + | |
99 | 95 | } |
100 | 96 | } |
101 | 97 | |
\ No newline at end of file |
@@ -0,0 +1,24 @@ | ||
1 | +using System.Data; | |
2 | + | |
3 | +namespace CleanAuLait48.OuterEdge.Repository.Db.Tx | |
4 | +{ | |
5 | + public static class DbConnectionHelper | |
6 | + { | |
7 | + public static IDbConnection GetRawConnection(IDbConnection conn) | |
8 | + { | |
9 | + if (conn is IDbConnectionWrapper connWrapper) | |
10 | + { | |
11 | + conn = connWrapper.GetRawConnection(); | |
12 | + } | |
13 | + | |
14 | + return conn; | |
15 | + } | |
16 | + | |
17 | +#if false | |
18 | + public static IDatabase CreateDatabaseFromRawConnection(IDbConnection conn) | |
19 | + { | |
20 | + return new Database((DbConnection)GetRawConnection(conn)); | |
21 | + } | |
22 | +#endif | |
23 | + } | |
24 | +} |
@@ -5,5 +5,7 @@ namespace CleanAuLait48.OuterEdge.Repository.Db.Tx | ||
5 | 5 | public interface IDbConnectionWrapper : IDbConnection |
6 | 6 | { |
7 | 7 | IDbConnection GetRawConnection(); |
8 | + | |
9 | + void TestConnection(); | |
8 | 10 | } |
9 | 11 | } |
\ No newline at end of file |
@@ -1,11 +1,23 @@ | ||
1 | -using System.Data.Common; | |
1 | +using System.Data; | |
2 | +using System.Data.Common; | |
2 | 3 | |
3 | 4 | namespace CleanAuLait48.OuterEdge.Repository.Db.Tx |
4 | 5 | { |
5 | 6 | public class SimpleConnectionFactory : AbstractConnectionFactory |
6 | 7 | { |
7 | - public SimpleConnectionFactory(DbProviderFactory factory, string connStr) : base(factory, connStr) | |
8 | + public SimpleConnectionFactory(DbProviderFactory factory, string connStr) : | |
9 | + base(factory, connStr) | |
8 | 10 | { |
9 | 11 | } |
12 | + | |
13 | + public SimpleConnectionFactory(DbProviderFactory factory, string connStr, string name) : | |
14 | + base(factory, connStr, name) | |
15 | + { | |
16 | + } | |
17 | + | |
18 | + public override IDbConnectionWrapper CreateWrapper(IDbConnection conn, string name) | |
19 | + { | |
20 | + return new SimpleDbConnectionWrapper(conn, this.name); | |
21 | + } | |
10 | 22 | } |
11 | 23 | } |
@@ -0,0 +1,17 @@ | ||
1 | +using System; | |
2 | +using System.Data; | |
3 | + | |
4 | +namespace CleanAuLait48.OuterEdge.Repository.Db.Tx | |
5 | +{ | |
6 | + internal class SimpleDbConnectionWrapper : AbstractDbConnectionWrapper | |
7 | + { | |
8 | + public SimpleDbConnectionWrapper(IDbConnection conn, string name) : base(conn, name) | |
9 | + { | |
10 | + } | |
11 | + | |
12 | + public override void TestConnection() | |
13 | + { | |
14 | + throw new NotSupportedException(); | |
15 | + } | |
16 | + } | |
17 | +} |