Refactor envelope creation logic and add IEnvelopeExecutor
Updated CreateEnvelopeCommandHandler to use IEnvelopeExecutor for managing envelope creation. Introduced CreateParmas method for parameter handling in CreateEnvelopeSQL. Corrected Dapper type mapping method name and added service registration for IEnvelopeExecutor in DependencyExtensions. Refactored SQLExecutor to enhance encapsulation. Created EnvelopeExecutor class implementing IEnvelopeExecutor for dedicated envelope operations.
This commit is contained in:
@@ -12,6 +12,7 @@ using EnvelopeGenerator.Infrastructure.Executor;
|
||||
using Dapper;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Reflection;
|
||||
using DigitalData.UserManager.Domain.Entities;
|
||||
|
||||
namespace EnvelopeGenerator.Infrastructure;
|
||||
|
||||
@@ -74,11 +75,14 @@ public static class DIExtensions
|
||||
services.AddSQLExecutor<DocumentReceiverElement>();
|
||||
services.AddSQLExecutor<DocumentStatus>();
|
||||
|
||||
SetDappeTypeMap<Envelope>();
|
||||
SetDappeTypeMap<Receiver>();
|
||||
SetDappeTypeMap<EnvelopeDocument>();
|
||||
SetDappeTypeMap<DocumentReceiverElement>();
|
||||
SetDappeTypeMap<DocumentStatus>();
|
||||
SetDapperTypeMap<Envelope>();
|
||||
SetDapperTypeMap<User>();
|
||||
SetDapperTypeMap<Receiver>();
|
||||
SetDapperTypeMap<EnvelopeDocument>();
|
||||
SetDapperTypeMap<DocumentReceiverElement>();
|
||||
SetDapperTypeMap<DocumentStatus>();
|
||||
|
||||
services.AddScoped<IEnvelopeExecutor, EnvelopeExecutor>();
|
||||
|
||||
if (sqlExecutorConfiguration is not null || sqlExecutorConfigureOptions is not null)
|
||||
services.AddSQLExecutor(sqlExecutorConfiguration, sqlExecutorConfigureOptions);
|
||||
@@ -100,7 +104,7 @@ public static class DIExtensions
|
||||
return services.AddSingleton<ISQLExecutor, SQLExecutor>();
|
||||
}
|
||||
|
||||
private static void SetDappeTypeMap<TModel>()
|
||||
private static void SetDapperTypeMap<TModel>()
|
||||
{
|
||||
Dapper.SqlMapper.SetTypeMap(typeof(TModel), new CustomPropertyTypeMap(
|
||||
typeof(TModel),
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
using Dapper;
|
||||
using DigitalData.UserManager.Application.Contracts.Repositories;
|
||||
using DigitalData.UserManager.Infrastructure.Repositories;
|
||||
using EnvelopeGenerator.Application.Contracts.SQLExecutor;
|
||||
using EnvelopeGenerator.Application.SQL;
|
||||
using EnvelopeGenerator.Domain.Entities;
|
||||
using Microsoft.Data.SqlClient;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace EnvelopeGenerator.Infrastructure.Executor;
|
||||
|
||||
public class EnvelopeExecutor : SQLExecutor, IEnvelopeExecutor
|
||||
{
|
||||
private readonly IUserRepository _userRepository;
|
||||
|
||||
public EnvelopeExecutor(ILogger<EnvelopeExecutor> logger, IServiceProvider provider, IOptions<SQLExecutorParams> sqlExecutorParamsOptions, IUserRepository userRepository) : base(provider, sqlExecutorParamsOptions)
|
||||
{
|
||||
_userRepository = userRepository;
|
||||
}
|
||||
|
||||
public async Task<Envelope?> CreateEnvelopeAsync(DynamicParameters parameters, bool addUser = true, CancellationToken cancellation = default)
|
||||
{
|
||||
using var connection = new SqlConnection(Params.ConnectionString);
|
||||
var sql = Provider.GetRequiredService<CreateEnvelopeSQL>();
|
||||
await connection.OpenAsync(cancellation);
|
||||
var envelopes = await connection.QueryAsync<Envelope>(sql.Raw, parameters);
|
||||
var envelope = envelopes.FirstOrDefault();
|
||||
|
||||
if (envelope is null)
|
||||
return null;
|
||||
|
||||
// Add User
|
||||
if (addUser)
|
||||
{
|
||||
var user = await _userRepository.ReadByIdAsync(envelope.UserId);
|
||||
envelope.User = user;
|
||||
}
|
||||
|
||||
return envelope;
|
||||
}
|
||||
}
|
||||
@@ -8,26 +8,26 @@ namespace EnvelopeGenerator.Infrastructure.Executor;
|
||||
|
||||
public class SQLExecutor : ISQLExecutor
|
||||
{
|
||||
private readonly SQLExecutorParams _params;
|
||||
protected readonly SQLExecutorParams Params;
|
||||
|
||||
private readonly IServiceProvider _provider;
|
||||
protected readonly IServiceProvider Provider;
|
||||
|
||||
public SQLExecutor(IServiceProvider provider, IOptions<SQLExecutorParams> sqlExecutorParamsOptions)
|
||||
{
|
||||
_provider = provider;
|
||||
_params = sqlExecutorParamsOptions.Value;
|
||||
Provider = provider;
|
||||
Params = sqlExecutorParamsOptions.Value;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<TEntity>> Execute<TEntity>(string sql, DynamicParameters parameters, CancellationToken cancellation = default)
|
||||
{
|
||||
using var connection = new SqlConnection(_params.ConnectionString);
|
||||
using var connection = new SqlConnection(Params.ConnectionString);
|
||||
await connection.OpenAsync(cancellation);
|
||||
return await connection.QueryAsync<TEntity>(sql, parameters);
|
||||
}
|
||||
|
||||
public Task<IEnumerable<TEntity>> Execute<TEntity, TSQL>(DynamicParameters parameters, CancellationToken cancellation = default) where TSQL : ISQL
|
||||
{
|
||||
var sql = _provider.GetRequiredService<TSQL>();
|
||||
var sql = Provider.GetRequiredService<TSQL>();
|
||||
return Execute<TEntity>(sql.Raw, parameters, cancellation);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user