Skip to content

Commit

Permalink
refactor: Add MasaDbContext and support no-argument constructor (#532)
Browse files Browse the repository at this point in the history
* rename: callerOptions -> callerBuilder

* chore: Adjust MasaDbContext to support no-argument constructor

* feat: Support OnConfiguring

* chore: remove  EnablePluralizingTableName

* feat: Support no-argument constructor

* rename: Masa.Contrib.Data.EFCore.Tests.Scenes.Isolation -> Masa.Contrib.Data.EFCore.Tests.Scenes.Isolation

* chore: remove invalid references

* chore: Use lazy loading

* chore: remove dead code

* chore: Dealing with Code Smells

* chore: Dealing with Code Smells

* chore: Modify inherited class
  • Loading branch information
zhenlei520 authored Mar 31, 2023
1 parent e563920 commit 98b20d4
Show file tree
Hide file tree
Showing 40 changed files with 865 additions and 476 deletions.
26 changes: 12 additions & 14 deletions Masa.Framework.sln
Original file line number Diff line number Diff line change
Expand Up @@ -695,9 +695,9 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Masa.Contrib.Dispatcher.Eve
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Masa.Contrib.Data.UoW.EFCore.Tests", "src\Contrib\Data\UoW\Tests\Masa.Contrib.Data.UoW.EFCore.Tests\Masa.Contrib.Data.UoW.EFCore.Tests.csproj", "{9208B446-A7E0-4B5A-A42D-37E63B76E875}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Scenes", "Scenes", "{361A7C4C-20A7-4799-B4D6-5AFA656C3FA3}"
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Scenes", "Scenes", "{02591EE4-1033-4575-984F-7AB290E7C6FA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Masa.Contrib.Data.EFCore.Tests.Scenes.Isolation", "src\Contrib\Data\Orm\EFCore\Scenes\Masa.Contrib.Data.EFCore.Tests.Scenes.Isolation\Masa.Contrib.Data.EFCore.Tests.Scenes.Isolation.csproj", "{472375CE-5547-46F7-8CC5-88ED69DEF230}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Masa.Contrib.Data.EFCore.Tests.Scenes.Isolation", "src\Contrib\Data\Orm\EFCore\Scenes\Masa.Contrib.Data.EFCore.Tests.Scenes.Isolation\Masa.Contrib.Data.EFCore.Tests.Scenes.Isolation.csproj", "{A5B30EEA-2DE1-4233-8584-936CB862191B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -2507,14 +2507,6 @@ Global
{1221F32F-7310-49FF-94DD-2BCF570E03F2}.Release|Any CPU.Build.0 = Release|Any CPU
{1221F32F-7310-49FF-94DD-2BCF570E03F2}.Release|x64.ActiveCfg = Release|Any CPU
{1221F32F-7310-49FF-94DD-2BCF570E03F2}.Release|x64.Build.0 = Release|Any CPU
{472375CE-5547-46F7-8CC5-88ED69DEF230}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{472375CE-5547-46F7-8CC5-88ED69DEF230}.Debug|Any CPU.Build.0 = Debug|Any CPU
{472375CE-5547-46F7-8CC5-88ED69DEF230}.Debug|x64.ActiveCfg = Debug|Any CPU
{472375CE-5547-46F7-8CC5-88ED69DEF230}.Debug|x64.Build.0 = Debug|Any CPU
{472375CE-5547-46F7-8CC5-88ED69DEF230}.Release|Any CPU.ActiveCfg = Release|Any CPU
{472375CE-5547-46F7-8CC5-88ED69DEF230}.Release|Any CPU.Build.0 = Release|Any CPU
{472375CE-5547-46F7-8CC5-88ED69DEF230}.Release|x64.ActiveCfg = Release|Any CPU
{472375CE-5547-46F7-8CC5-88ED69DEF230}.Release|x64.Build.0 = Release|Any CPU
{D1BC6F22-430A-4C5A-BDA8-5F8CC49D8E71}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D1BC6F22-430A-4C5A-BDA8-5F8CC49D8E71}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D1BC6F22-430A-4C5A-BDA8-5F8CC49D8E71}.Debug|x64.ActiveCfg = Debug|Any CPU
Expand All @@ -2523,6 +2515,14 @@ Global
{D1BC6F22-430A-4C5A-BDA8-5F8CC49D8E71}.Release|Any CPU.Build.0 = Release|Any CPU
{D1BC6F22-430A-4C5A-BDA8-5F8CC49D8E71}.Release|x64.ActiveCfg = Release|Any CPU
{D1BC6F22-430A-4C5A-BDA8-5F8CC49D8E71}.Release|x64.Build.0 = Release|Any CPU
{A5B30EEA-2DE1-4233-8584-936CB862191B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A5B30EEA-2DE1-4233-8584-936CB862191B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A5B30EEA-2DE1-4233-8584-936CB862191B}.Debug|x64.ActiveCfg = Debug|Any CPU
{A5B30EEA-2DE1-4233-8584-936CB862191B}.Debug|x64.Build.0 = Debug|Any CPU
{A5B30EEA-2DE1-4233-8584-936CB862191B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A5B30EEA-2DE1-4233-8584-936CB862191B}.Release|Any CPU.Build.0 = Release|Any CPU
{A5B30EEA-2DE1-4233-8584-936CB862191B}.Release|x64.ActiveCfg = Release|Any CPU
{A5B30EEA-2DE1-4233-8584-936CB862191B}.Release|x64.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -2865,11 +2865,9 @@ Global
{EEACDE24-9D4A-4F65-B13A-644E89A7918D} = {8A9DBB76-6618-4982-87D7-6CBD8375EB15}
{1221F32F-7310-49FF-94DD-2BCF570E03F2} = {EC7A08E9-3355-486B-BA30-41A1F8CAC5F5}
{9208B446-A7E0-4B5A-A42D-37E63B76E875} = {3C12BE69-247B-4448-9700-0ACEE8440A34}
{361A7C4C-20A7-4799-B4D6-5AFA656C3FA3} = {48853497-9C2D-4ADA-AED3-61280AF1418D}
{472375CE-5547-46F7-8CC5-88ED69DEF230} = {361A7C4C-20A7-4799-B4D6-5AFA656C3FA3}
{D1BC6F22-430A-4C5A-BDA8-5F8CC49D8E71} = {08B138B5-2599-4F42-9584-6AE736673882}
{361A7C4C-20A7-4799-B4D6-5AFA656C3FA3} = {48853497-9C2D-4ADA-AED3-61280AF1418D}
{472375CE-5547-46F7-8CC5-88ED69DEF230} = {361A7C4C-20A7-4799-B4D6-5AFA656C3FA3}
{02591EE4-1033-4575-984F-7AB290E7C6FA} = {48853497-9C2D-4ADA-AED3-61280AF1418D}
{A5B30EEA-2DE1-4233-8584-936CB862191B} = {02591EE4-1033-4575-984F-7AB290E7C6FA}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {40383055-CC50-4600-AD9A-53C14F620D03}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
[assembly: InternalsVisibleTo("Masa.BuildingBlocks.RulesEngine")]
[assembly: InternalsVisibleTo("Masa.BuildingBlocks.SearchEngine.AutoComplete")]
[assembly: InternalsVisibleTo("Masa.BuildingBlocks.Storage.ObjectStorage")]
[assembly: InternalsVisibleTo("Masa.Contrib.Data.EFCore")]

// ReSharper disable once CheckNamespace

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class OidcDbContextOptionsTest
public async Task TestSeedStandardResources1Async()
{
var services = new ServiceCollection();
services.AddMasaDbContext<CustomDbContext>(dbContext => { dbContext.UseInMemoryDatabase(Guid.NewGuid().ToString()); dbContext.EnablePluralizingTableName = true; });
services.AddMasaDbContext<CustomDbContext>(dbContext => { dbContext.UseInMemoryDatabase(Guid.NewGuid().ToString()); });

Mock<IUserClaimRepository> userClaimRepository = new();
userClaimRepository.Setup(u => u.AddStandardUserClaimsAsync()).Verifiable();
Expand All @@ -35,7 +35,7 @@ public async Task TestSeedStandardResources1Async()
public async Task TestSeedStandardResources2Async()
{
var services = new ServiceCollection();
services.AddMasaDbContext<CustomDbContext>(dbContext => { dbContext.UseInMemoryDatabase(Guid.NewGuid().ToString()); dbContext.EnablePluralizingTableName = true; });
services.AddMasaDbContext<CustomDbContext>(dbContext => { dbContext.UseInMemoryDatabase(Guid.NewGuid().ToString()); });

Mock<IUserClaimRepository> userClaimRepository = new();
userClaimRepository.Setup(u => u.AddStandardUserClaimsAsync()).Verifiable();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ static ServiceCollection InitializingData()
services.AddMasaDbContext<CustomDbContext>(dbContext => dbContext.UseInMemoryDatabase(Guid.NewGuid().ToString()));
services.AddScoped(provider => new OidcDbContext(provider.GetRequiredService<CustomDbContext>()));


var options = new RedisConfigurationOptions()
{
Servers = new List<RedisServerOptions>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
global using Masa.BuildingBlocks.Data;
global using Microsoft.EntityFrameworkCore.Infrastructure;
global using Microsoft.EntityFrameworkCore.Internal;
global using Microsoft.Extensions.DependencyInjection;
global using Microsoft.Extensions.DependencyInjection;
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

// ReSharper disable once CheckNamespace

namespace Microsoft.EntityFrameworkCore;

public static class MasaDbContextOptionsBuilderExtensions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@

global using Masa.BuildingBlocks.Data;
global using Microsoft.EntityFrameworkCore.Infrastructure;
global using Microsoft.Extensions.DependencyInjection;
global using Microsoft.Extensions.DependencyInjection;
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,14 @@ public async Task TestAddMultiDbContextAsync()
var services = new ServiceCollection();
string connectionString = $"data source=test-{Guid.NewGuid()}";
string connectionStringByQuery = connectionString;
services.AddMasaDbContext<CustomQueryDbContext>(options => { options.UseSqlite(connectionStringByQuery).UseFilter(); options.EnablePluralizingTableName = false; });
services.AddMasaDbContext<CustomDbContext>(options => { options.UseSqlite(connectionStringByQuery).UseFilter(); options.EnablePluralizingTableName = false; });
services.AddMasaDbContext<CustomQueryDbContext>(options =>
{
options.UseSqlite(connectionStringByQuery).UseFilter();
});
services.AddMasaDbContext<CustomDbContext>(options =>
{
options.UseSqlite(connectionStringByQuery).UseFilter();
});
var serviceProvider = services.BuildServiceProvider();
var dbContext = serviceProvider.GetRequiredService<CustomDbContext>();
var queryDbContext = serviceProvider.GetRequiredService<CustomQueryDbContext>();
Expand Down Expand Up @@ -173,7 +179,9 @@ public async Task TestDisabledSoftDelete()
{
Services.AddMasaDbContext<CustomDbContext>(options
=>
{ options.UseSqlite($"data source=disabled-soft-delete-db-{Guid.NewGuid()}").UseFilter(); options.EnablePluralizingTableName = false; });
{
options.UseSqlite($"data source=disabled-soft-delete-db-{Guid.NewGuid()}").UseFilter();
});
var serviceProvider = Services.BuildServiceProvider();
var dbContext = serviceProvider.GetRequiredService<CustomDbContext>();
await dbContext.Database.EnsureCreatedAsync();
Expand Down Expand Up @@ -206,30 +214,14 @@ public async Task TestDisabledSoftDelete()
}

[TestMethod]
public void TestAddMultiMasaDbContextReturnSaveChangeFilterEqual1()
public void TestAddMasaDbContextReturnSaveChangeFilterEqual3()
{
var services = new ServiceCollection();
services.AddMasaDbContext<CustomDbContext>(opt => opt.UseSqlite(Guid.NewGuid().ToString()))
.AddMasaDbContext<CustomDbContext>(opt => opt.UseSqlite(Guid.NewGuid().ToString()));

var serviceProvider = services.BuildServiceProvider();
Assert.IsTrue(serviceProvider.GetServices<ISaveChangesFilter<CustomDbContext>>().Count() == 3);
}

[TestMethod]
public void TestAddMasaDbContextReturnSaveChangeFilterEqual2()
{
var services = new ServiceCollection();
services.AddMasaDbContext<CustomDbContext>(opt =>
{
opt.UseSqlite(Guid.NewGuid().ToString()).UseFilter();
opt.EnablePluralizingTableName = false;
});

var serviceProvider = services.BuildServiceProvider();

var filters = serviceProvider.GetServices<ISaveChangesFilter<CustomDbContext>>();
Assert.IsTrue(filters.Count() == 3);
Assert.AreEqual(3, serviceProvider.GetServices<ISaveChangesFilter<CustomDbContext>>().Count());
}

[TestMethod]
Expand Down Expand Up @@ -291,7 +283,10 @@ public async Task TestModifyConnectionString()
.AddJsonFile("appsettings.json", true, true)
.Build();
services.AddSingleton<IConfiguration>(configuration);
services.AddMasaDbContext<CustomQueryDbContext>(optionsBuilder => { optionsBuilder.UseSqlite(); optionsBuilder.EnablePluralizingTableName = false; });
services.AddMasaDbContext<CustomQueryDbContext>(optionsBuilder =>
{
optionsBuilder.UseSqlite();
});

var serviceProvider = services.BuildServiceProvider();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ namespace Masa.Contrib.Data.EFCore.Tests;
[TestClass]
public class DefaultConnectionStringProviderTest
{
[TestMethod]
public async Task TestGetConnectionStringAsyncReturnTest1()
private readonly DefaultConnectionStringProvider _defaultConnectionStringProvider;

public DefaultConnectionStringProviderTest()
{
IServiceCollection services = new ServiceCollection();
services.Configure<ConnectionStrings>(options =>
Expand All @@ -16,23 +17,20 @@ public async Task TestGetConnectionStringAsyncReturnTest1()
});
var serviceProvider = services.BuildServiceProvider();
var options = serviceProvider.GetRequiredService<IOptionsSnapshot<ConnectionStrings>>();
var defaultConnectionStringProvider = new DefaultConnectionStringProvider(options);
var connectionString = await defaultConnectionStringProvider.GetConnectionStringAsync();
_defaultConnectionStringProvider = new DefaultConnectionStringProvider(options);
}

[TestMethod]
public async Task TestGetConnectionStringAsyncReturnTest1()
{
var connectionString = await _defaultConnectionStringProvider.GetConnectionStringAsync();
Assert.AreEqual("Test1", connectionString);
}

[TestMethod]
public async Task TestGetConnectionStringAsyncAndNameIsEmptyReturnTest1()
{
IServiceCollection services = new ServiceCollection();
services.Configure<ConnectionStrings>(options =>
{
options.DefaultConnection = "Test1";
});
var serviceProvider = services.BuildServiceProvider();
var options = serviceProvider.GetRequiredService<IOptionsSnapshot<ConnectionStrings>>();
var defaultConnectionStringProvider = new DefaultConnectionStringProvider(options);
var connectionString = await defaultConnectionStringProvider.GetConnectionStringAsync(string.Empty);
var connectionString = await _defaultConnectionStringProvider.GetConnectionStringAsync(string.Empty);
Assert.AreEqual("Test1", connectionString);
}
}
Loading

0 comments on commit 98b20d4

Please sign in to comment.