Skip to content

Commit 4d928b5

Browse files
committed
Atualiza versões e refatora estrutura de namespaces
Mudanças significativas na configuração e estrutura do projeto. * Atualização de versões - Alteradas versões em `Directory.Build.props` para refletir novas versões. - Introduzidas novas propriedades `ProblemsVer` e `GenVer`. * Refatoração de namespaces - Substituição de `RoyalCode.WorkContext.Abstractions` por `RoyalCode.WorkContext` em vários arquivos. - Atualizações em `ICreationHandler.cs`, `IValidationHandler.cs`, `CreateCommandHandler.cs` e `SearchesTestes.cs`. * Melhoria na configuração do DbContext - Alterado tipo de `configureBuilder` em `Utils.cs` para `IWorkContextBuilder`. - Removido `IConfigureDbContextService.cs` em favor de novas interfaces de configuração. * Adição de novas interfaces e classes - Introduzidas interfaces `IConfigureConventions`, `IConfigureModel` e `IConfigureOptions`. - Criada a classe `DefaultDbContext` para implementação padrão do DbContext. * Extensibilidade e integração de serviços - Novos métodos em `UnitOfWorkServiceCollectionExtensions.cs` para adicionar contextos de trabalho. - Atualizações em `WorkContextServiceCollectionExtensions.cs` para facilitar a adição de serviços. * Atualizações em arquivos de projeto - Modificações em `RoyalCode.Examples.Api.csproj` e `RoyalCode.Examples.Blogs.csproj` para usar propriedades de versão. - Atualizações em `Program.cs` para novas inscrições de serviços.
1 parent 4bdcf9f commit 4d928b5

27 files changed

Lines changed: 442 additions & 58 deletions

File tree

RoyalCode.EnterprisePatterns/Directory.Build.props

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@
66
</PropertyGroup>
77
<PropertyGroup>
88

9-
<DomainVer>1.0.0</DomainVer>
10-
<DomainPreview>-preview-8.0</DomainPreview>
9+
<DomainVer>0.8.0</DomainVer>
10+
<DomainPreview></DomainPreview>
1111

12-
<PersistVer>1.0.0</PersistVer>
13-
<PersistPreview>-preview-9.0</PersistPreview>
12+
<PersistVer>0.8.0</PersistVer>
13+
<PersistPreview></PersistPreview>
1414

1515
<CommandVer>0.1.0</CommandVer>
1616
<CommandPreview>-preview-1</CommandPreview>
1717

18-
<MsgVer>1.0.0</MsgVer>
18+
<MsgVer>0.1.0</MsgVer>
1919
<MsgVerPreview>-preview-0.1</MsgVerPreview>
2020
</PropertyGroup>
2121
<PropertyGroup>

RoyalCode.EnterprisePatterns/RoyalCode.Commands.Abstractions/ICreationHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using RoyalCode.SmartProblems;
2-
using RoyalCode.WorkContext.Abstractions;
2+
using RoyalCode.WorkContext;
33

44
namespace RoyalCode.Commands.Abstractions;
55

RoyalCode.EnterprisePatterns/RoyalCode.Commands.Abstractions/IValidationHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using RoyalCode.SmartProblems;
2-
using RoyalCode.WorkContext.Abstractions;
2+
using RoyalCode.WorkContext;
33

44
namespace RoyalCode.Commands.Abstractions;
55

RoyalCode.EnterprisePatterns/RoyalCode.Commands.Handlers/CreateCommandHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using RoyalCode.Commands.Abstractions;
22
using RoyalCode.Entities;
33
using RoyalCode.SmartProblems;
4-
using RoyalCode.WorkContext.Abstractions;
4+
using RoyalCode.WorkContext;
55

66
namespace RoyalCode.Commands.Handlers;
77

RoyalCode.EnterprisePatterns/RoyalCode.OperationHint.Tests/Searches/SearchesTestes.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using FluentAssertions;
22
using Microsoft.Extensions.DependencyInjection;
3-
using RoyalCode.WorkContext.Abstractions;
3+
using RoyalCode.WorkContext;
44

55
namespace RoyalCode.OperationHint.Tests.Searches;
66

RoyalCode.EnterprisePatterns/RoyalCode.OperationHint.Tests/Utils.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Microsoft.EntityFrameworkCore;
33
using Microsoft.Extensions.DependencyInjection;
44
using Microsoft.Extensions.DependencyInjection.Extensions;
5+
using RoyalCode.WorkContext.EntityFramework.Configurations;
56
using System.Data.Common;
67

78
namespace RoyalCode.OperationHint.Tests;
@@ -10,7 +11,7 @@ internal static class Utils
1011
{
1112
public static TServices AddWorkContext<TServices>(
1213
TServices services,
13-
Action<IUnitOfWorkBuilder<LocalDbContext>>? configureBuilder = null)
14+
Action<IWorkContextBuilder<LocalDbContext>>? configureBuilder = null)
1415
where TServices : IServiceCollection
1516
{
1617
DbConnection conn = new SqliteConnection("Data Source=:memory:");
@@ -31,7 +32,7 @@ public static TServices AddWorkContext<TServices>(
3132

3233
public static TServices AddWorkContextWithIncludes<TServices>(
3334
TServices services,
34-
Action<IUnitOfWorkBuilder<LocalDbContext>>? configureBuilder = null)
35+
Action<IWorkContextBuilder<LocalDbContext>>? configureBuilder = null)
3536
where TServices : IServiceCollection
3637
{
3738
AddWorkContext(services, builder =>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using Microsoft.EntityFrameworkCore;
2+
3+
namespace RoyalCode.UnitOfWork.EntityFramework.Configurations;
4+
5+
/// <summary>
6+
/// Defines a method to configure conventions for a <see cref="DbContext"/> using a <see cref="ModelConfigurationBuilder"/>.
7+
/// </summary>
8+
/// <typeparam name="TDbContext">Tipo do DbContext.</typeparam>
9+
public interface IConfigureConventions<TDbContext>
10+
where TDbContext : DbContext
11+
{
12+
/// <summary>
13+
/// Configure as convensões do <see cref="DbContext"/> através do <see cref="ModelConfigurationBuilder"/>.
14+
/// </summary>
15+
/// <param name="builder">O <see cref="ModelConfigurationBuilder"/> a ser configurado.</param>
16+
void Configure(ModelConfigurationBuilder builder);
17+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using Microsoft.EntityFrameworkCore;
2+
3+
namespace RoyalCode.UnitOfWork.EntityFramework.Configurations;
4+
5+
/// <summary>
6+
/// Defines a contract for configuring the <see cref="ModelBuilder"/> of a <see cref="DbContext"/>.
7+
/// </summary>
8+
public interface IConfigureModel<TDbContext>
9+
where TDbContext : DbContext
10+
{
11+
/// <summary>
12+
/// Configura o <see cref="ModelBuilder"/> de um <see cref="DbContext"/>.
13+
/// </summary>
14+
/// <param name="modelBuilder"></param>
15+
public void Configure(ModelBuilder modelBuilder);
16+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using Microsoft.EntityFrameworkCore;
2+
3+
namespace RoyalCode.UnitOfWork.EntityFramework.Configurations;
4+
5+
/// <summary>
6+
/// Defines a method to configure options for a <see cref="DbContext"/> using a <see cref="DbContextOptionsBuilder"/>.
7+
/// </summary>
8+
/// <typeparam name="TDbContext">Tipo do DbContext.</typeparam>
9+
public interface IConfigureOptions<TDbContext>
10+
where TDbContext : DbContext
11+
{
12+
/// <summary>
13+
/// Configure as opções do <see cref="DbContext"/> através do <see cref="DbContextOptionsBuilder"/>.
14+
/// </summary>
15+
/// <param name="optionsBuilder">O <see cref="DbContextOptionsBuilder"/> a ser configurado.</param>
16+
void Configure(DbContextOptionsBuilder optionsBuilder);
17+
}

RoyalCode.EnterprisePatterns/RoyalCode.UnitOfWork.EntityFramework/Configurations/IUnitOfWorkBuilder.cs

Lines changed: 176 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using Microsoft.EntityFrameworkCore;
22
using Microsoft.Extensions.DependencyInjection;
3+
using Microsoft.Extensions.DependencyInjection.Extensions;
34
using RoyalCode.UnitOfWork.Configurations;
4-
using RoyalCode.UnitOfWork.EntityFramework.Services;
55

66
namespace RoyalCode.UnitOfWork.EntityFramework.Configurations;
77

@@ -38,35 +38,79 @@ public interface IUnitOfWorkBuilder<out TDbContext, out TBuilder> : IUnitOfWorkB
3838
/// Configure the <see cref="DbContext"/> for the unit of work.
3939
/// </para>
4040
/// <para>
41-
/// The configuration is done by the <see cref="IConfigureDbContextService{TDbContext}"/>
41+
/// The configuration is done by the <see cref="IConfigureOptions{TDbContext}"/>
4242
/// registered in the services.
4343
/// </para>
4444
/// <para>
45-
/// When the <see cref="IConfigureDbContextService{TDbContext}"/> is not registered, an
45+
/// When the <see cref="IConfigureOptions{TDbContext}"/> is not registered, an
4646
/// <see cref="InvalidOperationException"/> is thrown.
4747
/// </para>
4848
/// </summary>
49+
/// <remarks>
50+
/// This method call AddDbContext with the <see cref="ServiceLifetime"/> of the unit of work.
51+
/// </remarks>
4952
/// <returns>The same instance.</returns>
5053
/// <exception cref="InvalidOperationException">
51-
/// The <see cref="IConfigureDbContextService{TDbContext}"/> is not registered.
54+
/// The <see cref="IConfigureOptions{TDbContext}"/> is not registered.
5255
/// </exception>
53-
public TBuilder ConfigureWithService()
56+
public TBuilder ConfigureDbContextWithService()
5457
{
5558
Services.AddDbContext<TDbContext>((sp, builder) =>
5659
{
57-
var configurator = sp.GetService<IConfigureDbContextService<TDbContext>>();
60+
var configurators = sp.GetService<IEnumerable<IConfigureOptions<TDbContext>>>();
5861

59-
if (configurator is null)
62+
if (configurators is null || !configurators.Any())
6063
throw new InvalidOperationException(
61-
"The IConfigureDbContextService is not registered. " +
64+
"The IConfigureOptions is not registered. " +
6265
"When using the ConfigureWithService method, it is necessary to register the " +
63-
"IConfigureDbContextService<TDbContext>.");
66+
"IConfigureOptions<TDbContext>.");
67+
68+
foreach (var configurator in configurators)
69+
configurator.Configure(builder);
6470

65-
configurator.ConfigureDbContext(builder);
6671
}, Lifetime);
6772
return (TBuilder)this;
6873
}
6974

75+
/// <summary>
76+
/// <para>
77+
/// Configure the <see cref="DbContext"/> for the unit of work.
78+
/// </para>
79+
/// <para>
80+
/// The configuration is done by the <see cref="IConfigureOptions{TDbContext}"/>
81+
/// registered in the services.
82+
/// </para>
83+
/// <para>
84+
/// When the <see cref="IConfigureOptions{TDbContext}"/> is not registered, an
85+
/// <see cref="InvalidOperationException"/> is thrown.
86+
/// </para>
87+
/// </summary>
88+
/// <remarks>
89+
/// This method call AddDbContextPool.
90+
/// </remarks>
91+
/// <returns>The same instance.</returns>
92+
/// <exception cref="InvalidOperationException">
93+
/// The <see cref="IConfigureOptions{TDbContext}"/> is not registered.
94+
/// </exception>
95+
public TBuilder ConfigureDbContextPoolWithService()
96+
{
97+
Services.AddDbContextPool<TDbContext>((sp, builder) =>
98+
{
99+
var configurators = sp.GetService<IEnumerable<IConfigureOptions<TDbContext>>>();
100+
101+
if (configurators is null || !configurators.Any())
102+
throw new InvalidOperationException(
103+
"The IConfigureOptions is not registered. " +
104+
"When using the ConfigureWithService method, it is necessary to register the " +
105+
"IConfigureOptions<TDbContext>.");
106+
107+
foreach (var configurator in configurators)
108+
configurator.Configure(builder);
109+
110+
});
111+
return (TBuilder)this;
112+
}
113+
70114
/// <summary>
71115
/// Configure the <see cref="DbContext"/> for the unit of work as pooled.
72116
/// </summary>
@@ -113,4 +157,126 @@ public TBuilder ConfigureDbContext(Action<IServiceProvider, DbContextOptionsBuil
113157
Services.AddDbContext<TDbContext>(configurer, Lifetime);
114158
return (TBuilder)this;
115159
}
160+
161+
/// <summary>
162+
/// Adds an action to configure the <see cref="ModelBuilder"/> of a <see cref="DbContext"/>.
163+
/// </summary>
164+
/// <param name="configure">Action to configure.</param>
165+
/// <returns>The same instance.</returns>
166+
public TBuilder ConfigureModel(Action<ModelBuilder> configure)
167+
{
168+
InternalConfigureModel<TDbContext>.GetFromServices(Services).Configure(configure);
169+
return (TBuilder)this;
170+
}
171+
172+
/// <summary>
173+
/// Adds an action to configure the <see cref="DbContextOptionsBuilder"/> of a <see cref="DbContext"/>.
174+
/// </summary>
175+
/// <param name="configure">Action to configure.</param>
176+
/// <returns>The same instance.</returns>
177+
public TBuilder ConfigureOptions(Action<DbContextOptionsBuilder> configure)
178+
{
179+
ConfigureOptionsAction<TDbContext> configureOptions = (sp, builder) => configure(builder);
180+
Services.AddSingleton(configureOptions);
181+
Services.TryAddEnumerable(ServiceDescriptor.Transient<IConfigureOptions<TDbContext>, InternalConfigureOptions<TDbContext>>());
182+
return (TBuilder)this;
183+
}
184+
185+
/// <summary>
186+
/// Adds an action to configure the <see cref="DbContextOptionsBuilder"/> of a <see cref="DbContext"/>.
187+
/// </summary>
188+
/// <param name="configure">Action to configure.</param>
189+
/// <returns>The same instance.</returns>
190+
public TBuilder ConfigureOptions(Action<IServiceProvider, DbContextOptionsBuilder> configure)
191+
{
192+
ConfigureOptionsAction<TDbContext> configureOptions = (sp, builder) => configure(sp, builder);
193+
Services.AddSingleton(configureOptions);
194+
Services.TryAddEnumerable(ServiceDescriptor.Transient<IConfigureOptions<TDbContext>, InternalConfigureOptions<TDbContext>>());
195+
return (TBuilder)this;
196+
}
197+
198+
/// <summary>
199+
/// Adds an action to configure the <see cref="ModelConfigurationBuilder"/> which can apply
200+
/// convention configurations for a <see cref="DbContext"/>.
201+
/// </summary>
202+
/// <param name="configure">Action to configure.</param>
203+
/// <returns>The same instance.</returns>
204+
public TBuilder ConfigureConventions(Action<ModelConfigurationBuilder> configure)
205+
{
206+
var service = new InternalConfigureConventions<TDbContext>(configure);
207+
Services.AddSingleton<IConfigureConventions<TDbContext>>(service);
208+
return (TBuilder)this;
209+
}
210+
}
211+
212+
internal sealed class InternalConfigureOptions<TDb> : IConfigureOptions<TDb>
213+
where TDb : DbContext
214+
{
215+
private readonly IServiceProvider sp;
216+
217+
public InternalConfigureOptions(IServiceProvider sp)
218+
{
219+
this.sp = sp;
220+
}
221+
222+
public void Configure(DbContextOptionsBuilder optionsBuilder)
223+
{
224+
var configurations = sp.GetRequiredService<IEnumerable<ConfigureOptionsAction<TDb>>>();
225+
foreach (var action in configurations)
226+
{
227+
action(sp, optionsBuilder);
228+
}
229+
}
230+
}
231+
232+
internal delegate void ConfigureOptionsAction<TDb>(IServiceProvider sp, DbContextOptionsBuilder optionsBuilder)
233+
where TDb : DbContext;
234+
235+
internal sealed class InternalConfigureModel<TDb> : IConfigureModel<TDb>
236+
where TDb : DbContext
237+
{
238+
public static InternalConfigureModel<TDb> GetFromServices(IServiceCollection services)
239+
{
240+
var descriptor = services.FirstOrDefault(d => d.ImplementationType == typeof(InternalConfigureModel<TDb>));
241+
if (descriptor is null || descriptor.ImplementationInstance is not InternalConfigureModel<TDb> options)
242+
{
243+
options = new InternalConfigureModel<TDb>();
244+
services.AddSingleton<IConfigureModel<TDb>>(options);
245+
}
246+
return options;
247+
}
248+
249+
private Action<ModelBuilder>? configure;
250+
251+
public void Configure(ModelBuilder modelBuilder)
252+
{
253+
if (configure is null)
254+
return;
255+
256+
configure(modelBuilder);
257+
}
258+
259+
public void Configure(Action<ModelBuilder> configure)
260+
{
261+
if (this.configure is null)
262+
this.configure = configure;
263+
else
264+
this.configure += configure;
265+
}
266+
}
267+
268+
internal sealed class InternalConfigureConventions<TDb> : IConfigureConventions<TDb>
269+
where TDb : DbContext
270+
{
271+
private readonly Action<ModelConfigurationBuilder> configure;
272+
273+
public InternalConfigureConventions(Action<ModelConfigurationBuilder> configure)
274+
{
275+
this.configure = configure;
276+
}
277+
278+
public void Configure(ModelConfigurationBuilder builder)
279+
{
280+
configure(builder);
281+
}
116282
}

0 commit comments

Comments
 (0)