Skip to content

Commit 690a78b

Browse files
committed
Expansão do suporte a contextos de trabalho em múltiplos bancos
* Atualização da pipeline CI/CD - Adicionadas etapas de build e pack para PostgreSql, Sqlite e SqlServer. * Versão e estrutura do projeto - Atualizada a versão de persistência para 0.8.1. - Novos projetos para Sqlite, PostgreSql e SqlServer adicionados à solução. * Acesso e visibilidade das classes - `DefaultDbContext` alterado de interno para público. - Implementação interna removida, promovendo uma API mais acessível. * Extensibilidade e configuração - Métodos de extensão para configuração de contextos de trabalho adicionados para novos bancos.
1 parent 54d4699 commit 690a78b

File tree

13 files changed

+426
-42
lines changed

13 files changed

+426
-42
lines changed

.github/workflows/persistence-nuget.yml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ jobs:
3838

3939
- name: Build and pack Work Context EntityFramework
4040
run: dotnet build ./RoyalCode.EnterprisePatterns/RoyalCode.WorkContext.EntityFramework/RoyalCode.WorkContext.EntityFramework.csproj -c Release
41-
41+
42+
- name: Build and pack Work Context PostgreSql
43+
run: dotnet build ./RoyalCode.EnterprisePatterns/RoyalCode.WorkContext.PostgreSql/RoyalCode.WorkContext.PostgreSql.csproj -c Release
44+
45+
- name: Build and pack Work Context Sqlite
46+
run: dotnet build ./RoyalCode.EnterprisePatterns/RoyalCode.WorkContext.Sqlite/RoyalCode.WorkContext.Sqlite.csproj -c Release
47+
48+
- name: Build and pack Work Context SqlServer
49+
run: dotnet build ./RoyalCode.EnterprisePatterns/RoyalCode.WorkContext.SqlServer/RoyalCode.WorkContext.SqlServer.csproj -c Release
50+
4251
- name: Publish
4352
run: dotnet nuget push ./**/*.nupkg --api-key ${{ secrets.NUGET_API_KEY }} --source https://api.nuget.org/v3/index.json --skip-duplicate

RoyalCode.EnterprisePatterns/Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<DomainVer>0.8.0</DomainVer>
1010
<DomainPreview></DomainPreview>
1111

12-
<PersistVer>0.8.0</PersistVer>
12+
<PersistVer>0.8.1</PersistVer>
1313
<PersistPreview></PersistPreview>
1414

1515
<CommandVer>0.1.0</CommandVer>

RoyalCode.EnterprisePatterns/RoyalCode.EnterprisePatterns.sln

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RoyalCode.Events.Outbox.Ent
7474
EndProject
7575
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RoyalCode.WorkContext.AspNetCore", "RoyalCode.WorkContext.AspNetCore\RoyalCode.WorkContext.AspNetCore.csproj", "{E7AD2D84-8249-47DC-A400-93572C952755}"
7676
EndProject
77+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RoyalCode.WorkContext.Sqlite", "RoyalCode.WorkContext.Sqlite\RoyalCode.WorkContext.Sqlite.csproj", "{63BD398D-0D71-4B28-87A8-E6D335730F9C}"
78+
EndProject
79+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RoyalCode.WorkContext.PostgreSql", "RoyalCode.WorkContext.PostgreSql\RoyalCode.WorkContext.PostgreSql.csproj", "{430B93E7-7F7C-4DAB-95EF-BBEDD98338B7}"
80+
EndProject
81+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RoyalCode.WorkContext.SqlServer", "RoyalCode.WorkContext.SqlServer\RoyalCode.WorkContext.SqlServer.csproj", "{80B13539-A3CB-4C66-B1E2-189B1FDC4D04}"
82+
EndProject
7783
Global
7884
GlobalSection(SolutionConfigurationPlatforms) = preSolution
7985
Debug|Any CPU = Debug|Any CPU
@@ -172,6 +178,18 @@ Global
172178
{E7AD2D84-8249-47DC-A400-93572C952755}.Debug|Any CPU.Build.0 = Debug|Any CPU
173179
{E7AD2D84-8249-47DC-A400-93572C952755}.Release|Any CPU.ActiveCfg = Release|Any CPU
174180
{E7AD2D84-8249-47DC-A400-93572C952755}.Release|Any CPU.Build.0 = Release|Any CPU
181+
{63BD398D-0D71-4B28-87A8-E6D335730F9C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
182+
{63BD398D-0D71-4B28-87A8-E6D335730F9C}.Debug|Any CPU.Build.0 = Debug|Any CPU
183+
{63BD398D-0D71-4B28-87A8-E6D335730F9C}.Release|Any CPU.ActiveCfg = Release|Any CPU
184+
{63BD398D-0D71-4B28-87A8-E6D335730F9C}.Release|Any CPU.Build.0 = Release|Any CPU
185+
{430B93E7-7F7C-4DAB-95EF-BBEDD98338B7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
186+
{430B93E7-7F7C-4DAB-95EF-BBEDD98338B7}.Debug|Any CPU.Build.0 = Debug|Any CPU
187+
{430B93E7-7F7C-4DAB-95EF-BBEDD98338B7}.Release|Any CPU.ActiveCfg = Release|Any CPU
188+
{430B93E7-7F7C-4DAB-95EF-BBEDD98338B7}.Release|Any CPU.Build.0 = Release|Any CPU
189+
{80B13539-A3CB-4C66-B1E2-189B1FDC4D04}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
190+
{80B13539-A3CB-4C66-B1E2-189B1FDC4D04}.Debug|Any CPU.Build.0 = Debug|Any CPU
191+
{80B13539-A3CB-4C66-B1E2-189B1FDC4D04}.Release|Any CPU.ActiveCfg = Release|Any CPU
192+
{80B13539-A3CB-4C66-B1E2-189B1FDC4D04}.Release|Any CPU.Build.0 = Release|Any CPU
175193
EndGlobalSection
176194
GlobalSection(SolutionProperties) = preSolution
177195
HideSolutionNode = FALSE
@@ -206,6 +224,9 @@ Global
206224
{79F643A3-9BFA-4200-A9FB-83A713A6C3D2} = {80CCF203-3C6E-4B07-B011-04F7800EA52E}
207225
{BDAFD007-FEB0-44B8-8A44-5C6F6E540B45} = {80CCF203-3C6E-4B07-B011-04F7800EA52E}
208226
{E7AD2D84-8249-47DC-A400-93572C952755} = {DD6336C0-C66B-44CD-9BEA-D9ED66EEC7BF}
227+
{63BD398D-0D71-4B28-87A8-E6D335730F9C} = {DD6336C0-C66B-44CD-9BEA-D9ED66EEC7BF}
228+
{430B93E7-7F7C-4DAB-95EF-BBEDD98338B7} = {DD6336C0-C66B-44CD-9BEA-D9ED66EEC7BF}
229+
{80B13539-A3CB-4C66-B1E2-189B1FDC4D04} = {DD6336C0-C66B-44CD-9BEA-D9ED66EEC7BF}
209230
EndGlobalSection
210231
GlobalSection(ExtensibilityGlobals) = postSolution
211232
SolutionGuid = {9FAC6C2E-62DE-4665-BB16-4E7D445FAB26}

RoyalCode.EnterprisePatterns/RoyalCode.UnitOfWork.EntityFramework/DefaultDbContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace RoyalCode.UnitOfWork.EntityFramework;
55
/// <summary>
66
/// Represents the default Entity Framework <see cref="DbContext"/> used for database operations.
77
/// </summary>
8-
internal sealed class DefaultDbContext : DbContext
8+
public sealed class DefaultDbContext : DbContext
99
{
1010
/// <summary>
1111
/// Initializes a new instance of the <see cref="DefaultDbContext"/> class with the specified options.

RoyalCode.EnterprisePatterns/RoyalCode.UnitOfWork.EntityFramework/Extensions/UnitOfWorkServiceCollectionExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public static class UnitOfWorkServiceCollectionExtensions
1818
/// <param name="services">The service collection.</param>
1919
/// <param name="lifetime">The services lifetime, by default is scoped.</param>
2020
/// <returns>A unit of work builder to configure the <see cref="DbContext"/> and services like repositories and searches.</returns>
21-
public static IUnitOfWorkBuilder<DbContext> AddUnitOfWork(this IServiceCollection services,
21+
public static IUnitOfWorkBuilder<DefaultDbContext> AddUnitOfWorkDefault(this IServiceCollection services,
2222
ServiceLifetime lifetime = ServiceLifetime.Scoped)
2323
{
2424
return services.AddUnitOfWork<DefaultDbContext>(lifetime)

RoyalCode.EnterprisePatterns/RoyalCode.WorkContext.EntityFramework/Extensions/WorkContextServiceCollectionExtensions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using RoyalCode.SmartSearch.EntityFramework.Services;
77
using RoyalCode.SmartSearch.Linq;
88
using RoyalCode.UnitOfWork;
9+
using RoyalCode.UnitOfWork.EntityFramework;
910
using RoyalCode.WorkContext;
1011
using RoyalCode.WorkContext.Commands;
1112
using RoyalCode.WorkContext.EntityFramework;
@@ -30,7 +31,7 @@ public static class WorkContextServiceCollectionExtensions
3031
/// <returns>
3132
/// A unit of work builder to configure the <see cref="DbContext"/> and services like repositories and searches.
3233
/// </returns>
33-
public static IWorkContextBuilder<DbContext> AddWorkContext(
34+
public static IWorkContextBuilder<DefaultDbContext> AddWorkContextDefault(
3435
this IServiceCollection services,
3536
ServiceLifetime lifetime = ServiceLifetime.Scoped)
3637
{

RoyalCode.EnterprisePatterns/RoyalCode.WorkContext.EntityFramework/Internal/DefaultDbContext.cs

Lines changed: 0 additions & 37 deletions
This file was deleted.
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+

2+
using Microsoft.EntityFrameworkCore;
3+
using Microsoft.Extensions.Configuration;
4+
using Npgsql.EntityFrameworkCore.PostgreSQL.Infrastructure;
5+
using RoyalCode.UnitOfWork.EntityFramework;
6+
using RoyalCode.WorkContext.EntityFramework.Configurations;
7+
8+
namespace Microsoft.Extensions.DependencyInjection;
9+
10+
/// <summary>
11+
/// Extensions methods for <see cref="IServiceCollection"/> to add PostgreSQL-based work context.
12+
/// </summary>
13+
public static class PgWorkContextExtensions
14+
{
15+
/// <summary>
16+
/// Adds a PostgreSQL-based work context for the <see cref="DefaultDbContext"/> to the service collection.
17+
/// </summary>
18+
/// <param name="services">The <see cref="IServiceCollection"/> to which the work context will be added.</param>
19+
/// <param name="connectionStringName">
20+
/// The name of the connection string, used to get the connection string from <see cref="IConfiguration"/>.
21+
/// </param>
22+
/// <param name="lifetime">The services lifetime, by default is scoped.</param>
23+
/// <returns>
24+
/// A unit of work builder to configure the <see cref="DbContext"/> and services like repositories and searches.
25+
/// </returns>
26+
public static IWorkContextBuilder<DefaultDbContext> AddPostgreWorkContextDefault(
27+
this IServiceCollection services,
28+
string connectionStringName = "Default",
29+
ServiceLifetime lifetime = ServiceLifetime.Scoped)
30+
{
31+
return services.AddPostgreWorkContext<DefaultDbContext>(connectionStringName, lifetime);
32+
}
33+
34+
/// <summary>
35+
/// Adds a PostgreSQL-based work context related to a <see cref="DbContext"/>.
36+
/// </summary>
37+
/// <typeparam name="TDbContext">The type of the DbContext used in the work context.</typeparam>
38+
/// <param name="services">The service collection.</param>
39+
/// <param name="connectionStringName">
40+
/// The name of the connection string, used to get the connection string from <see cref="IConfiguration"/>.
41+
/// </param>
42+
/// <param name="lifetime">The services lifetime, by default is scoped.</param>
43+
/// <returns>
44+
/// A unit of work builder to configure the <see cref="DbContext"/> and services like repositories and searches.
45+
/// </returns>
46+
public static IWorkContextBuilder<TDbContext> AddPostgreWorkContext<TDbContext>(
47+
this IServiceCollection services,
48+
string connectionStringName = "Default",
49+
ServiceLifetime lifetime = ServiceLifetime.Scoped)
50+
where TDbContext : DbContext
51+
{
52+
return services.AddWorkContext<TDbContext>(lifetime)
53+
.ConfigureDbContextWithService()
54+
.ConfigureOptions((provider, options) =>
55+
{
56+
var configuration = provider.GetRequiredService<IConfiguration>();
57+
options.UseNpgsql(configuration.GetConnectionString(connectionStringName));
58+
});
59+
}
60+
61+
/// <summary>
62+
/// Allows you to configure specific Npgsql options for the Entity Framework context.
63+
/// </summary>
64+
/// <typeparam name="TDbContext">The type of the DbContext used in the work context.</typeparam>
65+
/// <param name="builder">The work context builder to which the Npgsql options will be added.</param>
66+
/// <param name="configure">The action to configure the Npgsql options.</param>
67+
/// <returns>The same builder for chained calls.</returns>
68+
public static IWorkContextBuilder<TDbContext> ConfigureNpgsqlOptions<TDbContext>(
69+
this IWorkContextBuilder<TDbContext> builder,
70+
Action<NpgsqlDbContextOptionsBuilder> configure)
71+
where TDbContext : DbContext
72+
{
73+
return builder.ConfigureOptions((sp, ob) =>
74+
{
75+
configure(new NpgsqlDbContextOptionsBuilder(ob));
76+
});
77+
}
78+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<Import Project="..\base.targets" />
4+
5+
<PropertyGroup>
6+
<TargetFrameworks>$(LibTargets)</TargetFrameworks>
7+
<Version>$(PersistVer)$(PersistPreview)</Version>
8+
<AssemblyVersion>$(PersistVer)</AssemblyVersion>
9+
<FileVersion>$(PersistVer)</FileVersion>
10+
<Description>
11+
Persistence components implementation with EntityFrameworkCore and PostgreSql,
12+
including the handling of unit of works, repositories and searches.
13+
The Work Context is an pattern that include the Unit Of Work functionality
14+
and add access to others patterns like Repository, Search, Event, Message Bus, etc.
15+
</Description>
16+
<PackageTags>
17+
RoyalCode Enterprise-Patterns UoW Unit-Of-Work WorkContext Work-Context
18+
</PackageTags>
19+
</PropertyGroup>
20+
21+
<ItemGroup>
22+
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="$(DotNetCoreVersion)" />
23+
</ItemGroup>
24+
25+
<ItemGroup>
26+
<ProjectReference Include="..\RoyalCode.WorkContext.EntityFramework\RoyalCode.WorkContext.EntityFramework.csproj" />
27+
</ItemGroup>
28+
29+
</Project>
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
using Microsoft.EntityFrameworkCore;
2+
using Microsoft.EntityFrameworkCore.Infrastructure;
3+
using Microsoft.Extensions.Configuration;
4+
using RoyalCode.UnitOfWork.EntityFramework;
5+
using RoyalCode.WorkContext.EntityFramework.Configurations;
6+
7+
namespace Microsoft.Extensions.DependencyInjection;
8+
9+
/// <summary>
10+
/// Extensions methods for <see cref="IServiceCollection"/> to add SQL Server-based work context.
11+
/// </summary>
12+
public static class SqlServerWorkContextExtensions
13+
{
14+
/// <summary>
15+
/// Adds a SQL Server-based work context for the <see cref="DefaultDbContext"/> to the service collection.
16+
/// </summary>
17+
/// <param name="services">The <see cref="IServiceCollection"/> to which the work context will be added.</param>
18+
/// <param name="connectionStringName">
19+
/// The name of the connection string, used to get the connection string from <see cref="IConfiguration"/>.
20+
/// </param>
21+
/// <param name="lifetime">The services lifetime, by default is scoped.</param>
22+
/// <returns>
23+
/// A unit of work builder to configure the <see cref="DbContext"/> and services like repositories and searches.
24+
/// </returns>
25+
public static IWorkContextBuilder<DefaultDbContext> AddSqlServerWorkContextDefault(
26+
this IServiceCollection services,
27+
string connectionStringName = "Default",
28+
ServiceLifetime lifetime = ServiceLifetime.Scoped)
29+
{
30+
return services.AddSqlServerWorkContext<DefaultDbContext>(connectionStringName, lifetime);
31+
}
32+
33+
/// <summary>
34+
/// Adds a SQL Server-based work context related to a <see cref="DbContext"/>.
35+
/// </summary>
36+
/// <typeparam name="TDbContext">The type of the DbContext used in the work context.</typeparam>
37+
/// <param name="services">The service collection.</param>
38+
/// <param name="connectionStringName">
39+
/// The name of the connection string, used to get the connection string from <see cref="IConfiguration"/>.
40+
/// </param>
41+
/// <param name="lifetime">The services lifetime, by default is scoped.</param>
42+
/// <returns>
43+
/// A unit of work builder to configure the <see cref="DbContext"/> and services like repositories and searches.
44+
/// </returns>
45+
public static IWorkContextBuilder<TDbContext> AddSqlServerWorkContext<TDbContext>(
46+
this IServiceCollection services,
47+
string connectionStringName = "Default",
48+
ServiceLifetime lifetime = ServiceLifetime.Scoped)
49+
where TDbContext : DbContext
50+
{
51+
return services.AddWorkContext<TDbContext>(lifetime)
52+
.ConfigureDbContextWithService()
53+
.ConfigureOptions((provider, options) =>
54+
{
55+
var configuration = provider.GetRequiredService<IConfiguration>();
56+
options.UseSqlServer(configuration.GetConnectionString(connectionStringName));
57+
});
58+
}
59+
60+
/// <summary>
61+
/// Allows you to configure specific SqlServer options for the Entity Framework context.
62+
/// </summary>
63+
/// <param name="builder">The work context builder to which the SqlServer options will be added.</param>
64+
/// <param name="configure">The action to configure the SqlServer options.</param>
65+
/// <returns>The same builder for chained calls.</returns>
66+
public static IWorkContextBuilder<TDbContext> ConfigureSqlServerOptions<TDbContext>(
67+
this IWorkContextBuilder<TDbContext> builder,
68+
Action<SqlServerDbContextOptionsBuilder> configure)
69+
where TDbContext : DbContext
70+
{
71+
return builder.ConfigureOptions((sp, ob) =>
72+
{
73+
configure(new SqlServerDbContextOptionsBuilder(ob));
74+
});
75+
}
76+
77+
/// <summary>
78+
/// Configures the context to use relational database semantics when comparing null
79+
/// values. By default, Entity Framework will use C# semantics for null values, and
80+
/// generate SQL to compensate for differences in how the database handles nulls.
81+
/// </summary>
82+
/// <param name="builder">The work context builder to which the SqlServer options will be added.</param>
83+
/// <returns>The same builder for chained calls.</returns>
84+
public static IWorkContextBuilder<TDbContext> UseRelationalNulls<TDbContext>(
85+
this IWorkContextBuilder<TDbContext> builder)
86+
where TDbContext : DbContext
87+
{
88+
return builder.ConfigureSqlServerOptions(b => b.UseRelationalNulls());
89+
}
90+
}

0 commit comments

Comments
 (0)