-
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathSqlServerProvider.cs
More file actions
62 lines (53 loc) · 2.06 KB
/
SqlServerProvider.cs
File metadata and controls
62 lines (53 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Nano.Common.Mvc.HealthChecks.Extensions;
using Nano.Data.Abstractions;
using Nano.Data.Abstractions.Config;
using Nano.Data.Extensions;
using System;
namespace Nano.Data.SqlServer;
/// <summary>
/// SQL Server data provider.
/// </summary>
/// <remarks>
/// Supports retry policies, batching, spatial data via NetTopologySuite, query splitting behavior, and optional health checks.
/// Documentation: https://github.com/Nano-Core/Nano.Library/tree/master/Nano.Data.SqlServer
/// </remarks>
public sealed class SqlServerProvider : IDataProvider
{
/// <inheritdoc />
public static void Configure(IServiceCollection services, DataOptions options)
{
ArgumentNullException.ThrowIfNull(services);
ArgumentNullException.ThrowIfNull(options);
services
.AddSingleton<IDatabaseExceptionTranslator, SqlServerExceptionTranslator>();
if (options.HealthCheck != null)
{
var failureStatus = options.HealthCheck.UnhealthyStatus
.GetHealthStatus();
services
.AddHealthChecks()
.AddSqlServer(options.ConnectionString, name: "sqlserver", failureStatus: failureStatus);
}
}
/// <inheritdoc />
public static void Configure(DbContextOptionsBuilder builder, DataOptions options)
{
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(options);
var batchSize = options.BatchSize;
var retryCount = options.QueryRetryCount;
var connectionString = options.ConnectionString;
builder
.UseSqlServer(connectionString, x =>
{
var querySplittingBehavior = options.QuerySplittingBehavior
.GetQuerySplittingBehavior();
x.MaxBatchSize(batchSize);
x.EnableRetryOnFailure(retryCount);
x.UseNetTopologySuite();
x.UseQuerySplittingBehavior(querySplittingBehavior);
});
}
}