Skip to content

Commit c7a2ba4

Browse files
authored
Merge pull request #18 from koenbeuk/feature/simple-registration
Feature/simple registration
2 parents 889c04a + 58f626a commit c7a2ba4

16 files changed

+411
-4
lines changed

EntityFrameworkCore.Triggered.sln

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,15 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EntityFrameworkCore.Trigger
2121
EndProject
2222
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EntityFrameworkCore.Triggered.Tests", "test\EntityFrameworkCore.Triggered.Tests\EntityFrameworkCore.Triggered.Tests.csproj", "{93EA656F-C065-450E-83C4-9CD510BF03A8}"
2323
EndProject
24-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EntityFrameworkCore.Triggered.Transactions.Abstractions", "src\EntityFrameworkCore.Triggered.Transactions.Abstractions\EntityFrameworkCore.Triggered.Transactions.Abstractions.csproj", "{E07F11E4-9B0E-4639-948B-24517C04A121}"
24+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EntityFrameworkCore.Triggered.Transactions.Abstractions", "src\EntityFrameworkCore.Triggered.Transactions.Abstractions\EntityFrameworkCore.Triggered.Transactions.Abstractions.csproj", "{E07F11E4-9B0E-4639-948B-24517C04A121}"
2525
EndProject
26-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EntityFrameworkCore.Triggered.Transactions", "src\EntityFrameworkCore.Triggered.Transactions\EntityFrameworkCore.Triggered.Transactions.csproj", "{D1F91AA5-9A1F-4817-874F-D5A7707EC150}"
26+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EntityFrameworkCore.Triggered.Transactions", "src\EntityFrameworkCore.Triggered.Transactions\EntityFrameworkCore.Triggered.Transactions.csproj", "{D1F91AA5-9A1F-4817-874F-D5A7707EC150}"
2727
EndProject
28-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EntityFrameworkCore.Triggered.Transactions.Tests", "test\EntityFrameworkCore.Triggered.Transactions.Tests\EntityFrameworkCore.Triggered.Transactions.Tests.csproj", "{20CD4CB9-A061-4C32-8F9E-E2C26D19A219}"
28+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EntityFrameworkCore.Triggered.Transactions.Tests", "test\EntityFrameworkCore.Triggered.Transactions.Tests\EntityFrameworkCore.Triggered.Transactions.Tests.csproj", "{20CD4CB9-A061-4C32-8F9E-E2C26D19A219}"
29+
EndProject
30+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EntityFrameworkCore.Triggered.AspNetCore", "src\EntityFrameworkCore.Triggered.AspNetCore\EntityFrameworkCore.Triggered.AspNetCore.csproj", "{9D83E93D-FF88-4034-BB37-20BA92379CAF}"
31+
EndProject
32+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EntityFrameworkCore.Triggered.AspNetCore.Tests", "test\EntityFrameworkCore.Triggered.AspNetCore.Tests\EntityFrameworkCore.Triggered.AspNetCore.Tests.csproj", "{A87E7272-8E3E-4D14-A1FA-DB25E25B214B}"
2933
EndProject
3034
Global
3135
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -57,6 +61,14 @@ Global
5761
{20CD4CB9-A061-4C32-8F9E-E2C26D19A219}.Debug|Any CPU.Build.0 = Debug|Any CPU
5862
{20CD4CB9-A061-4C32-8F9E-E2C26D19A219}.Release|Any CPU.ActiveCfg = Release|Any CPU
5963
{20CD4CB9-A061-4C32-8F9E-E2C26D19A219}.Release|Any CPU.Build.0 = Release|Any CPU
64+
{9D83E93D-FF88-4034-BB37-20BA92379CAF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
65+
{9D83E93D-FF88-4034-BB37-20BA92379CAF}.Debug|Any CPU.Build.0 = Debug|Any CPU
66+
{9D83E93D-FF88-4034-BB37-20BA92379CAF}.Release|Any CPU.ActiveCfg = Release|Any CPU
67+
{9D83E93D-FF88-4034-BB37-20BA92379CAF}.Release|Any CPU.Build.0 = Release|Any CPU
68+
{A87E7272-8E3E-4D14-A1FA-DB25E25B214B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
69+
{A87E7272-8E3E-4D14-A1FA-DB25E25B214B}.Debug|Any CPU.Build.0 = Debug|Any CPU
70+
{A87E7272-8E3E-4D14-A1FA-DB25E25B214B}.Release|Any CPU.ActiveCfg = Release|Any CPU
71+
{A87E7272-8E3E-4D14-A1FA-DB25E25B214B}.Release|Any CPU.Build.0 = Release|Any CPU
6072
EndGlobalSection
6173
GlobalSection(SolutionProperties) = preSolution
6274
HideSolutionNode = FALSE
@@ -68,6 +80,8 @@ Global
6880
{E07F11E4-9B0E-4639-948B-24517C04A121} = {EDFABD48-3C79-47AE-B84C-47CE2A52C20D}
6981
{D1F91AA5-9A1F-4817-874F-D5A7707EC150} = {EDFABD48-3C79-47AE-B84C-47CE2A52C20D}
7082
{20CD4CB9-A061-4C32-8F9E-E2C26D19A219} = {0FAE4F6A-93BB-453C-8FB4-B24A9F30DA59}
83+
{9D83E93D-FF88-4034-BB37-20BA92379CAF} = {EDFABD48-3C79-47AE-B84C-47CE2A52C20D}
84+
{A87E7272-8E3E-4D14-A1FA-DB25E25B214B} = {0FAE4F6A-93BB-453C-8FB4-B24A9F30DA59}
7185
EndGlobalSection
7286
GlobalSection(ExtensibilityGlobals) = postSolution
7387
SolutionGuid = {847A0017-23D6-4513-B78E-CAADBD836A7D}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
9+
</ItemGroup>
10+
11+
<ItemGroup>
12+
<ProjectReference Include="..\EntityFrameworkCore.Triggered\EntityFrameworkCore.Triggered.csproj" />
13+
</ItemGroup>
14+
15+
</Project>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Microsoft.EntityFrameworkCore;
7+
using Microsoft.Extensions.DependencyInjection;
8+
9+
namespace Microsoft.Extensions.DependencyInjection
10+
{
11+
public static class ServiceCollectionExtensions
12+
{
13+
public static IServiceCollection AddAspNetCoreTriggeredDbContext<TContext>(this IServiceCollection serviceCollection, Action<DbContextOptionsBuilder>? optionsAction = null, ServiceLifetime contextLifetime = ServiceLifetime.Scoped, ServiceLifetime optionsLifetime = ServiceLifetime.Scoped) where TContext : DbContext
14+
{
15+
if (serviceCollection is null)
16+
{
17+
throw new ArgumentNullException(nameof(serviceCollection));
18+
}
19+
20+
serviceCollection.AddTriggeredDbContext<TContext>(options => {
21+
optionsAction?.Invoke(options);
22+
23+
options.UseTriggers(triggerOptions => {
24+
triggerOptions.UseAspNetCoreIntegration();
25+
});
26+
27+
}, contextLifetime, optionsLifetime);
28+
29+
return serviceCollection;
30+
}
31+
}
32+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using EntityFrameworkCore.Triggered.Infrastructure;
7+
using Microsoft.AspNetCore.Http;
8+
9+
namespace Microsoft.Extensions.DependencyInjection
10+
{
11+
public static class TriggersContextOptionsBuilderExtensions
12+
{
13+
public static TriggersContextOptionsBuilder UseAspNetCoreIntegration(this TriggersContextOptionsBuilder builder)
14+
{
15+
if (builder is null)
16+
{
17+
throw new ArgumentNullException(nameof(builder));
18+
}
19+
20+
builder.UseApplicationScopedServiceProviderAccessor(serviceProvider => {
21+
var httpContextAccessor = serviceProvider.GetRequiredService<IHttpContextAccessor>();
22+
return httpContextAccessor.HttpContext.RequestServices;
23+
});
24+
25+
return builder;
26+
}
27+
}
28+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Microsoft.EntityFrameworkCore;
7+
8+
namespace Microsoft.Extensions.DependencyInjection
9+
{
10+
public static class ServiceCollectionExtensions
11+
{
12+
public static IServiceCollection AddTriggeredDbContext<TContext>(this IServiceCollection serviceCollection, Action<DbContextOptionsBuilder>? optionsAction = null, ServiceLifetime contextLifetime = ServiceLifetime.Scoped, ServiceLifetime optionsLifetime = ServiceLifetime.Scoped) where TContext : DbContext
13+
{
14+
var serviceCollections = serviceCollection.AddDbContext<TContext>(options => {
15+
optionsAction?.Invoke(options);
16+
options.UseTriggers();
17+
}, contextLifetime, optionsLifetime);
18+
19+
20+
return serviceCollection;
21+
}
22+
23+
public static IServiceCollection AddTriggeredDbContextPool<TContext>(this IServiceCollection serviceCollection, Action<DbContextOptionsBuilder>? optionsAction = null, int poolSize = 128) where TContext : DbContext
24+
{
25+
26+
serviceCollection.AddDbContextPool<TContext>(options => {
27+
optionsAction?.Invoke(options);
28+
options.UseTriggers();
29+
}, poolSize);
30+
31+
32+
return serviceCollection;
33+
}
34+
35+
}
36+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Linq.Expressions;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace EntityFrameworkCore.Triggered.Infrastructure.Internal
9+
{
10+
public static class AspNetCoreScopedServiceProviderAccessor
11+
{
12+
public static readonly Func<IServiceProvider, IServiceProvider> AspNetCoreServiceProviderAccessor = sp => {
13+
14+
return sp;
15+
};
16+
}
17+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
5+
<IsPackable>false</IsPackable>
6+
<Nullable>disable</Nullable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.0" />
11+
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.2" />
12+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
13+
<PackageReference Include="xunit" Version="2.4.0" />
14+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
15+
<PackageReference Include="coverlet.collector" Version="1.2.0" />
16+
</ItemGroup>
17+
18+
<ItemGroup>
19+
<ProjectReference Include="..\..\src\EntityFrameworkCore.Triggered.AspNetCore\EntityFrameworkCore.Triggered.AspNetCore.csproj" />
20+
</ItemGroup>
21+
22+
</Project>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using EntityFrameworkCore.Triggered.Transactions.Tests.Stubs;
7+
using Microsoft.AspNetCore.Http;
8+
using Microsoft.EntityFrameworkCore;
9+
using Microsoft.Extensions.DependencyInjection;
10+
using Xunit;
11+
12+
namespace EntityFrameworkCore.Triggered.AspNetCore.Tests.Extensions
13+
{
14+
public class ServiceCollectionExtensionsTests
15+
{
16+
public class TestModel
17+
{
18+
public Guid Id { get; set; }
19+
}
20+
21+
public class TestDbContext : TriggeredDbContext
22+
{
23+
public TestDbContext(DbContextOptions options) : base(options)
24+
{
25+
}
26+
27+
public DbSet<TestModel> TestModels { get; set; }
28+
}
29+
30+
31+
[Fact]
32+
public void AddTriggeredDbContext_RegistersHttpContextServiceProviderAccessor()
33+
{
34+
IServiceProvider capturedServiceProvider = null;
35+
36+
var serviceProvider = new ServiceCollection()
37+
.AddSingleton<IHttpContextAccessor, Stubs.HttpContextAccessorStub>()
38+
.AddAspNetCoreTriggeredDbContext<TestDbContext>(options => {
39+
options.UseInMemoryDatabase("Test");
40+
})
41+
.AddTransient<IBeforeSaveTrigger<TestModel>>(serviceProvider => {
42+
capturedServiceProvider = serviceProvider;
43+
return new TriggerStub<TestModel>();
44+
})
45+
.BuildServiceProvider();
46+
47+
48+
using var serviceScope = serviceProvider.CreateScope();
49+
serviceProvider.GetRequiredService<IHttpContextAccessor>().HttpContext.RequestServices = serviceScope.ServiceProvider;
50+
var dbContext = serviceScope.ServiceProvider.GetRequiredService<TestDbContext>();
51+
52+
dbContext.Add(new TestModel { });
53+
dbContext.SaveChanges();
54+
55+
Assert.NotNull(capturedServiceProvider);
56+
Assert.Equal(serviceScope.ServiceProvider, capturedServiceProvider);
57+
}
58+
}
59+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using EntityFrameworkCore.Triggered.Transactions.Tests.Stubs;
7+
using Microsoft.AspNetCore.Http;
8+
using Microsoft.EntityFrameworkCore;
9+
using Microsoft.Extensions.DependencyInjection;
10+
using Xunit;
11+
12+
namespace EntityFrameworkCore.Triggered.AspNetCore.Tests.Extensions
13+
{
14+
public class TriggersContextOptionsBuilderExtensionsTests
15+
{
16+
public class TestModel
17+
{
18+
public Guid Id { get; set; }
19+
}
20+
21+
public class TestDbContext : TriggeredDbContext
22+
{
23+
public TestDbContext(DbContextOptions options) : base(options)
24+
{
25+
}
26+
27+
public DbSet<TestModel> TestModels { get; set; }
28+
}
29+
30+
[Fact]
31+
public void UseAspNetCoreIntegration_RegistersHttpContextServiceProviderAccessor()
32+
{
33+
IServiceProvider capturedServiceProvider = null;
34+
35+
var serviceProvider = new ServiceCollection()
36+
.AddSingleton<IHttpContextAccessor, Stubs.HttpContextAccessorStub>()
37+
.AddDbContext<TestDbContext>(options => {
38+
options.UseInMemoryDatabase("UseAspNetCoreIntegration_RegistersHttpContextServiceProviderAccessor");
39+
options.UseTriggers(triggerOptions => {
40+
triggerOptions.UseAspNetCoreIntegration();
41+
});
42+
})
43+
.AddTransient<IBeforeSaveTrigger<TestModel>>(serviceProvider => {
44+
capturedServiceProvider = serviceProvider;
45+
return new TriggerStub<TestModel>();
46+
})
47+
.BuildServiceProvider();
48+
49+
50+
using var serviceScope = serviceProvider.CreateScope();
51+
serviceProvider.GetRequiredService<IHttpContextAccessor>().HttpContext.RequestServices = serviceScope.ServiceProvider;
52+
var dbContext = serviceScope.ServiceProvider.GetRequiredService<TestDbContext>();
53+
54+
dbContext.Add(new TestModel { });
55+
dbContext.SaveChanges();
56+
57+
Assert.NotNull(capturedServiceProvider);
58+
Assert.Equal(serviceScope.ServiceProvider, capturedServiceProvider);
59+
}
60+
}
61+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Microsoft.AspNetCore.Http;
7+
8+
namespace EntityFrameworkCore.Triggered.AspNetCore.Tests.Stubs
9+
{
10+
public class HttpContextAccessorStub : IHttpContextAccessor
11+
{
12+
public HttpContext HttpContext { get; set; } = new DefaultHttpContext();
13+
}
14+
}

0 commit comments

Comments
 (0)