Skip to content

Commit e31849b

Browse files
committed
Refactored to use latest C# features
1 parent 887c7bb commit e31849b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+102
-173
lines changed

.editorconfig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ dotnet_naming_rule.members_should_be_pascal_case.style = pascal_case_style
137137
dotnet_naming_symbols.all_members.applicable_kinds = *
138138

139139
dotnet_naming_style.pascal_case_style.capitalization = pascal_case
140+
dotnet_style_operator_placement_when_wrapping=beginning_of_line
140141

141142
# CSharp code style settings:
142143
[*.cs]
@@ -204,6 +205,6 @@ csharp_space_between_parentheses = false
204205
csharp_space_between_square_brackets = false
205206

206207
# Blocks are allowed
207-
csharp_prefer_braces = true:warning
208+
csharp_prefer_braces =when_multiline:warning
208209
csharp_preserve_single_line_blocks = true
209210
csharp_preserve_single_line_statements = true

src/EntityFrameworkCore.AutoFixture/Core/DbContextCustomization.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using AutoFixture;
33

44
namespace EntityFrameworkCore.AutoFixture.Core
@@ -7,10 +7,7 @@ public class DbContextCustomization : ICustomization
77
{
88
public virtual void Customize(IFixture fixture)
99
{
10-
if (fixture is null)
11-
{
12-
throw new ArgumentNullException(nameof(fixture));
13-
}
10+
if (fixture is null) throw new ArgumentNullException(nameof(fixture));
1411

1512
fixture.Customizations.Add(new DbContextOptionsSpecimenBuilder());
1613
}

src/EntityFrameworkCore.AutoFixture/Core/DbContextOptionsSpecimenBuilder.cs

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Linq;
33
using AutoFixture.Kernel;
44
using Microsoft.EntityFrameworkCore;
@@ -22,48 +22,30 @@ public DbContextOptionsSpecimenBuilder(IRequestSpecification optionsSpecificatio
2222

2323
public object Create(object request, ISpecimenContext context)
2424
{
25-
if (context == null)
26-
{
27-
throw new ArgumentNullException(nameof(context));
28-
}
29-
30-
if (!this.OptionsSpecification.IsSatisfiedBy(request))
31-
{
32-
return new NoSpecimen();
33-
}
34-
35-
if (!(request is Type type))
36-
{
37-
return new NoSpecimen();
38-
}
25+
if (context == null) throw new ArgumentNullException(nameof(context));
26+
if (!this.OptionsSpecification.IsSatisfiedBy(request)) return new NoSpecimen();
27+
if (request is not Type type) return new NoSpecimen();
3928

4029
var contextType = type.GetGenericArguments().Single();
41-
4230
var optionsBuilderObj = context.Resolve(typeof(IOptionsBuilder));
43-
44-
if (optionsBuilderObj is NoSpecimen
45-
|| optionsBuilderObj is OmitSpecimen
46-
|| optionsBuilderObj is null)
31+
return optionsBuilderObj switch
4732
{
48-
return optionsBuilderObj;
49-
}
50-
51-
if (!(optionsBuilderObj is IOptionsBuilder optionsBuilder))
52-
{
53-
return new NoSpecimen();
54-
}
55-
56-
return optionsBuilder.Build(contextType);
33+
NoSpecimen or OmitSpecimen or null => optionsBuilderObj,
34+
IOptionsBuilder optionsBuilder => optionsBuilder.Build(contextType),
35+
_ => new NoSpecimen()
36+
};
5737
}
5838

5939
private class IsDbContextOptionsSpecification : IRequestSpecification
6040
{
6141
public bool IsSatisfiedBy(object request)
6242
{
63-
return request is Type type
64-
&& !type.IsAbstract
65-
&& type.IsGenericType
66-
&& typeof(DbContextOptions<>) == type.GetGenericTypeDefinition();
43+
return request is Type
44+
{
45+
IsAbstract: false,
46+
IsGenericType: true
47+
} type
48+
&& typeof(DbContextOptions<>) == type.GetGenericTypeDefinition();
6749
}
6850
}
6951
}

src/EntityFrameworkCore.AutoFixture/Core/IOptionsBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22

33
namespace EntityFrameworkCore.AutoFixture.Core
44
{

src/EntityFrameworkCore.AutoFixture/Core/OptionsBuilder.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Reflection;
33
using Microsoft.EntityFrameworkCore;
44
using static System.FormattableString;
@@ -11,14 +11,13 @@ public abstract class OptionsBuilder : IOptionsBuilder
1111

1212
public virtual object Build(Type type)
1313
{
14-
if (type == null)
15-
{
16-
throw new ArgumentNullException(nameof(type));
17-
}
14+
if (type == null) throw new ArgumentNullException(nameof(type));
1815

1916
if (!typeof(DbContext).IsAssignableFrom(type) || type.IsAbstract)
2017
{
21-
throw new ArgumentException(Invariant($"The context type should be a non-abstract class inherited from {typeof(DbContext)}"), nameof(type));
18+
throw new ArgumentException(
19+
Invariant($"The context type should be a non-abstract class inherited from {typeof(DbContext)}"),
20+
nameof(type));
2221
}
2322

2423
var methods = this.GetType().GetMethods(BindingFlags.Public | BindingFlags.Instance);

src/EntityFrameworkCore.AutoFixture/InMemory/InMemoryContextCustomization.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using AutoFixture;
33
using EntityFrameworkCore.AutoFixture.Core;
44

@@ -8,13 +8,9 @@ public class InMemoryContextCustomization : DbContextCustomization
88
{
99
public override void Customize(IFixture fixture)
1010
{
11-
if (fixture is null)
12-
{
13-
throw new ArgumentNullException(nameof(fixture));
14-
}
11+
if (fixture is null) throw new ArgumentNullException(nameof(fixture));
1512

1613
base.Customize(fixture);
17-
1814
fixture.Customizations.Add(new InMemoryOptionsSpecimenBuilder());
1915
}
2016
}

src/EntityFrameworkCore.AutoFixture/InMemory/InMemoryOptionsBuilder.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using EntityFrameworkCore.AutoFixture.Core;
33
using Microsoft.EntityFrameworkCore;
44

@@ -19,7 +19,8 @@ public InMemoryOptionsBuilder()
1919

2020
public string DatabaseName { get; }
2121

22-
public override DbContextOptions<TContext> Build<TContext>() => new DbContextOptionsBuilder<TContext>()
22+
public override DbContextOptions<TContext> Build<TContext>()
23+
=> new DbContextOptionsBuilder<TContext>()
2324
.UseInMemoryDatabase(this.DatabaseName)
2425
.Options;
2526
}

src/EntityFrameworkCore.AutoFixture/InMemory/InMemoryOptionsSpecimenBuilder.cs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using AutoFixture.Kernel;
33
using EntityFrameworkCore.AutoFixture.Core;
44

@@ -21,15 +21,8 @@ public InMemoryOptionsSpecimenBuilder()
2121

2222
public object Create(object request, ISpecimenContext context)
2323
{
24-
if (context == null)
25-
{
26-
throw new ArgumentNullException(nameof(context));
27-
}
28-
29-
if (!this.OptionsSpecification.IsSatisfiedBy(request))
30-
{
31-
return new NoSpecimen();
32-
}
24+
if (context == null) throw new ArgumentNullException(nameof(context));
25+
if (!this.OptionsSpecification.IsSatisfiedBy(request)) return new NoSpecimen();
3326

3427
return new InMemoryOptionsBuilder();
3528
}
@@ -38,9 +31,8 @@ private class IsOptionsBuilder : IRequestSpecification
3831
{
3932
public bool IsSatisfiedBy(object request)
4033
{
41-
return request is Type type
42-
&& type.IsInterface
43-
&& type == typeof(IOptionsBuilder);
34+
return request is Type { IsInterface: true } type
35+
&& type == typeof(IOptionsBuilder);
4436
}
4537
}
4638
}

src/EntityFrameworkCore.AutoFixture/Sqlite/SqliteConnectionSpecimenBuilder.cs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using AutoFixture.Kernel;
33
using Microsoft.Data.Sqlite;
44

@@ -21,15 +21,9 @@ public SqliteConnectionSpecimenBuilder()
2121

2222
public object Create(object request, ISpecimenContext context)
2323
{
24-
if (context == null)
25-
{
26-
throw new ArgumentNullException(nameof(context));
27-
}
24+
if (context == null) throw new ArgumentNullException(nameof(context));
2825

29-
if (!this.ConnectionSpecification.IsSatisfiedBy(request))
30-
{
31-
return new NoSpecimen();
32-
}
26+
if (!this.ConnectionSpecification.IsSatisfiedBy(request)) return new NoSpecimen();
3327

3428
return new SqliteConnection("DataSource=:memory:");
3529
}
@@ -38,9 +32,8 @@ private class IsSqliteConnectionSpecification : IRequestSpecification
3832
{
3933
public bool IsSatisfiedBy(object request)
4034
{
41-
return request is Type type
42-
&& !type.IsAbstract
43-
&& type == typeof(SqliteConnection);
35+
return request is Type { IsAbstract: false } type
36+
&& type == typeof(SqliteConnection);
4437
}
4538
}
4639
}

src/EntityFrameworkCore.AutoFixture/Sqlite/SqliteContextCustomization.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using AutoFixture;
33
using EntityFrameworkCore.AutoFixture.Core;
44

@@ -8,10 +8,7 @@ public class SqliteContextCustomization : DbContextCustomization
88
{
99
public override void Customize(IFixture fixture)
1010
{
11-
if (fixture is null)
12-
{
13-
throw new ArgumentNullException(nameof(fixture));
14-
}
11+
if (fixture is null) throw new ArgumentNullException(nameof(fixture));
1512

1613
base.Customize(fixture);
1714

0 commit comments

Comments
 (0)