Skip to content

Commit ec81996

Browse files
committed
Code cleanup for configuration of Json. Readme updates for new features. Version & Release notes updated for publishing to Nuget.
1 parent 6b259a5 commit ec81996

File tree

7 files changed

+61
-32
lines changed

7 files changed

+61
-32
lines changed

DotNetFramework.SqlBulkHelpers/DotNetFramework.SqlBulkHelpers-Deprecated.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
<WarningLevel>4</WarningLevel>
3333
</PropertyGroup>
3434
<ItemGroup>
35-
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
36-
<HintPath>..\packages\Newtonsoft.Json.12.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
35+
<Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
36+
<HintPath>..\packages\Newtonsoft.Json.13.0.4\lib\net45\Newtonsoft.Json.dll</HintPath>
3737
</Reference>
3838
<Reference Include="System" />
3939
<Reference Include="System.Configuration" />
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3-
<package id="Newtonsoft.Json" version="13.0.1" targetFramework="net461" />
3+
<package id="Newtonsoft.Json" version="13.0.4" targetFramework="net48" />
44
</packages>

NetStandard.SqlBulkHelpers/Attributes/SqlBulkConvertToJsonAttribute.cs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,10 @@ namespace SqlBulkHelpers
1111
[AttributeUsage(AttributeTargets.Property, Inherited = true, AllowMultiple = false)]
1212
public class SqlBulkConvertToJsonAttribute : Attribute, ISqlBulkHelpersPropertyTransformer
1313
{
14-
/// <summary>
15-
/// You can globally set this in your application root or bootstrapping logic to ensure the desired Serialization options
16-
/// are used for all implementations of the Json Conversion!
17-
/// </summary>
18-
public static JsonSerializerOptions SqlBulkJsonConverterSerializerOptions { get; set; } = new JsonSerializerOptions();
19-
20-
public static void SetJsonSerializerOptions(JsonSerializerOptions options)
21-
=> SqlBulkJsonConverterSerializerOptions = options;
22-
2314
object ISqlBulkHelpersPropertyTransformer.TransformPropValue(object propValue)
24-
=> JsonSerializer.Serialize(propValue, SqlBulkJsonConverterSerializerOptions);
15+
=> JsonSerializer.Serialize(
16+
propValue,
17+
SqlBulkHelpersConfig.DefaultConfig.SqlBulkJsonConverterSerializerOptions ?? new JsonSerializerOptions()
18+
);
2519
}
2620
}

NetStandard.SqlBulkHelpers/NetStandard.SqlBulkHelpers.csproj

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@
1414
<Description>A library for easy, efficient and high performance bulk insert and update of data, into a Sql Database, from .Net applications. By leveraging the power of the SqlBulkCopy classes with added support for Identity primary key table columns this library provides a greatly simplified interface to process Identity based Entities with Bulk Performance with the wide compatibility of .NetStandard 2.0.</Description>
1515
<PackageTags>sql server database table bulk insert update identity column sqlbulkcopy orm dapper linq2sql materialization materialized data view materialized-data materialized-view sync replication replica readonly</PackageTags>
1616
<PackageReleaseNotes>
17-
- Migrate from FastMember to Fasterflect for improved performance and reliability and functionality.
18-
- Implement support for Property Handler interceptors to enable advanced custom handling of Property Get/Set operations during Bulk Insert/Update operations.
19-
- Improve compatibility with RepoDb by implementing support for RepoDb PropertyHandler interceptors by dynamically detecting and using any registered PropertyHandlers (Annotations on Properties) if already used on models.
2017
- Fix issue with SLQ Server non-writeable fields such as Computed Columns causing failures during Bulk Insert/Update operations.
18+
- Migrate from FastMember to Fasterflect for improved performance, reliability and functionality.
19+
- Implement support for Property Handler interceptors to enable advanced dynamic transformation of property values during Bulk Insert/Update operations.
20+
- Improve compatibility with RepoDb by implementing support for RepoDb PropertyHandler interceptors by dynamically detecting and using any registered PropertyHandlers (Annotations on Properties) if already used on models.
2121
- Add Support to intentionally ignore properties on data models (for edge cases) using SqlBulkIgnoreAttribute.
2222
- Add Support for property mapping/interception hanlding for dynamic conversions before writing to the DB via new ISqlBulkHelpersPropertyConverter interface that can be added to any custom Attribute (e.g. convert comples object property to Json to store in a single field).
23-
- Add compatibility support to utilize RepoDb PropertyHandlerAttribute for conversions before writing to the DB.
24-
- Add Support for dynamic Json property conversion with built in SqlBulkConvertToJsonAttribute.
23+
- Add compatibility support to utilize RepoDb IPropertyHandler and PropertyHandlerAttribute for conversions before writing to the DB.
24+
- Add Support for dynamic Json property conversion with new SqlBulkConvertToJsonAttribute now provided out-of-the-box.
2525

2626
Prior Relese Notes:
2727
- Updates to resolve security vulnerabilities in dependencies.
@@ -97,7 +97,7 @@
9797
<ItemGroup>
9898
<PackageReference Include="fasterflect" Version="3.0.0" />
9999
<PackageReference Include="LazyCacheHelpers" Version="1.3.3" />
100-
<PackageReference Include="Microsoft.Data.SqlClient" Version="5.1.3" />
100+
<PackageReference Include="Microsoft.Data.SqlClient" Version="6.1.4" />
101101
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
102102
<PackageReference Include="System.Text.RegularExpressions" Version="4.3.1" />
103103
</ItemGroup>

NetStandard.SqlBulkHelpers/SqlBulkHelpersConfig.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Text.Json;
23
using Microsoft.Data.SqlClient;
34
using SqlBulkHelpers.CustomExtensions;
45
using SqlBulkHelpers.MaterializedData;
@@ -53,6 +54,8 @@ public interface ISqlBulkHelpersConfig
5354
bool IsConcurrentConnectionProcessingEnabled { get; }
5455
bool IsFullTextIndexHandlingEnabled { get; }
5556

57+
JsonSerializerOptions SqlBulkJsonConverterSerializerOptions { get; }
58+
5659
ISqlBulkHelpersConfig Clone();
5760
ISqlBulkHelpersConfig Configure(Action<SqlBulkHelpersConfig> configAction);
5861
}
@@ -224,6 +227,11 @@ public int MaxConcurrentConnections
224227
/// </summary>
225228
public bool IsFullTextIndexHandlingEnabled { get; set; } = false;
226229

230+
/// <summary>
231+
/// Set the default Json Serializer Options used for JSON conversion (e.g. via [SqlBulkConvertToJson] attribute annotation).
232+
/// </summary>
233+
public JsonSerializerOptions SqlBulkJsonConverterSerializerOptions { get; set; } = new JsonSerializerOptions();
234+
227235
#endregion
228236
}
229237
}

README.md

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -217,14 +217,14 @@ public class TestDataService
217217
To use in your project, add the [SqlBulkHelpers NuGet package](https://www.nuget.org/packages/SqlBulkHelpers/) to your project.
218218

219219
### v2.5.0 Release Notes:
220-
- Migrate from FastMember to Fasterflect for improved performance and reliability and functionality.
221-
- Implement support for Property Handler interceptors to enable advanced custom handling of Property Get/Set operations during Bulk Insert/Update operations.
222-
- Improve compatibility with RepoDb by implementing support for RepoDb PropertyHandler interceptors by dynamically detecting and using any registered PropertyHandlers (Annotations on Properties) if already used on models.
223220
- Fix issue with SLQ Server non-writeable fields such as Computed Columns causing failures during Bulk Insert/Update operations.
221+
- Migrate from FastMember to Fasterflect for improved performance, reliability and functionality.
222+
- Implement support for Property Handler interceptors to enable advanced dynamic transformation of property values during Bulk Insert/Update operations.
223+
- Improve compatibility with RepoDb by implementing support for RepoDb PropertyHandler interceptors by dynamically detecting and using any registered PropertyHandlers (Annotations on Properties) if already used on models.
224224
- Add Support to intentionally ignore properties on data models (for edge cases) using SqlBulkIgnoreAttribute.
225225
- Add Support for property mapping/interception hanlding for dynamic conversions before writing to the DB via new ISqlBulkHelpersPropertyConverter interface that can be added to any custom Attribute (e.g. convert comples object property to Json to store in a single field).
226-
- Add compatibility support to utilize RepoDb PropertyHandlerAttribute for conversions before writing to the DB.
227-
- Add Support for dynamic Json property conversion with built in SqlBulkConvertToJsonAttribute.
226+
- Add compatibility support to utilize RepoDb IPropertyHandler and PropertyHandlerAttribute for conversions before writing to the DB.
227+
- Add Support for dynamic Json property conversion with new SqlBulkConvertToJsonAttribute now provided out-of-the-box.
228228

229229
### v2.4.6 Release Notes:
230230
- Updates to resolve security vulnerabilities in dependencies.
@@ -300,7 +300,7 @@ incorrect values when classes have the same name.
300300

301301
NOTE: [Prior Release Notes are below...](#prior-release-notes)
302302

303-
### Additional Examples:
303+
## Additional Examples:
304304

305305
### Simple Example for Sql Bulk Insert or Update :
306306
Usage is very simple if you use a lightweigth Model (e.g. ORM model via Dapper) to load data from your tables...
@@ -332,10 +332,21 @@ Usage is very simple if you use a lightweigth Model (e.g. ORM model via Dapper)
332332

333333
```
334334

335-
### Example Data Model Table/Column name mapping via Annotations
336-
NOTE: The fully qualified format isn't strictly required, but is encouraged; the `[dbo]` schema will be used as default if not specified.
335+
### Example Data Model Table/Column name & value mapping via Annotations
336+
The library supports the following attribute annotations and respective functionality:
337+
1. ` [SqlBulkTable("")]` -- Defines the Table name mapping of the class
338+
- Similar attributes from Dapper & RepoDb are also fully supported for models that already utilize them.
339+
2. `[SqlBulkColumn("")]` -- Defines the Column name mapping of the property
340+
- Similar attributes from Dapper & RepoDb are also fully supported for models that already utilize them.
341+
3. `[SqlBulkIgnore]` -- Column vs Schema differences are automatically resolved, however for edge cases this allows developers to explicitly ignore/exclude a property from being used when writing to the database.
342+
4. `ISqlBulkHelpersPropertyTransformer` -- Any custom property attribute that implements this interface will be used for custom property transformation when writing to the database...
343+
- A great use of this is when one column stores some unstructures (NoSql) data for the record/row which can be automatically serialized from the Properties Class/Object. An out-of-the-box implementation of this is now provided via `[SqlBulkConvertToJson]` attribute.
344+
- Json Serialization (via System.Text.Json) can be controlled explicitly by setting the `SqlBulkJsonConverterSerializerOp tions` property via `SqlBulkHelpersConfig.ConfigureDefaults()` in your bootstrap/startup code.
345+
- Similarly the `IPropertyHandler` and associated `[PropertyHandler]` attribute from RepoDb is fully supported here also for the same functionality (uses the `Set()` method of RepoDbs interface).
346+
347+
NOTE: For Table & Column names, the fully qualified format isn't strictly required, but is encouraged; the `[dbo]` schema will be used as default if not specified.
337348

338-
*WARNING: Do NOT use `.` in your Schema or Table Names... this is not only a really bad sql code smell, it will break the parsing logic.*
349+
*WARNING: Do NOT use `.` in your Schema, Table, or Column Names... this is not only a really bad sql code smell, it will break the parsing logic.*
339350

340351
```csharp
341352
[SqlBulkTable("[dbo].[TestDataModelMappedTableName]")] //Built-in
@@ -344,11 +355,24 @@ NOTE: The fully qualified format isn't strictly required, but is encouraged; the
344355
public class TestDataModel
345356
{
346357
[SqlBulkColumn("MyColumnMappedName")] //Built-in
347-
//[Column(""MyColumnMappedName"")] -- Dapper/Linq2Sql
348-
//[Map("MyColumnMappedName")] -- RepoDB
358+
//[Column(""MyColumnMappedName"")] -- Dapper/Linq2Sql also Supported!
359+
//[Map("MyColumnMappedName")] -- RepoDB also Supported!
349360
public string Column1 { get; set; }
350361

362+
//Ignore this Column (will not be written to the DB)
363+
[SqlBulkIgnore]
351364
public int Column2 { get; set; }
365+
366+
//Implements ISqlBulkHelpersPropertyTransformer and provides an out-of-the-box JSON Column Transformer
367+
//NOTE: Json Serialization (via System.Text.Json) can be controlled explicitly by setting the `SqlBulkJsonConverterSerializerOptions`
368+
// property via `SqlBulkHelpersConfig.ConfigureDefaults()` in your bootstrap/startup code.
369+
[SqlBulkConvertToJson]
370+
//[PropertyHandler(typeof(RepoDbCustomJsonPropertyHandler))] -- RepoDB IPropertyHandler interface also Supported!
371+
public MyObjectModel Column3StoreAsJson { get; set; }
372+
373+
//Attribute that implements ISqlBulkHelpersPropertyTransformer Enum converter that ensures Enums are stored as String names vs Integer IDs.
374+
[MyCustomAttributeEnumConverter] // Must implement ISqlBulkHelpersPropertyTransformer
375+
public Enum Column4ConvertEnumToString { get; set; }
352376
}
353377

354378
```
@@ -378,6 +402,9 @@ globally in your application startup.
378402
//One new optimization (mainly for Materialized Data) is to enable support for concurrent connections...
379403
// via a Connection Factory which Enables Concurrent Connection Processing for Performance...
380404
config.EnableConcurrentSqlConnectionProcessing(sqlConnectionProvider, maxConcurrentConnections: 5);
405+
406+
//Explicity control the Json serialization behavior of the [SqlBulkConvertToJson] attribute (if used)...
407+
config.SqlBulkJsonConverterSerializerOptions = new JsonSerializerOptions(); //Customize as needed!
381408
});
382409
```
383410

SqlBulkHelpers.SampleApp.Common/RepoDbJsonPropertyHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ namespace SqlBulkHelpers.Tests
77
public class RepoDbJsonPropertyHandler : IPropertyHandler<string, TestElement>
88
{
99
public TestElement Get(string input, PropertyHandlerGetOptions options)
10-
=> JsonSerializer.Deserialize<TestElement>(input, SqlBulkConvertToJsonAttribute.SqlBulkJsonConverterSerializerOptions);
10+
=> JsonSerializer.Deserialize<TestElement>(input, SqlBulkHelpersConfig.DefaultConfig.SqlBulkJsonConverterSerializerOptions);
1111

1212
public string Set(TestElement input, PropertyHandlerSetOptions options)
13-
=> JsonSerializer.Serialize(input, SqlBulkConvertToJsonAttribute.SqlBulkJsonConverterSerializerOptions);
13+
=> JsonSerializer.Serialize(input, SqlBulkHelpersConfig.DefaultConfig.SqlBulkJsonConverterSerializerOptions);
1414
}
1515
}

0 commit comments

Comments
 (0)