Skip to content

Commit 651b5cd

Browse files
authoredJan 13, 2023
pref(ConnectionStringName): Optimize multiple acquisitions of ConnectionStringName based on DbContextType (#414)
* pref(ConnectionStringName): Optimize multiple acquisitions of ConnectionStringName based on DbContextType * pref: MasaDbContextBuilder is not a required parameter
1 parent f0edbd9 commit 651b5cd

File tree

12 files changed

+178
-8
lines changed

12 files changed

+178
-8
lines changed
 

‎Masa.Framework.sln

+14
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Masa.Contrib.StackSdks.Conf
651651
EndProject
652652
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Masa.Contrib.StackSdks.Config.Tests", "src\Contrib\StackSdks\Tests\Masa.Contrib.StackSdks.Config.Tests\Masa.Contrib.StackSdks.Config.Tests.csproj", "{5130EF56-9225-453C-A9AF-EB80A8A19E18}"
653653
EndProject
654+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{F6BD4124-2DCA-453E-B73C-D9DB6323CCDC}"
655+
EndProject
656+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Masa.BuildingBlocks.Data.Tests", "src\BuildingBlocks\Data\Tests\Masa.BuildingBlocks.Data.Tests\Masa.BuildingBlocks.Data.Tests.csproj", "{4F1E9488-FAC5-4966-90E6-F1DFE35F8370}"
657+
EndProject
654658
Global
655659
GlobalSection(SolutionConfigurationPlatforms) = preSolution
656660
Debug|Any CPU = Debug|Any CPU
@@ -2331,6 +2335,14 @@ Global
23312335
{5130EF56-9225-453C-A9AF-EB80A8A19E18}.Release|Any CPU.Build.0 = Release|Any CPU
23322336
{5130EF56-9225-453C-A9AF-EB80A8A19E18}.Release|x64.ActiveCfg = Release|Any CPU
23332337
{5130EF56-9225-453C-A9AF-EB80A8A19E18}.Release|x64.Build.0 = Release|Any CPU
2338+
{4F1E9488-FAC5-4966-90E6-F1DFE35F8370}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
2339+
{4F1E9488-FAC5-4966-90E6-F1DFE35F8370}.Debug|Any CPU.Build.0 = Debug|Any CPU
2340+
{4F1E9488-FAC5-4966-90E6-F1DFE35F8370}.Debug|x64.ActiveCfg = Debug|Any CPU
2341+
{4F1E9488-FAC5-4966-90E6-F1DFE35F8370}.Debug|x64.Build.0 = Debug|Any CPU
2342+
{4F1E9488-FAC5-4966-90E6-F1DFE35F8370}.Release|Any CPU.ActiveCfg = Release|Any CPU
2343+
{4F1E9488-FAC5-4966-90E6-F1DFE35F8370}.Release|Any CPU.Build.0 = Release|Any CPU
2344+
{4F1E9488-FAC5-4966-90E6-F1DFE35F8370}.Release|x64.ActiveCfg = Release|Any CPU
2345+
{4F1E9488-FAC5-4966-90E6-F1DFE35F8370}.Release|x64.Build.0 = Release|Any CPU
23342346
EndGlobalSection
23352347
GlobalSection(SolutionProperties) = preSolution
23362348
HideSolutionNode = FALSE
@@ -2652,6 +2664,8 @@ Global
26522664
{AF718133-0A37-4F1A-96DF-61C3D9836B7F} = {8A9DBB76-6618-4982-87D7-6CBD8375EB15}
26532665
{F3C2B14A-C350-4E30-9EB5-5BFCCC4EEB5B} = {383995FF-B661-4E15-A830-640FC5BA8A1F}
26542666
{5130EF56-9225-453C-A9AF-EB80A8A19E18} = {EC7A08E9-3355-486B-BA30-41A1F8CAC5F5}
2667+
{F6BD4124-2DCA-453E-B73C-D9DB6323CCDC} = {D2DF986D-5EA6-40ED-93F3-2CF3A035F26B}
2668+
{4F1E9488-FAC5-4966-90E6-F1DFE35F8370} = {F6BD4124-2DCA-453E-B73C-D9DB6323CCDC}
26552669
EndGlobalSection
26562670
GlobalSection(ExtensibilityGlobals) = postSolution
26572671
SolutionGuid = {40383055-CC50-4600-AD9A-53C14F620D03}

‎src/BuildingBlocks/Data/Masa.BuildingBlocks.Data/ConnectionStringNameAttribute.cs

+19-5
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,31 @@ public class ConnectionStringNameAttribute : Attribute
88
{
99
public string Name { get; set; }
1010

11-
public ConnectionStringNameAttribute(string name) => Name = name;
11+
public ConnectionStringNameAttribute(string name = "") => Name = name;
12+
13+
private static bool _isUseDefaultConnectionStringName;
14+
private static readonly List<DbContextNameRelationOptions> DbContextNameRelationOptions = new();
1215

1316
public static string GetConnStringName<T>() => GetConnStringName(typeof(T));
1417

1518
public static string GetConnStringName(Type type)
1619
{
17-
var nameAttribute = type.GetTypeInfo().GetCustomAttribute<ConnectionStringNameAttribute>();
20+
var options = DbContextNameRelationOptions.FirstOrDefault(c => c.DbContextType == type);
21+
if (options != null) return options.Name;
1822

19-
if (nameAttribute == null)
20-
return ConnectionStrings.DEFAULT_CONNECTION_STRING_NAME;
23+
var nameAttribute = type.GetTypeInfo().GetCustomAttribute<ConnectionStringNameAttribute>();
2124

22-
return !string.IsNullOrEmpty(nameAttribute.Name) ? nameAttribute.Name : type.FullName!;
25+
var name = nameAttribute?.Name;
26+
if (name.IsNullOrWhiteSpace())
27+
{
28+
if (_isUseDefaultConnectionStringName) name = type.FullName;
29+
else
30+
{
31+
_isUseDefaultConnectionStringName = true;
32+
name = ConnectionStrings.DEFAULT_CONNECTION_STRING_NAME;
33+
}
34+
}
35+
DbContextNameRelationOptions.Add(new DbContextNameRelationOptions(name!, type));
36+
return name!;
2337
}
2438
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright (c) MASA Stack All rights reserved.
2+
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
3+
4+
// ReSharper disable once CheckNamespace
5+
6+
namespace Masa.BuildingBlocks.Data;
7+
8+
internal class DbContextNameRelationOptions
9+
{
10+
public string Name { get; }
11+
12+
public Type DbContextType { get; }
13+
14+
public DbContextNameRelationOptions(string name, Type dbContextType)
15+
{
16+
Name = name;
17+
DbContextType = dbContextType;
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright (c) MASA Stack All rights reserved.
2+
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
3+
4+
namespace Masa.BuildingBlocks.Data.Tests;
5+
6+
[TestClass]
7+
public class ConnectionStringNameTest
8+
{
9+
[TestMethod]
10+
public void TestGetConnectionStringName()
11+
{
12+
string name = ConnectionStringNameAttribute.GetConnStringName<CustomDbContext>();
13+
Assert.AreEqual(ConnectionStrings.DEFAULT_CONNECTION_STRING_NAME, name);
14+
15+
name = ConnectionStringNameAttribute.GetConnStringName<CustomQueryDbContext>();
16+
Assert.AreEqual(typeof(CustomQueryDbContext).FullName, name);
17+
18+
name = ConnectionStringNameAttribute.GetConnStringName<CustomQuery2DbContext>();
19+
Assert.AreEqual(typeof(CustomQuery2DbContext).FullName, name);
20+
21+
name = ConnectionStringNameAttribute.GetConnStringName<CustomQuery3DbContext>();
22+
Assert.AreEqual("query3", name);
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Copyright (c) MASA Stack All rights reserved.
2+
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
3+
4+
namespace Masa.BuildingBlocks.Data.Tests.Infrastructure;
5+
6+
public class CustomDbContext: CustomDbContextBase
7+
{
8+
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) MASA Stack All rights reserved.
2+
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
3+
4+
namespace Masa.BuildingBlocks.Data.Tests.Infrastructure;
5+
6+
public class CustomDbContextBase : IMasaDbContext
7+
{
8+
public ValueTask DisposeAsync()
9+
{
10+
DisposeAsync(true);
11+
GC.SuppressFinalize(this);
12+
return ValueTask.CompletedTask;
13+
}
14+
15+
protected virtual void DisposeAsync(bool disposing)
16+
{
17+
}
18+
19+
public void Dispose()
20+
{
21+
Dispose(true);
22+
GC.SuppressFinalize(this);
23+
}
24+
25+
protected virtual void Dispose(bool disposing)
26+
{
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Copyright (c) MASA Stack All rights reserved.
2+
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
3+
4+
namespace Masa.BuildingBlocks.Data.Tests.Infrastructure;
5+
6+
[ConnectionStringName]
7+
public class CustomQuery2DbContext : CustomDbContextBase
8+
{
9+
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Copyright (c) MASA Stack All rights reserved.
2+
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
3+
4+
namespace Masa.BuildingBlocks.Data.Tests.Infrastructure;
5+
6+
[ConnectionStringName("query3")]
7+
public class CustomQuery3DbContext : CustomDbContextBase
8+
{
9+
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Copyright (c) MASA Stack All rights reserved.
2+
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
3+
4+
namespace Masa.BuildingBlocks.Data.Tests.Infrastructure;
5+
6+
public class CustomQueryDbContext : CustomDbContextBase
7+
{
8+
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
<IsPackable>false</IsPackable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(MicrosoftTeskSdkPackageVersion)" />
12+
<PackageReference Include="MSTest.TestAdapter" Version="$(MSTestPackageVersion)" />
13+
<PackageReference Include="MSTest.TestFramework" Version="$(MSTestPackageVersion)" />
14+
<PackageReference Include="coverlet.msbuild" Version="$(CoverletPackageVersion)">
15+
<PrivateAssets>all</PrivateAssets>
16+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
17+
</PackageReference>
18+
<PackageReference Include="coverlet.collector" Version="$(CoverletPackageVersion)">
19+
<PrivateAssets>all</PrivateAssets>
20+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
21+
</PackageReference>
22+
</ItemGroup>
23+
24+
<ItemGroup>
25+
<ProjectReference Include="..\..\Masa.BuildingBlocks.Data\Masa.BuildingBlocks.Data.csproj" />
26+
</ItemGroup>
27+
28+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Copyright (c) MASA Stack All rights reserved.
2+
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
3+
4+
global using Masa.BuildingBlocks.Data.Tests.Infrastructure;
5+
global using Microsoft.VisualStudio.TestTools.UnitTesting;

‎src/Contrib/Isolation/UoW/Masa.Contrib.Isolation.UoW.EFCore/Extensions/DomainEventOptionsExtensions.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public static class DomainEventOptionsExtensions
1010
public static IDomainEventOptions UseIsolationUoW<TDbContext>(
1111
this IDomainEventOptions options,
1212
Action<IsolationBuilder> isolationBuilder,
13-
Action<MasaDbContextBuilder>? optionsBuilder,
13+
Action<MasaDbContextBuilder>? optionsBuilder = null,
1414
bool disableRollbackOnFailure = false,
1515
bool useTransaction = true)
1616
where TDbContext : MasaDbContext, IMasaDbContext
@@ -19,7 +19,7 @@ public static IDomainEventOptions UseIsolationUoW<TDbContext>(
1919
public static IDomainEventOptions UseIsolationUoW<TDbContext, TTenantId>(
2020
this IDomainEventOptions options,
2121
Action<IsolationBuilder> isolationBuilder,
22-
Action<MasaDbContextBuilder>? optionsBuilder,
22+
Action<MasaDbContextBuilder>? optionsBuilder = null,
2323
bool disableRollbackOnFailure = false,
2424
bool useTransaction = true)
2525
where TDbContext : MasaDbContext, IMasaDbContext
@@ -30,7 +30,7 @@ public static IDomainEventOptions UseIsolationUoW<TDbContext, TTenantId>(
3030
public static IDomainEventOptions UseIsolationUoW<TDbContext, TTenantId, TUserId>(
3131
this IDomainEventOptions options,
3232
Action<IsolationBuilder> isolationBuilder,
33-
Action<MasaDbContextBuilder>? optionsBuilder,
33+
Action<MasaDbContextBuilder>? optionsBuilder = null,
3434
bool disableRollbackOnFailure = false,
3535
bool useTransaction = true)
3636
where TDbContext : MasaDbContext, IMasaDbContext

0 commit comments

Comments
 (0)
Please sign in to comment.