-
Notifications
You must be signed in to change notification settings - Fork 119
feat(UoW): add IUnitOfWorkAccessor and IUnitOfWorkManager support CreateDbContextAsync #27
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
11 commits
Select commit
Hold shift + click to select a range
3024430
feat(uoW): add IUnitOfWorkManager, Support for creating new DbContext…
zhenlei520 c45bc0d
test: Adapt unit tests to new background tasks
zhenlei520 6555898
test(uoW): Added UnitOfWorkManager unit test
zhenlei520 831f749
feat(UoW): add IUnitOfWorkAccessor and IUnitOfWorkManager support Cre…
zhenlei520 45352f0
test(uoW): Added UnitOfWorkAccessor unit test
zhenlei520 de59a29
chore(uoW): Change class name IDataConnectionStringProvider to IDbCon…
zhenlei520 86015a4
docs(UoW): Modify readme title error
zhenlei520 8f94d2d
refactor: CreateDbContext is modified to create sync
zhenlei520 a21e5b5
Merge branch 'main' of https://github.com/masastack/MASA.Contrib into…
zhenlei520 d6f5949
feat(uoW): Extend UseUoW method for IEventBusBuilder
zhenlei520 81ccaae
docs(uoW): Adjust uoW documentation
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
Submodule MASA.BuildingBlocks
updated
13 files
13 changes: 13 additions & 0 deletions
13
src/Data/Masa.Contrib.Data.UoW.EF/DbConnectionStringProvider.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.Data.UoW.EF; | ||
|
|
||
| public class DbConnectionStringProvider : BaseDbConnectionStringProvider | ||
| { | ||
| private readonly IOptionsMonitor<MasaDbConnectionOptions> _options; | ||
|
|
||
| public DbConnectionStringProvider(IOptionsMonitor<MasaDbConnectionOptions> options) => _options = options; | ||
|
|
||
| protected override List<DbContextOptions> GetDbContextOptionsList() | ||
| { | ||
| return new() { new(_options.CurrentValue.DefaultConnection) }; | ||
| } | ||
| } |
33 changes: 33 additions & 0 deletions
33
src/Data/Masa.Contrib.Data.UoW.EF/DefaultConnectionStringProvider.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,33 @@ | ||
| namespace Masa.Contrib.Data.UoW.EF; | ||
|
|
||
| public class DefaultConnectionStringProvider : IConnectionStringProvider | ||
| { | ||
| private readonly IUnitOfWorkAccessor _unitOfWorkAccessor; | ||
| private readonly IOptionsSnapshot<MasaDbConnectionOptions> _options; | ||
| private readonly ILogger<DefaultConnectionStringProvider>? _logger; | ||
|
|
||
| public DefaultConnectionStringProvider( | ||
| IUnitOfWorkAccessor unitOfWorkAccessor, | ||
| IOptionsSnapshot<MasaDbConnectionOptions> options, | ||
| ILogger<DefaultConnectionStringProvider>? logger = null) | ||
| { | ||
| _unitOfWorkAccessor = unitOfWorkAccessor; | ||
| _options = options; | ||
| _logger = logger; | ||
| } | ||
|
|
||
| public Task<string> GetConnectionStringAsync() => Task.FromResult(GetConnectionString()); | ||
|
|
||
| public string GetConnectionString() | ||
| { | ||
| if (_unitOfWorkAccessor.CurrentDbContextOptions != null) | ||
| return _unitOfWorkAccessor.CurrentDbContextOptions.ConnectionString; | ||
|
|
||
| var connectionString = _options.Value.DefaultConnection; | ||
| if (string.IsNullOrEmpty(connectionString)) | ||
| _logger?.LogError("Failed to get database connection string, please check whether the configuration of IOptionsSnapshot<MasaDbConnectionOptions> is abnormal"); | ||
|
|
||
| _unitOfWorkAccessor.CurrentDbContextOptions = new MasaDbContextConfigurationOptions(connectionString); | ||
| return connectionString; | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,25 @@ | ||
| [中](README.zh-CN.md) | EN | ||
|
|
||
| ## Contracts.EF | ||
| ## UoW.EF | ||
|
|
||
| Example: | ||
|
|
||
| ```C# | ||
| Install-Package Masa.Contrib.Dispatcher.Events | ||
| Install-Package Masa.Contrib.Data.UoW.EF | ||
| Install-Package Masa.Utils.Data.EntityFrameworkCore.SqlServer | ||
| ``` | ||
|
|
||
| 1. Configure appsettings.json | ||
| ``` appsettings.json | ||
| { | ||
| "ConnectionStrings": { | ||
| "DefaultConnection": "server=localhost;uid=sa;pwd=P@ssw0rd;database=identity" | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| 2. Use UoW | ||
| ```C# | ||
| builder.Services.AddEventBus(options => { | ||
| options.UseUoW<CustomDbContext>(dbOptions => dbOptions.UseSqlServer("server=localhost;uid=sa;pwd=P@ssw0rd;database=identity")); | ||
| }); | ||
| builder.Services.AddEventBus(eventBusBuilder => eventBusBuilder.UseUoW<CustomDbContext>(dbOptions => dbOptions.UseSqlServer())); | ||
| ``` |
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 |
|---|---|---|
| @@ -1,16 +1,25 @@ | ||
| 中 | [EN](README.md) | ||
|
|
||
| ## Contracts.EF | ||
| ## UoW.EF | ||
|
|
||
| 用例: | ||
|
|
||
| ```C# | ||
| Install-Package Masa.Contrib.Dispatcher.Events | ||
| Install-Package Masa.Contrib.Data.UoW.EF | ||
| Install-Package Masa.Utils.Data.EntityFrameworkCore.SqlServer | ||
| ``` | ||
|
|
||
| 1. 配置appsettings.json | ||
| ``` appsettings.json | ||
| { | ||
| "ConnectionStrings": { | ||
| "DefaultConnection": "server=localhost;uid=sa;pwd=P@ssw0rd;database=identity" | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| 2. 使用UoW | ||
| ```C# | ||
| builder.Services.AddEventBus(options => { | ||
| options.UseUoW<CustomDbContext>(dbOptions => dbOptions.UseSqlServer("server=localhost;uid=sa;pwd=P@ssw0rd;database=identity")); | ||
| }); | ||
| ``` | ||
| builder.Services.AddEventBus(eventBusBuilder => eventBusBuilder.UseUoW<CustomDbContext>(dbOptions => dbOptions.UseSqlServer())); | ||
| ``` |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| namespace Masa.Contrib.Data.UoW.EF; | ||
|
|
||
| public class UnitOfWorkAccessor : IUnitOfWorkAccessor | ||
| { | ||
| public MasaDbContextConfigurationOptions? CurrentDbContextOptions { get; set; } | ||
| } |
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,26 @@ | ||
| namespace Masa.Contrib.Data.UoW.EF; | ||
|
|
||
| public class UnitOfWorkManager : IUnitOfWorkManager | ||
| { | ||
| private readonly IServiceProvider _serviceProvider; | ||
|
|
||
| public UnitOfWorkManager(IServiceProvider serviceProvider) => _serviceProvider = serviceProvider; | ||
|
|
||
| public IUnitOfWork CreateDbContext() | ||
| { | ||
| var scope = _serviceProvider.CreateAsyncScope(); | ||
| return scope.ServiceProvider.GetRequiredService<IUnitOfWork>(); | ||
| } | ||
|
|
||
| public IUnitOfWork CreateDbContext(MasaDbContextConfigurationOptions options) | ||
| { | ||
| ArgumentNullException.ThrowIfNull(options, nameof(options)); | ||
| if (string.IsNullOrEmpty(options.ConnectionString)) | ||
| throw new ArgumentException($"Invalid {nameof(options)}"); | ||
|
|
||
| var scope = _serviceProvider.CreateAsyncScope(); | ||
| var unitOfWorkAccessor = scope.ServiceProvider.GetRequiredService<IUnitOfWorkAccessor>(); | ||
| unitOfWorkAccessor.CurrentDbContextOptions = options; | ||
| return scope.ServiceProvider.GetRequiredService<IUnitOfWork>(); | ||
| } | ||
| } |
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 |
|---|---|---|
| @@ -1,11 +1,14 @@ | ||
| global using Masa.BuildingBlocks.Data.UoW; | ||
| global using Masa.BuildingBlocks.Data.UoW.Options; | ||
| global using Masa.BuildingBlocks.Dispatcher.Events; | ||
| global using Masa.Utils.Data.EntityFrameworkCore; | ||
| global using Microsoft.EntityFrameworkCore; | ||
| global using Microsoft.EntityFrameworkCore.Storage; | ||
| global using Microsoft.Extensions.DependencyInjection; | ||
| global using Microsoft.Extensions.DependencyInjection.Extensions; | ||
| global using Microsoft.Extensions.Logging; | ||
| global using Microsoft.Extensions.Options; | ||
| global using System.Data.Common; | ||
| global using System.Text.Json.Serialization; | ||
| global using EntityState = Masa.BuildingBlocks.Data.UoW.EntityState; | ||
|
|
||
| global using DbContextOptions = Masa.BuildingBlocks.Data.UoW.Options.MasaDbContextConfigurationOptions; |
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
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.