Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add IdentityModel #81

Merged
merged 23 commits into from
Jun 14, 2022
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
707c9a7
feat(Identity): Add Identity
zhenlei520 May 27, 2022
90f9370
chore: Adjust the parser
zhenlei520 Jun 9, 2022
aba12a6
chore: update branch
zhenlei520 Jun 9, 2022
0727ff9
Merge branch 'main' of https://github.com/masastack/MASA.Contrib
zhenlei520 Jun 9, 2022
5a04b6a
feat(Identity): Add Identity
zhenlei520 Jun 10, 2022
ce33287
docs(Identity): Add Readme
zhenlei520 Jun 10, 2022
6ca603e
chore:update documentation
zhenlei520 Jun 10, 2022
7c64eda
test: Update unit tests
zhenlei520 Jun 10, 2022
484aedf
chore: Change parameter to property
zhenlei520 Jun 10, 2022
4325882
refactor(Identity): Refactor Identity
zhenlei520 Jun 13, 2022
422c9ad
chore: update branch
zhenlei520 Jun 13, 2022
d9342e6
Merge branch 'feature/parser' of https://github.com/masastack/MASA.Co…
zhenlei520 Jun 13, 2022
9882dba
chore(Identity): Add Default ClaimType
zhenlei520 Jun 13, 2022
d55c9e3
chore: Modify class name
zhenlei520 Jun 13, 2022
6d58bcf
Update class library named Masa.Contrib.IdentityModel
zhenlei520 Jun 14, 2022
1871ca5
Merge branch 'main' of https://github.com/masastack/MASA.Contrib into…
zhenlei520 Jun 14, 2022
a2e356b
chore: global sort
zhenlei520 Jun 14, 2022
3025371
fix: IUserContext is optional
zhenlei520 Jun 14, 2022
84ca5d3
chore: InvokeDelegate parameter allows null
zhenlei520 Jun 14, 2022
1e2a96b
rename: Masa.Contrib.IdentityModel rename to Masa.Contrib.Identity.Id…
zhenlei520 Jun 14, 2022
ce5b03a
rename: Masa.Contrib.IdentityModel.Tests rename to Masa.Contrib.Ident…
zhenlei520 Jun 14, 2022
9704a05
chore: Modify the method name AddMasaIdentity to AddMasaIdentityModel
zhenlei520 Jun 14, 2022
4751e7e
chore: using global using
zhenlei520 Jun 14, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@
<MicrosoftTeskSdkPackageVersion>16.9.4</MicrosoftTeskSdkPackageVersion>
<CoverletPackageVersion>3.0.2</CoverletPackageVersion>
<MoqPackageVersion>4.16.1</MoqPackageVersion>
<MasaUtilsPackageVersion>0.4.0</MasaUtilsPackageVersion>
<MasaUtilsPackageVersion>0.5.0-preview.1</MasaUtilsPackageVersion>
<MapsterPackageVersion>7.3.0</MapsterPackageVersion>
<DaprPackageVersion>1.5.0</DaprPackageVersion>

36 changes: 36 additions & 0 deletions Masa.Contrib.sln
Original file line number Diff line number Diff line change
@@ -223,10 +223,16 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.Contrib.BasicAbility.A
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.Contrib.Data.IdGenerator.Snowflake.Distributed.Redis", "src\Data\IdGenerator\Masa.Contrib.Data.IdGenerator.Snowflake.Distributed.Redis\Masa.Contrib.Data.IdGenerator.Snowflake.Distributed.Redis.csproj", "{DC50078D-D706-4CB9-A301-F47CB3F46007}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Masa.BuildingBlocks.Identity.IdentityModel", "src\BuildingBlocks\MASA.BuildingBlocks\src\Identity\Masa.BuildingBlocks.Identity.IdentityModel\Masa.BuildingBlocks.Identity.IdentityModel.csproj", "{2FD02408-DF1D-48E0-94F2-9910E76D8B6E}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "IdGenerator", "IdGenerator", "{E6363F59-2BA4-4AA7-8578-C433A2C2567F}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Identity", "Identity", "{8E9691A2-BBCA-4A0F-906D-61D159F78B3B}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.Contrib.Data.IdGenerator.Snowflake.BenchmarkDotnet.Tests", "test\Masa.Contrib.Data.IdGenerator.Snowflake.BenchmarkDotnet.Tests\Masa.Contrib.Data.IdGenerator.Snowflake.BenchmarkDotnet.Tests.csproj", "{D8E85337-A779-48FC-B822-CE8A850661DC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Masa.Contrib.Identity.IdentityModel", "src\Identity\Masa.Contrib.Identity.IdentityModel\Masa.Contrib.Identity.IdentityModel.csproj", "{A0185D9A-69AB-4668-97F6-132C7D0947AA}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.BuildingBlocks.Data.MappingExtensions", "src\BuildingBlocks\MASA.BuildingBlocks\src\Data\Masa.BuildingBlocks.Data.MappingExtensions\Masa.BuildingBlocks.Data.MappingExtensions.csproj", "{30DC35DF-9D86-443C-B26C-9053E6FCA434}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.Contrib.Data.IdGenerator.NormalGuid", "src\Data\IdGenerator\Masa.Contrib.Data.IdGenerator.NormalGuid\Masa.Contrib.Data.IdGenerator.NormalGuid.csproj", "{24E5BF57-33B6-4D5B-A32F-2F64D1A5954F}"
@@ -235,6 +241,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.Contrib.Data.IdGenerat
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Masa.Contrib.Data.IdGenerator.SequentialGuid.Tests", "test\Masa.Contrib.Data.IdGenerator.SequentialGuid.Tests\Masa.Contrib.Data.IdGenerator.SequentialGuid.Tests.csproj", "{32488758-25C9-4257-9B98-DAA034C19909}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Masa.Contrib.Identity.IdentityModel.Tests", "test\Masa.Contrib.Identity.IdentityModel.Tests\Masa.Contrib.Identity.IdentityModel.Tests.csproj", "{E08AF686-91D4-48B4-B5DF-90474D18E984}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -931,6 +939,22 @@ Global
{D8E85337-A779-48FC-B822-CE8A850661DC}.Release|Any CPU.Build.0 = Release|Any CPU
{D8E85337-A779-48FC-B822-CE8A850661DC}.Release|x64.ActiveCfg = Release|Any CPU
{D8E85337-A779-48FC-B822-CE8A850661DC}.Release|x64.Build.0 = Release|Any CPU
{2FD02408-DF1D-48E0-94F2-9910E76D8B6E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2FD02408-DF1D-48E0-94F2-9910E76D8B6E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2FD02408-DF1D-48E0-94F2-9910E76D8B6E}.Debug|x64.ActiveCfg = Debug|Any CPU
{2FD02408-DF1D-48E0-94F2-9910E76D8B6E}.Debug|x64.Build.0 = Debug|Any CPU
{2FD02408-DF1D-48E0-94F2-9910E76D8B6E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2FD02408-DF1D-48E0-94F2-9910E76D8B6E}.Release|Any CPU.Build.0 = Release|Any CPU
{2FD02408-DF1D-48E0-94F2-9910E76D8B6E}.Release|x64.ActiveCfg = Release|Any CPU
{2FD02408-DF1D-48E0-94F2-9910E76D8B6E}.Release|x64.Build.0 = Release|Any CPU
{A0185D9A-69AB-4668-97F6-132C7D0947AA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A0185D9A-69AB-4668-97F6-132C7D0947AA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A0185D9A-69AB-4668-97F6-132C7D0947AA}.Debug|x64.ActiveCfg = Debug|Any CPU
{A0185D9A-69AB-4668-97F6-132C7D0947AA}.Debug|x64.Build.0 = Debug|Any CPU
{A0185D9A-69AB-4668-97F6-132C7D0947AA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A0185D9A-69AB-4668-97F6-132C7D0947AA}.Release|Any CPU.Build.0 = Release|Any CPU
{A0185D9A-69AB-4668-97F6-132C7D0947AA}.Release|x64.ActiveCfg = Release|Any CPU
{A0185D9A-69AB-4668-97F6-132C7D0947AA}.Release|x64.Build.0 = Release|Any CPU
{30DC35DF-9D86-443C-B26C-9053E6FCA434}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{30DC35DF-9D86-443C-B26C-9053E6FCA434}.Debug|Any CPU.Build.0 = Debug|Any CPU
{30DC35DF-9D86-443C-B26C-9053E6FCA434}.Debug|x64.ActiveCfg = Debug|Any CPU
@@ -963,6 +987,14 @@ Global
{32488758-25C9-4257-9B98-DAA034C19909}.Release|Any CPU.Build.0 = Release|Any CPU
{32488758-25C9-4257-9B98-DAA034C19909}.Release|x64.ActiveCfg = Release|Any CPU
{32488758-25C9-4257-9B98-DAA034C19909}.Release|x64.Build.0 = Release|Any CPU
{E08AF686-91D4-48B4-B5DF-90474D18E984}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E08AF686-91D4-48B4-B5DF-90474D18E984}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E08AF686-91D4-48B4-B5DF-90474D18E984}.Debug|x64.ActiveCfg = Debug|Any CPU
{E08AF686-91D4-48B4-B5DF-90474D18E984}.Debug|x64.Build.0 = Debug|Any CPU
{E08AF686-91D4-48B4-B5DF-90474D18E984}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E08AF686-91D4-48B4-B5DF-90474D18E984}.Release|Any CPU.Build.0 = Release|Any CPU
{E08AF686-91D4-48B4-B5DF-90474D18E984}.Release|x64.ActiveCfg = Release|Any CPU
{E08AF686-91D4-48B4-B5DF-90474D18E984}.Release|x64.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@@ -1076,10 +1108,14 @@ Global
{DC50078D-D706-4CB9-A301-F47CB3F46007} = {6DB8780E-BA11-47CD-8FAB-D73A1F71B305}
{E6363F59-2BA4-4AA7-8578-C433A2C2567F} = {38E6C400-90C0-493E-9266-C1602E229F1B}
{D8E85337-A779-48FC-B822-CE8A850661DC} = {E6363F59-2BA4-4AA7-8578-C433A2C2567F}
{2FD02408-DF1D-48E0-94F2-9910E76D8B6E} = {DC578D74-98F0-4F19-A230-CFA8DAEE0AF1}
{8E9691A2-BBCA-4A0F-906D-61D159F78B3B} = {42DF7AAC-362C-48F4-B76A-BDEEEFF17CC9}
{A0185D9A-69AB-4668-97F6-132C7D0947AA} = {8E9691A2-BBCA-4A0F-906D-61D159F78B3B}
{30DC35DF-9D86-443C-B26C-9053E6FCA434} = {DC578D74-98F0-4F19-A230-CFA8DAEE0AF1}
{24E5BF57-33B6-4D5B-A32F-2F64D1A5954F} = {6DB8780E-BA11-47CD-8FAB-D73A1F71B305}
{B15F4561-E511-42EA-A02E-F772BAF41E20} = {6DB8780E-BA11-47CD-8FAB-D73A1F71B305}
{32488758-25C9-4257-9B98-DAA034C19909} = {E6363F59-2BA4-4AA7-8578-C433A2C2567F}
{E08AF686-91D4-48B4-B5DF-90474D18E984} = {38E6C400-90C0-493E-9266-C1602E229F1B}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {40383055-CC50-4600-AD9A-53C14F620D03}
2 changes: 1 addition & 1 deletion src/BuildingBlocks/MASA.BuildingBlocks
Submodule MASA.BuildingBlocks updated 28 files
+17 −7 Masa.BuildingBlocks.sln
+9 −0 src/Data/Masa.BuildingBlocks.Data/Concurrency/DefaultConcurrencyStampProvider.cs
+9 −0 src/Data/Masa.BuildingBlocks.Data/Concurrency/IConcurrencyStampProvider.cs
+9 −0 src/Data/Masa.BuildingBlocks.Data/Concurrency/IHasConcurrencyStamp.cs
+0 −1 src/Data/Masa.BuildingBlocks.Data/_Imports.cs
+9 −2 src/Dispatcher/Masa.BuildingBlocks.Dispatcher.IntegrationEvents/Logs/IntegrationEventLog.cs
+1 −0 src/Dispatcher/Masa.BuildingBlocks.Dispatcher.IntegrationEvents/_Imports.cs
+15 −0 src/Identity/Masa.BuildingBlocks.Identity.IdentityModel/DefaultTypeConvertProvider.cs
+11 −0 src/Identity/Masa.BuildingBlocks.Identity.IdentityModel/Entities/IIdentityUser.cs
+11 −0 src/Identity/Masa.BuildingBlocks.Identity.IdentityModel/Entities/IdentityUser.cs
+8 −0 src/Identity/Masa.BuildingBlocks.Identity.IdentityModel/Entities/Isolation/IIsolatedIdentityUser.cs
+9 −0 src/Identity/Masa.BuildingBlocks.Identity.IdentityModel/Entities/Isolation/IMultiEnvironmentIdentityUser.cs
+9 −0 src/Identity/Masa.BuildingBlocks.Identity.IdentityModel/Entities/Isolation/IMultiTenantIdentityUser.cs
+9 −0 src/Identity/Masa.BuildingBlocks.Identity.IdentityModel/Entities/Isolation/IsolatedIdentityUser.cs
+9 −0 src/Identity/Masa.BuildingBlocks.Identity.IdentityModel/Entities/Isolation/MultiEnvironmentIdentityUser.cs
+9 −0 src/Identity/Masa.BuildingBlocks.Identity.IdentityModel/Entities/Isolation/MultiTenantIdentityUser.cs
+23 −0 src/Identity/Masa.BuildingBlocks.Identity.IdentityModel/Enum/IdentityType.cs
+10 −0 src/Identity/Masa.BuildingBlocks.Identity.IdentityModel/IIsolatedUserContext.cs
+9 −0 src/Identity/Masa.BuildingBlocks.Identity.IdentityModel/IMultiEnvironmentUserContext.cs
+11 −0 src/Identity/Masa.BuildingBlocks.Identity.IdentityModel/IMultiTenantUserContext.cs
+9 −0 src/Identity/Masa.BuildingBlocks.Identity.IdentityModel/ITypeConvertProvider.cs
+17 −0 src/Identity/Masa.BuildingBlocks.Identity.IdentityModel/IUserContext.cs
+9 −0 src/Identity/Masa.BuildingBlocks.Identity.IdentityModel/IUserSetter.cs
+17 −0 src/Identity/Masa.BuildingBlocks.Identity.IdentityModel/Internal/DisposeAction.cs
+9 −0 src/Identity/Masa.BuildingBlocks.Identity.IdentityModel/Masa.BuildingBlocks.Identity.IdentityModel.csproj
+45 −0 src/Identity/Masa.BuildingBlocks.Identity.IdentityModel/UserContext.cs
+5 −0 src/Identity/Masa.BuildingBlocks.Identity.IdentityModel/_Imports.cs
+2 −2 src/Isolation/Masa.BuildingBlocks.Isolation/Masa.BuildingBlocks.Isolation.csproj
Original file line number Diff line number Diff line change
@@ -3,15 +3,22 @@

namespace Masa.Contrib.Data.Contracts.EF.DataFiltering;

public class SoftDeleteSaveChangesFilter<TDbContext> : ISaveChangesFilter where TDbContext : DbContext
public class SoftDeleteSaveChangesFilter<TDbContext, TUserId> : ISaveChangesFilter
where TDbContext : DbContext
where TUserId : IComparable
{
private readonly IUserContext? _userContext;
private readonly TDbContext _context;
private readonly MasaDbContextOptions<TDbContext> _masaDbContextOptions;

public SoftDeleteSaveChangesFilter(MasaDbContextOptions<TDbContext> masaDbContextOptions, TDbContext dbContext)
public SoftDeleteSaveChangesFilter(
MasaDbContextOptions<TDbContext> masaDbContextOptions,
TDbContext dbContext,
IUserContext? userContext = null)
{
_masaDbContextOptions = masaDbContextOptions;
_context = dbContext;
_userContext = userContext;
}

public void OnExecuting(ChangeTracker changeTracker)
@@ -20,15 +27,22 @@ public void OnExecuting(ChangeTracker changeTracker)
return;

changeTracker.DetectChanges();
var entries = changeTracker.Entries().Where(entry => entry.State == EntityState.Deleted);
var entries = changeTracker.Entries().Where(entry => entry.State == EntityState.Deleted && entry.Entity is ISoftDelete);
foreach (var entity in entries)
{
if (entity.Entity is ISoftDelete)
HandleNavigationEntry(entity.Navigations.Where(n => !((IReadOnlyNavigation)n.Metadata).IsOnDependent));

entity.State = EntityState.Modified;
entity.CurrentValues[nameof(ISoftDelete.IsDeleted)] = true;

if (_userContext != null && entity.Entity is IAuditEntity<TUserId> &&
entity.CurrentValues[nameof(IAuditEntity<TUserId>.Modifier)] != default)
{
HandleNavigationEntry(entity.Navigations.Where(n => !((IReadOnlyNavigation)n.Metadata).IsOnDependent));
var userId = _userContext.UserId;
if (userId != null) entity.CurrentValues[nameof(IAuditEntity<TUserId>.Modifier)] = userId;

entity.State = EntityState.Modified;
entity.CurrentValues[nameof(ISoftDelete.IsDeleted)] = true;
entity.CurrentValues[nameof(IAuditEntity<TUserId>.ModificationTime)] =
DateTime.UtcNow; //The current time to change to localization after waiting for localization
}
}
}
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ namespace Masa.Contrib.Data.Contracts.EF.Internal;

internal class InstanceBuilder
{
internal delegate object InvokeDelegate(params object[] parameters);
internal delegate object InvokeDelegate(params object?[] parameters);

public static InvokeDelegate CreateInstanceDelegate(ConstructorInfo constructorInfo)
{
Original file line number Diff line number Diff line change
@@ -45,7 +45,9 @@ private static void UseSoftDelete(this MasaDbContextOptionsBuilder masaDbContext

var masaDbContextOptionsType = typeof(MasaDbContextOptions<>).MakeGenericType(masaDbContextOptionsBuilder.DbContextType);
var softDeleteSaveChangesFilterType =
typeof(SoftDeleteSaveChangesFilter<>).MakeGenericType(masaDbContextOptionsBuilder.DbContextType);
typeof(SoftDeleteSaveChangesFilter<,>).MakeGenericType(
masaDbContextOptionsBuilder.DbContextType,
masaDbContextOptionsBuilder.UserIdType);
var constructorInfo = softDeleteSaveChangesFilterType.GetConstructors().FirstOrDefault()!;
var invokeDelegate = InstanceBuilder.CreateInstanceDelegate(constructorInfo);

@@ -55,7 +57,8 @@ private static void UseSoftDelete(this MasaDbContextOptionsBuilder masaDbContext
{
var instance = invokeDelegate.Invoke(
serviceProvider.GetRequiredService(masaDbContextOptionsType),
serviceProvider.GetRequiredService(masaDbContextOptionsBuilder.DbContextType));
serviceProvider.GetRequiredService(masaDbContextOptionsBuilder.DbContextType),
serviceProvider.GetService<IUserContext>());
return instance;
},
ServiceLifetime.Scoped));
2 changes: 2 additions & 0 deletions src/Data/Masa.Contrib.Data.Contracts.EF/_Imports.cs
Original file line number Diff line number Diff line change
@@ -2,6 +2,8 @@
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

global using Masa.BuildingBlocks.Data.Contracts.DataFiltering;
global using Masa.BuildingBlocks.Ddd.Domain.Entities.Auditing;
global using Masa.BuildingBlocks.Identity.IdentityModel;
global using Masa.Contrib.Data.Contracts.EF.DataFiltering;
global using Masa.Contrib.Data.Contracts.EF.Internal;
global using Masa.Contrib.Data.Contracts.EF.Options;
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.Contrib.Data.EntityFrameworkCore.Filters;

public class SaveChangeFilter<TDbContext, TUserId> : ISaveChangesFilter
where TDbContext : DbContext
{
private readonly IUserContext? _userContext;

public SaveChangeFilter(IUserContext? userContext = null)
{
_userContext = userContext;
}

public void OnExecuting(ChangeTracker changeTracker)
{
changeTracker.DetectChanges();

foreach (var entity in changeTracker.Entries()
.Where(entry => entry.Entity is IAuditEntity<TUserId> &&
(entry.State == EntityState.Added || entry.State == EntityState.Modified)))
{
var userId = _userContext?.UserId;
if (entity.State == EntityState.Added)
{
if (userId != null)
{
entity.CurrentValues[nameof(IAuditEntity<TUserId>.Creator)] = userId;
entity.CurrentValues[nameof(IAuditEntity<TUserId>.Modifier)] = userId;
}
entity.CurrentValues[nameof(IAuditEntity<TUserId>.CreationTime)] =
DateTime.UtcNow; //The current time to change to localization after waiting for localization
}
else if (entity.CurrentValues[nameof(IAuditEntity<TUserId>.Modifier)] != default && userId != null)
{
entity.CurrentValues[nameof(IAuditEntity<TUserId>.Modifier)] = userId;
}
entity.CurrentValues[nameof(IAuditEntity<TUserId>.ModificationTime)] =
DateTime.UtcNow; //The current time to change to localization after waiting for localization
}
}
}
Original file line number Diff line number Diff line change
@@ -18,6 +18,7 @@
<ProjectReference Include="..\..\BuildingBlocks\MASA.BuildingBlocks\src\Data\Masa.BuildingBlocks.Data.Contracts\Masa.BuildingBlocks.Data.Contracts.csproj" />
<ProjectReference Include="..\..\BuildingBlocks\MASA.BuildingBlocks\src\Data\Masa.BuildingBlocks.Data\Masa.BuildingBlocks.Data.csproj" />
<ProjectReference Include="..\..\BuildingBlocks\MASA.BuildingBlocks\src\Ddd\Masa.BuildingBlocks.Ddd.Domain\Masa.BuildingBlocks.Ddd.Domain.csproj" />
<ProjectReference Include="..\..\BuildingBlocks\MASA.BuildingBlocks\src\Identity\Masa.BuildingBlocks.Identity.IdentityModel\Masa.BuildingBlocks.Identity.IdentityModel.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -13,13 +13,16 @@ public class MasaDbContextOptionsBuilder

public Type DbContextType { get; }

public Type UserIdType { get; }

public Action<IServiceProvider, DbContextOptionsBuilder> Builder { get; set; } = default!;

public bool EnableSoftDelete { get; set; }

public MasaDbContextOptionsBuilder(IServiceCollection services, Type dbContextType)
public MasaDbContextOptionsBuilder(IServiceCollection services, Type dbContextType, Type userIdType)
{
Services = services;
DbContextType = dbContextType;
UserIdType = userIdType;
}
}
Original file line number Diff line number Diff line change
@@ -11,21 +11,31 @@ public static IServiceCollection AddMasaDbContext<TDbContextImplementation>(
ServiceLifetime contextLifetime = ServiceLifetime.Scoped,
ServiceLifetime optionsLifetime = ServiceLifetime.Scoped)
where TDbContextImplementation : MasaDbContext, IMasaDbContext
=> services.AddMasaDbContext<TDbContextImplementation, Guid>(optionsBuilder, contextLifetime, optionsLifetime);

public static IServiceCollection AddMasaDbContext<TDbContextImplementation, TUserId>(
this IServiceCollection services,
Action<MasaDbContextOptionsBuilder>? optionsBuilder = null,
ServiceLifetime contextLifetime = ServiceLifetime.Scoped,
ServiceLifetime optionsLifetime = ServiceLifetime.Scoped)
where TDbContextImplementation : MasaDbContext, IMasaDbContext
where TUserId : IComparable
=> services
.AddDbContext<TDbContextImplementation>(contextLifetime, optionsLifetime)
.AddCoreServices<TDbContextImplementation>(optionsBuilder, optionsLifetime);
.AddCoreServices<TDbContextImplementation, TUserId>(optionsBuilder, optionsLifetime);

private static IServiceCollection AddCoreServices<TDbContextImplementation>(
private static IServiceCollection AddCoreServices<TDbContextImplementation, TUserId>(
this IServiceCollection services,
Action<MasaDbContextOptionsBuilder>? optionsBuilder,
ServiceLifetime optionsLifetime)
where TDbContextImplementation : MasaDbContext, IMasaDbContext
where TUserId : IComparable
{
services.TryAddConfigure<MasaDbConnectionOptions>();

MasaDbContextOptionsBuilder masaBuilder = new MasaDbContextOptionsBuilder(services, typeof(TDbContextImplementation));
MasaDbContextOptionsBuilder masaBuilder = new(services, typeof(TDbContextImplementation), typeof(TUserId));
optionsBuilder?.Invoke(masaBuilder);
return services.AddCoreServices<TDbContextImplementation>((serviceProvider, efDbContextOptionsBuilder) =>
return services.AddCoreServices<TDbContextImplementation, TUserId>((serviceProvider, efDbContextOptionsBuilder) =>
{
if (masaBuilder.EnableSoftDelete)
efDbContextOptionsBuilder.UseSoftDelete();
@@ -34,11 +44,12 @@ private static IServiceCollection AddCoreServices<TDbContextImplementation>(
}, optionsLifetime);
}

private static IServiceCollection AddCoreServices<TDbContextImplementation>(
private static IServiceCollection AddCoreServices<TDbContextImplementation, TUserId>(
this IServiceCollection services,
Action<IServiceProvider, EFDbContextOptionsBuilder>? optionsBuilder,
ServiceLifetime optionsLifetime)
where TDbContextImplementation : MasaDbContext, IMasaDbContext
where TUserId : IComparable
{
services.TryAddSingleton<IConcurrencyStampProvider, DefaultConcurrencyStampProvider>();
services.TryAddScoped<IConnectionStringProvider, DefaultConnectionStringProvider>();
@@ -55,6 +66,8 @@ private static IServiceCollection AddCoreServices<TDbContextImplementation>(
typeof(MasaDbContextOptions),
serviceProvider => serviceProvider.GetRequiredService<MasaDbContextOptions<TDbContextImplementation>>(),
optionsLifetime));

services.TryAdd(new ServiceDescriptor(typeof(ISaveChangesFilter), typeof(SaveChangeFilter<TDbContextImplementation, TUserId>), ServiceLifetime.Scoped));
return services;
}

2 changes: 2 additions & 0 deletions src/Data/Masa.Contrib.Data.EntityFrameworkCore/_Imports.cs
Original file line number Diff line number Diff line change
@@ -6,7 +6,9 @@
global using Masa.BuildingBlocks.Data.Contracts.DataFiltering;
global using Masa.BuildingBlocks.Data.Options;
global using Masa.BuildingBlocks.Ddd.Domain.Entities;
global using Masa.BuildingBlocks.Ddd.Domain.Entities.Auditing;
global using Masa.BuildingBlocks.Ddd.Domain.Events;
global using Masa.BuildingBlocks.Identity.IdentityModel;
global using Masa.Contrib.Data.EntityFrameworkCore.Filters;
global using Microsoft.EntityFrameworkCore;
global using Microsoft.EntityFrameworkCore.ChangeTracking;
Loading