Skip to content

Commit 83a896d

Browse files
committed
Changes to SQLHelper to simplify it a bit. Also DI friendly now.
1 parent 9109c79 commit 83a896d

File tree

9 files changed

+119
-126
lines changed

9 files changed

+119
-126
lines changed

SQLHelper.SpeedTests/Tests/AddQuery.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
using BenchmarkDotNet.Attributes;
2+
using BigBook;
23
using Microsoft.Extensions.Configuration;
34
using Microsoft.Extensions.DependencyInjection;
5+
using Microsoft.Extensions.ObjectPool;
46
using SQLHelperDB.Registration;
57
using System.Collections.Generic;
68
using System.Data;
7-
using System.Data.SqlClient;
89
using System.Reflection;
10+
using System.Text;
911

1012
namespace SQLHelperDB.SpeedTests.Tests
1113
{
@@ -26,7 +28,7 @@ public void Setup()
2628
.AddAssembly(typeof(Program).GetTypeInfo().Assembly)
2729
.RegisterSQLHelper()
2830
.Build();
29-
Helper = new SQLHelper(Canister.Builder.Bootstrapper.Resolve<IConfiguration>(), SqlClientFactory.Instance);
31+
Helper = new SQLHelper(Canister.Builder.Bootstrapper.Resolve<ObjectPool<StringBuilder>>(), Canister.Builder.Bootstrapper.Resolve<DynamoFactory>(), Canister.Builder.Bootstrapper.Resolve<IConfiguration>());
3032
}
3133
}
3234
}

SQLHelper.SpeedTests/Tests/MassInsert.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
using BenchmarkDotNet.Attributes;
2+
using BigBook;
23
using Microsoft.Extensions.Configuration;
34
using Microsoft.Extensions.DependencyInjection;
5+
using Microsoft.Extensions.ObjectPool;
46
using SQLHelperDB.ExtensionMethods;
57
using SQLHelperDB.Registration;
68
using System;
79
using System.Collections.Generic;
810
using System.Data;
911
using System.Data.SqlClient;
12+
using System.Text;
1013
using System.Threading.Tasks;
1114

1215
namespace SQLHelperDB.SpeedTests.Tests
@@ -112,7 +115,7 @@ public void Setup()
112115
.AddAssembly(typeof(Program).Assembly)
113116
.RegisterSQLHelper()
114117
.Build();
115-
Helper = new SQLHelper(Canister.Builder.Bootstrapper.Resolve<IConfiguration>(), SqlClientFactory.Instance);
118+
Helper = new SQLHelper(Canister.Builder.Bootstrapper.Resolve<ObjectPool<StringBuilder>>(), Canister.Builder.Bootstrapper.Resolve<DynamoFactory>(), Canister.Builder.Bootstrapper.Resolve<IConfiguration>());
116119
Helper2 = new SQLHelperDBTests.SQLHelper(Canister.Builder.Bootstrapper.Resolve<IConfiguration>(), SqlClientFactory.Instance);
117120

118121
using (var TempConnection = SqlClientFactory.Instance.CreateConnection())

TestApp/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using BigBook;
2-
using Microsoft.Extensions.Configuration;
32
using SQLHelperDB;
43
using SQLHelperDB.Registration;
54
using System;
@@ -14,8 +13,9 @@ internal class Program
1413
private static async Task Main(string[] args)
1514
{
1615
Canister.Builder.CreateContainer(null).AddAssembly(typeof(Program).Assembly).RegisterSQLHelper().Build();
16+
var Helper = Canister.Builder.Bootstrapper.Resolve<SQLHelper>();
1717

18-
var Results = await new SQLHelper(Canister.Builder.Bootstrapper.Resolve<IConfiguration>(), SqlClientFactory.Instance)
18+
var Results = await Helper.CreateBatch(SqlClientFactory.Instance)
1919
.AddQuery(CommandType.Text, @";WITH MyDuplicate AS (SELECT
2020
Sch.[name] AS SchemaName,
2121
Obj.[name] AS TableName,

src/SQLHelper.DB/HelperClasses/Batch.cs

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ limitations under the License.
1515
*/
1616

1717
using BigBook;
18-
using BigBook.DataMapper;
1918
using Microsoft.Extensions.ObjectPool;
2019
using SQLHelperDB.ExtensionMethods;
2120
using SQLHelperDB.HelperClasses.Interfaces;
@@ -43,28 +42,32 @@ public class Batch : IBatch
4342
/// </summary>
4443
/// <param name="source">Source info</param>
4544
/// <param name="stringBuilderPool">The string builder pool.</param>
46-
/// <param name="aopManager">The aop manager.</param>
47-
/// <param name="dataMapper">The data mapper.</param>
48-
public Batch(IConnection source, ObjectPool<StringBuilder>? stringBuilderPool, Aspectus.Aspectus? aopManager, Manager? dataMapper)
45+
/// <param name="dynamoFactory">The dynamo factory.</param>
46+
public Batch(IConnection source, ObjectPool<StringBuilder> stringBuilderPool, DynamoFactory dynamoFactory)
4947
{
5048
Commands = new List<ICommand>();
5149
Headers = new List<ICommand>();
5250
Source = source;
5351
StringBuilderPool = stringBuilderPool;
54-
AOPManager = aopManager;
55-
DataMapper = dataMapper;
52+
DynamoFactory = dynamoFactory;
5653
}
5754

5855
/// <summary>
5956
/// Command count
6057
/// </summary>
6158
public int CommandCount => Commands.Count;
6259

60+
/// <summary>
61+
/// Gets the dynamo factory.
62+
/// </summary>
63+
/// <value>The dynamo factory.</value>
64+
public DynamoFactory DynamoFactory { get; }
65+
6366
/// <summary>
6467
/// Gets the string builder pool.
6568
/// </summary>
6669
/// <value>The string builder pool.</value>
67-
public ObjectPool<StringBuilder>? StringBuilderPool { get; }
70+
public ObjectPool<StringBuilder> StringBuilderPool { get; }
6871

6972
/// <summary>
7073
/// Commands to batch
@@ -82,18 +85,6 @@ public Batch(IConnection source, ObjectPool<StringBuilder>? stringBuilderPool, A
8285
/// </summary>
8386
protected IConnection Source { get; private set; }
8487

85-
/// <summary>
86-
/// Gets the aop manager.
87-
/// </summary>
88-
/// <value>The aop manager.</value>
89-
private Aspectus.Aspectus? AOPManager { get; }
90-
91-
/// <summary>
92-
/// Gets the data mapper.
93-
/// </summary>
94-
/// <value>The data mapper.</value>
95-
private Manager? DataMapper { get; }
96-
9788
/// <summary>
9889
/// Used to parse SQL commands to find parameters (when batching)
9990
/// </summary>
@@ -299,7 +290,7 @@ private List<dynamic> GetValues(DbDataReader tempReader)
299290
}
300291
while (tempReader.Read())
301292
{
302-
var Value = new Dynamo(false, AOPManager, StringBuilderPool, DataMapper);
293+
var Value = DynamoFactory.Create(false);
303294
for (var x = 0; x < tempReader.FieldCount; ++x)
304295
{
305296
Value.Add(FieldNames[x], tempReader[x]);

src/SQLHelper.DB/SQLHelper.DB.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
<PackageReference Include="System.Data.Common" Version="4.3.0" />
3636
<PackageReference Include="System.Data.SqlClient" Version="4.8.1" />
3737
<PackageReference Include="System.Dynamic.Runtime" Version="4.3.0" />
38-
<PackageReference Include="BigBook" Version="3.0.21" />
38+
<PackageReference Include="BigBook" Version="3.0.22" />
3939
<PackageReference Include="Microsoft.Extensions.Configuration" Version="3.1.3" />
4040
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
4141
</ItemGroup>

src/SQLHelper.DB/SQLHelper.cs

Lines changed: 22 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ limitations under the License.
1515
*/
1616

1717
using BigBook;
18-
using BigBook.DataMapper;
1918
using Microsoft.Extensions.Configuration;
2019
using Microsoft.Extensions.ObjectPool;
2120
using SQLHelperDB.HelperClasses;
@@ -40,46 +39,15 @@ public class SQLHelper
4039
/// <summary>
4140
/// Initializes a new instance of the <see cref="SQLHelper"/> class.
4241
/// </summary>
43-
/// <param name="configuration">The configuration object.</param>
44-
/// <param name="factory">The factory.</param>
45-
/// <param name="database">The database.</param>
46-
/// <param name="stringBuilderPool">
47-
/// The string builder pool (if passed in, this will be used to generate any StringBuilders
48-
/// the system needs).
49-
/// </param>
50-
/// <param name="aopManager">
51-
/// The aop manager (if passed in, this will be used to create new objects).
52-
/// </param>
53-
/// <param name="dataMapper">
54-
/// The data mapper (if passed in the data mapper is used to allow mapping between the
55-
/// dynamic objects that are returned and the class specified)
56-
/// </param>
57-
public SQLHelper(IConfiguration configuration, DbProviderFactory? factory = null, string database = "Default", ObjectPool<StringBuilder>? stringBuilderPool = null, Aspectus.Aspectus? aopManager = null, Manager? dataMapper = null)
58-
: this(Connections.ContainsKey(database) ? Connections[database] : new Connection(configuration, factory ?? SqlClientFactory.Instance, database), stringBuilderPool, aopManager, dataMapper)
59-
{
60-
}
61-
62-
/// <summary>
63-
/// Initializes a new instance of the <see cref="SQLHelper"/> class.
64-
/// </summary>
65-
/// <param name="connection">The connection to use.</param>
66-
/// <param name="stringBuilderPool">
67-
/// The string builder pool (if passed in, this will be used to generate any StringBuilders
68-
/// the system needs).
69-
/// </param>
70-
/// <param name="aopManager">
71-
/// The aop manager (if passed in, this will be used to create new objects).
72-
/// </param>
73-
/// <param name="dataMapper">
74-
/// The data mapper (if passed in the data mapper is used to allow mapping between the
75-
/// dynamic objects that are returned and the class specified)
76-
/// </param>
77-
public SQLHelper(IConnection connection, ObjectPool<StringBuilder>? stringBuilderPool = null, Aspectus.Aspectus? aopManager = null, Manager? dataMapper = null)
42+
/// <param name="stringBuilderPool">The string builder pool.</param>
43+
/// <param name="dynamoFactory">The dynamo factory.</param>
44+
/// <param name="configuration">The configuration.</param>
45+
public SQLHelper(ObjectPool<StringBuilder> stringBuilderPool, DynamoFactory dynamoFactory, IConfiguration configuration)
7846
{
7947
StringBuilderPool = stringBuilderPool;
80-
AopManager = aopManager;
81-
DataMapper = dataMapper;
82-
SetConnection(connection);
48+
DynamoFactory = dynamoFactory;
49+
Configuration = configuration;
50+
SetConnection(new Connection(configuration, SqlClientFactory.Instance, "Default"));
8351
}
8452

8553
/// <summary>
@@ -92,7 +60,7 @@ public SQLHelper(IConnection connection, ObjectPool<StringBuilder>? stringBuilde
9260
/// Gets or sets the source.
9361
/// </summary>
9462
/// <value>The source.</value>
95-
public IConnection DatabaseConnection { get; private set; }
63+
public IConnection? DatabaseConnection { get; private set; }
9664

9765
/// <summary>
9866
/// Gets the batch.
@@ -101,28 +69,28 @@ public SQLHelper(IConnection connection, ObjectPool<StringBuilder>? stringBuilde
10169
protected IBatch Batch { get; private set; }
10270

10371
/// <summary>
104-
/// Gets the connections.
72+
/// Gets the configuration.
10573
/// </summary>
106-
/// <value>The connections.</value>
107-
private static ConcurrentDictionary<string, IConnection> Connections { get; } = new ConcurrentDictionary<string, IConnection>();
74+
/// <value>The configuration.</value>
75+
protected IConfiguration Configuration { get; }
10876

10977
/// <summary>
110-
/// Gets the aop manager.
78+
/// Gets the dynamo factory.
11179
/// </summary>
112-
/// <value>The aop manager.</value>
113-
private Aspectus.Aspectus? AopManager { get; }
80+
/// <value>The dynamo factory.</value>
81+
protected DynamoFactory DynamoFactory { get; }
11482

11583
/// <summary>
116-
/// Gets the data mapper.
84+
/// Gets the string builder pool.
11785
/// </summary>
118-
/// <value>The data mapper.</value>
119-
private Manager? DataMapper { get; }
86+
/// <value>The string builder pool.</value>
87+
protected ObjectPool<StringBuilder> StringBuilderPool { get; }
12088

12189
/// <summary>
122-
/// Gets the string builder pool.
90+
/// Gets the connections.
12391
/// </summary>
124-
/// <value>The string builder pool.</value>
125-
private ObjectPool<StringBuilder>? StringBuilderPool { get; }
92+
/// <value>The connections.</value>
93+
private static ConcurrentDictionary<string, IConnection> Connections { get; } = new ConcurrentDictionary<string, IConnection>();
12694

12795
/// <summary>
12896
/// Adds a query that gets carried across in internal batches.
@@ -213,11 +181,10 @@ public SQLHelper CreateBatch(IConnection connection)
213181
/// <summary>
214182
/// Creates the batch using the connection info specified.
215183
/// </summary>
216-
/// <param name="configuration">The configuration.</param>
217184
/// <param name="factory">The factory.</param>
218185
/// <param name="database">The database.</param>
219186
/// <returns>This.</returns>
220-
public SQLHelper CreateBatch(IConfiguration configuration, DbProviderFactory? factory = null, string database = "Default") => CreateBatch(Connections.ContainsKey(database) ? Connections[database] : new Connection(configuration, factory ?? SqlClientFactory.Instance, database));
187+
public SQLHelper CreateBatch(DbProviderFactory? factory = null, string database = "Default") => CreateBatch(Connections.ContainsKey(database) ? Connections[database] : new Connection(Configuration, factory ?? SqlClientFactory.Instance, database));
221188

222189
/// <summary>
223190
/// Executes the queries asynchronously.
@@ -274,7 +241,7 @@ private void SetConnection(IConnection connection)
274241
DatabaseConnection = connection ?? throw new ArgumentNullException(nameof(connection));
275242
if (!Connections.ContainsKey(connection.Name))
276243
Connections.AddOrUpdate(connection.Name, connection, (_, value) => value);
277-
Batch ??= new Batch(DatabaseConnection, StringBuilderPool, AopManager, DataMapper);
244+
Batch ??= new Batch(DatabaseConnection, StringBuilderPool, DynamoFactory);
278245
Batch.SetConnection(DatabaseConnection);
279246
}
280247
}

test/SQLHelper.Tests/HelperClasses/BatchTests.cs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using BigBook.DataMapper;
1+
using BigBook;
22
using Microsoft.Extensions.Configuration;
33
using Microsoft.Extensions.ObjectPool;
44
using SQLHelperDB.HelperClasses;
@@ -23,8 +23,7 @@ public void AddQuery()
2323
"Data Source=localhost;Initial Catalog=TestDatabase;Integrated Security=SSPI;Pooling=false",
2424
"DATABASE NAME"),
2525
Canister.Builder.Bootstrapper.Resolve<ObjectPool<StringBuilder>>(),
26-
Canister.Builder.Bootstrapper.Resolve<Aspectus.Aspectus>(),
27-
Canister.Builder.Bootstrapper.Resolve<Manager>()
26+
Canister.Builder.Bootstrapper.Resolve<DynamoFactory>()
2827
);
2928
Instance.AddQuery((___, __, _) => { }, 10, false, "SELECT * FROM TestUsers", CommandType.Text);
3029
Assert.NotNull(Instance);
@@ -42,8 +41,7 @@ public void AddQuerys()
4241
"Data Source=localhost;Initial Catalog=TestDatabase;Integrated Security=SSPI;Pooling=false",
4342
"DATABASE NAME"),
4443
Canister.Builder.Bootstrapper.Resolve<ObjectPool<StringBuilder>>(),
45-
Canister.Builder.Bootstrapper.Resolve<Aspectus.Aspectus>(),
46-
Canister.Builder.Bootstrapper.Resolve<Manager>()
44+
Canister.Builder.Bootstrapper.Resolve<DynamoFactory>()
4745
);
4846
Instance.AddQuery((___, __, _) => { }, 10, false, "SELECT * FROM TestUsers", CommandType.Text)
4947
.AddQuery((___, __, _) => { }, 10, false, "SELECT * FROM TestGroups", CommandType.Text);
@@ -62,8 +60,7 @@ public void AddQuerysWithParameters()
6260
"Data Source=localhost;Initial Catalog=TestDatabase;Integrated Security=SSPI;Pooling=false",
6361
"DATABASE NAME"),
6462
Canister.Builder.Bootstrapper.Resolve<ObjectPool<StringBuilder>>(),
65-
Canister.Builder.Bootstrapper.Resolve<Aspectus.Aspectus>(),
66-
Canister.Builder.Bootstrapper.Resolve<Manager>()
63+
Canister.Builder.Bootstrapper.Resolve<DynamoFactory>()
6764
);
6865
Instance.AddQuery((___, __, _) => { }, 10, false, "SELECT * FROM TestUsers WHERE UserID=@0", CommandType.Text, 1)
6966
.AddQuery((___, __, _) => { }, 10, false, "SELECT * FROM TestGroups WHERE GroupID=@0", CommandType.Text, 10);
@@ -82,8 +79,7 @@ public void AddQueryWithParameters()
8279
"Data Source=localhost;Initial Catalog=TestDatabase;Integrated Security=SSPI;Pooling=false",
8380
"DATABASE NAME"),
8481
Canister.Builder.Bootstrapper.Resolve<ObjectPool<StringBuilder>>(),
85-
Canister.Builder.Bootstrapper.Resolve<Aspectus.Aspectus>(),
86-
Canister.Builder.Bootstrapper.Resolve<Manager>()
82+
Canister.Builder.Bootstrapper.Resolve<DynamoFactory>()
8783
);
8884
Instance.AddQuery((___, __, _) => { }, 10, false, "SELECT * FROM TestUsers WHERE UserID=@0", CommandType.Text, 1);
8985
Assert.NotNull(Instance);
@@ -101,8 +97,7 @@ public void Creation()
10197
"Data Source=localhost;Initial Catalog=TestDatabase;Integrated Security=SSPI;Pooling=false",
10298
"DATABASE NAME"),
10399
Canister.Builder.Bootstrapper.Resolve<ObjectPool<StringBuilder>>(),
104-
Canister.Builder.Bootstrapper.Resolve<Aspectus.Aspectus>(),
105-
Canister.Builder.Bootstrapper.Resolve<Manager>()
100+
Canister.Builder.Bootstrapper.Resolve<DynamoFactory>()
106101
);
107102
Assert.NotNull(Instance);
108103
Assert.Equal("", Instance.ToString());

0 commit comments

Comments
 (0)