Skip to content

Commit 65ef641

Browse files
Using DataAnnotation in options
1 parent 1aaaff7 commit 65ef641

3 files changed

Lines changed: 14 additions & 28 deletions

File tree

src/Dotnet6.GraphQL4.Repositories.Abstractions/DependencyInjection/Extensions/ServiceCollectionExtensions.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ public static OptionsBuilder<TransactionOptions> ConfigureTransactionOptions(thi
2525
=> services
2626
.AddOptions<TransactionOptions>()
2727
.Bind(section)
28+
.ValidateDataAnnotations()
2829
.Validate(
2930
validation: options => options.IsolationLevel is not System.Transactions.IsolationLevel.Unspecified,
30-
failureMessage: "Transaction isolation level must be specified");
31+
failureMessage: "Transaction isolation level must be specified")
32+
.ValidateOnStart();
3133
}
3234
}
Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
1-
using System.Transactions;
1+
using System.ComponentModel.DataAnnotations;
2+
using System.Transactions;
23

34
namespace Dotnet6.GraphQL4.Repositories.Abstractions.DependencyInjection.Options
45
{
56
public class TransactionOptions
67
{
7-
private const IsolationLevel DefaultIsolationLevel = IsolationLevel.ReadCommitted;
8-
private IsolationLevel? _isolationLevel;
9-
10-
public IsolationLevel IsolationLevel
11-
{
12-
get => _isolationLevel ?? DefaultIsolationLevel;
13-
set => _isolationLevel = value;
14-
}
8+
[Required, EnumDataType(typeof(IsolationLevel))]
9+
public IsolationLevel IsolationLevel { get; set; }
1510
}
1611
}
Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,19 @@
11
using System;
2+
using System.ComponentModel.DataAnnotations;
23

34
namespace Dotnet6.GraphQL4.Store.Repositories.DependencyInjection.Options
45
{
56
public class SqlServerRetryingOptions
67
{
7-
private const int DefaultRetryCount = 5;
8-
private const int DefaultRetryDelay = 5;
8+
[Required, Range(5, 20)]
9+
public int MaxRetryCount { get; init; }
910

10-
private int _maxRetryCount;
11-
private int _maxSecondsRetryDelay;
11+
[Required, Range(5, 20)]
12+
public int MaxSecondsRetryDelay { get; init; }
1213

13-
public int MaxRetryCount
14-
{
15-
get => _maxRetryCount <= 0 ? DefaultRetryCount : _maxRetryCount;
16-
set => _maxRetryCount = value;
17-
}
14+
public int[] ErrorNumbersToAdd { get; init; }
1815

19-
public int MaxSecondsRetryDelay
20-
{
21-
get => _maxSecondsRetryDelay <= 0 ? DefaultRetryDelay : _maxSecondsRetryDelay;
22-
set => _maxSecondsRetryDelay = value;
23-
}
24-
25-
public int[] ErrorNumbersToAdd { get; set; }
26-
27-
internal TimeSpan MaxRetryDelay
16+
internal TimeSpan MaxRetryDelay
2817
=> TimeSpan.FromSeconds(MaxSecondsRetryDelay);
2918
}
3019
}

0 commit comments

Comments
 (0)