-
Notifications
You must be signed in to change notification settings - Fork 119
feat(Isolation): Added isolation support #28
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
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
a2e246f
feat(UoW): CreateDbContext supports lazy loading
zhenlei520 21810d9
feat: support Isolation
zhenlei520 c2e2365
test: add Isolation UnitTest
zhenlei520 e6b08cc
chore: remove invalid references
zhenlei520 982c915
test(Isolation): Completing unit tests
zhenlei520 f90c48a
chore(IntegrationEvents.Dapr): Adjust UoW enable check prompt
zhenlei520 ae7633d
chore: update masa.utils library package version to 0.4.0-preview2
zhenlei520 d6074f8
Test(Isolation): adjust Isolation UnitTest
zhenlei520 e03697d
chore(Isolation): Modify the Isolation document, Adjust the IEnvironm…
zhenlei520 d7a3842
docs(Isolation): change IEnvironment to IMultiEnvironment
zhenlei520 345a889
docs(Isolation): change Isolation doc
zhenlei520 b3d3aa8
Update README.md
zhenlei520 c2a5ea7
docs(Isolation): delete erroneous documents
zhenlei520 7ff7d47
refactor(Isolation): Adjust message implementation
zhenlei520 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule MASA.BuildingBlocks
updated
21 files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
src/Isolation/Masa.Contrib.Isolation.Environment/EnvironmentContext.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| namespace Masa.Contrib.Isolation.Environment; | ||
|
|
||
| public class EnvironmentContext : IEnvironmentContext, IEnvironmentSetter | ||
| { | ||
| public string CurrentEnvironment { get; private set; } = string.Empty; | ||
|
|
||
| public void SetEnvironment(string environment) => CurrentEnvironment = environment; | ||
| } |
23 changes: 23 additions & 0 deletions
23
src/Isolation/Masa.Contrib.Isolation.Environment/EnvironmentSaveChangesFilter.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| namespace Masa.Contrib.Isolation.Environment; | ||
|
|
||
| public class EnvironmentSaveChangesFilter: ISaveChangesFilter | ||
| { | ||
| private readonly IEnvironmentContext _environmentContext; | ||
|
|
||
| public EnvironmentSaveChangesFilter(IEnvironmentContext environmentContext) | ||
| { | ||
| _environmentContext = environmentContext; | ||
| } | ||
|
|
||
| public void OnExecuting(ChangeTracker changeTracker) | ||
| { | ||
| changeTracker.DetectChanges(); | ||
| foreach (var entity in changeTracker.Entries().Where(entry => entry.State == EntityState.Added)) | ||
| { | ||
| if (entity.Entity is IMultiEnvironment) | ||
| { | ||
| entity.CurrentValues[nameof(IMultiEnvironment.Environment)] = _environmentContext.CurrentEnvironment; | ||
| } | ||
| } | ||
| } | ||
| } |
13 changes: 13 additions & 0 deletions
13
src/Isolation/Masa.Contrib.Isolation.Environment/IsolationBuilderExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| namespace Masa.Contrib.Isolation.Environment; | ||
|
|
||
| public static class IsolationBuilderExtensions | ||
| { | ||
| public static IIsolationBuilder UseEnvironment(this IIsolationBuilder isolationBuilder) | ||
| { | ||
| isolationBuilder.Services.TryAddEnumerable(new ServiceDescriptor(typeof(ISaveChangesFilter), typeof(EnvironmentSaveChangesFilter), ServiceLifetime.Scoped)); | ||
| isolationBuilder.Services.TryAddScoped<EnvironmentContext>(); | ||
| isolationBuilder.Services.TryAddScoped(typeof(IEnvironmentContext), serviceProvider => serviceProvider.GetRequiredService<EnvironmentContext>()); | ||
| isolationBuilder.Services.TryAddScoped(typeof(IEnvironmentSetter), serviceProvider => serviceProvider.GetRequiredService<EnvironmentContext>()); | ||
| return isolationBuilder; | ||
| } | ||
| } |
17 changes: 17 additions & 0 deletions
17
src/Isolation/Masa.Contrib.Isolation.Environment/Masa.Contrib.Isolation.Environment.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net6.0</TargetFramework> | ||
| <Nullable>enable</Nullable> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\..\BuildingBlocks\MASA.BuildingBlocks\src\Isolation\Masa.BuildingBlocks.Isolation\Masa.BuildingBlocks.Isolation.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Masa.Utils.Data.EntityFrameworkCore" Version="0.4.0-preview.2" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
88 changes: 88 additions & 0 deletions
88
src/Isolation/Masa.Contrib.Isolation.Environment/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| [中](README.zh-CN.md) | EN | ||
|
|
||
| ## Masa.Contrib.Isolation.Environment | ||
|
|
||
| Example: | ||
|
|
||
| ```C# | ||
| Install-Package Masa.Contrib.Isolation.UoW.EF | ||
| Install-Package Masa.Contrib.Isolation.Environment | ||
| Install-Package Masa.Utils.Data.EntityFrameworkCore.SqlServer | ||
| ``` | ||
|
|
||
| 1. 配置appsettings.json | ||
| ``` appsettings.json | ||
| { | ||
| "ConnectionStrings": { | ||
| "DefaultConnection": "server=localhost;uid=sa;pwd=P@ssw0rd;database=identity;", | ||
| "Isolations": [ | ||
| { | ||
| "Environment": "development", | ||
| "ConnectionString": "server=localhost,1674;uid=sa;pwd=P@ssw0rd;database=identity;" | ||
| }, | ||
| { | ||
| "Environment": "staging", | ||
| "ConnectionString": "server=localhost,1672;uid=sa;pwd=P@ssw0rd;database=identity;" | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| ``` | ||
| * 1.1 When the current environment is development: database address: server=localhost,1674;uid=sa;pwd=P@ssw0rd;database=identity; | ||
| * 1.2 When the current environment is staging: database address: server=localhost,1672;uid=sa;pwd=P@ssw0rd;database=identity; | ||
| * 1.3 When the current environment is another environment: database address: server=localhost;uid=sa;pwd=P@ssw0rd;database=identity; | ||
|
|
||
| 2. Using Isolation.UoW.EF | ||
| ```` C# | ||
| builder.Services.AddEventBus(eventBusBuilder => | ||
| { | ||
| eventBusBuilder.UseIsolationUoW<CustomDbContext>( | ||
| dbOptions => dbOptions.UseSqlServer(), | ||
| isolationBuilder => isolationBuilder.UseEnvironment()); | ||
| }); | ||
| ```` | ||
|
|
||
| 3. DbContext needs to inherit IsolationDbContext | ||
|
|
||
| ```` C# | ||
| public class CustomDbContext : IsolationDbContext | ||
| { | ||
| public CustomDbContext(MasaDbContextOptions<CustomDbContext> options) : base(options) | ||
| { | ||
| } | ||
| } | ||
| ```` | ||
|
|
||
| 4. The class corresponding to the isolated table needs to implement IMultiEnvironment | ||
|
|
||
| You can also choose not to implement IMultiEnvironment when using physical isolation | ||
|
|
||
| ##### Summarize | ||
|
|
||
| * How is the environment resolved in the controller or MinimalAPI? | ||
| * The environment provides one parser by default, and the execution order is: EnvironmentVariablesParserProvider (gets the parameters in the system environment variables, the parameters of the default environment isolation: ASPNETCORE_ENVIRONMENT) | ||
| * If the parser fails to resolve the environment, what is the last database used? | ||
| * If the parsing environment fails, return DefaultConnection directly | ||
| * How to change the default environment parameter name | ||
|
|
||
| ```` C# | ||
| builder.Services.AddEventBus(eventBusBuilder => | ||
| { | ||
| eventBusBuilder.UseIsolationUoW<CustomDbContext>( | ||
| dbOptions => dbOptions.UseSqlServer(), | ||
| isolationBuilder => isolationBuilder.SetEnvironmentKey("env").UseEnvironment());// Use environment isolation | ||
| }); | ||
| ```` | ||
| * How to change the parser | ||
|
|
||
| ```` C# | ||
| builder.Services.AddEventBus(eventBusBuilder => | ||
| { | ||
| eventBusBuilder.UseIsolationUoW<CustomDbContext>( | ||
| dbOptions => dbOptions.UseSqlServer(), | ||
| isolationBuilder => isolationBuilder.SetEnvironmentParsers(new List<IEnvironmentParserProvider>() | ||
| { | ||
| new EnvironmentVariablesParserProvider() //By default, environment information in environment isolation is obtained from system environment variables | ||
| }).UseEnvironment());// Use environment isolation | ||
| }); | ||
| ```` | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.