Dropps
Dropps
CC#
Created by Dropps on 1/5/2024 in #help
how to test untestable code
No description
9 replies
CC#
Created by Dropps on 12/17/2023 in #help
options pattern not working
migrated not so long ago .net 8 did they change something with the options pattern? it seems to not work anymore for some reason that i cant explain myself see code below:
builder.Services.AddOptions<JwtOptions>(JwtOptions.SectionName)
.Bind(config.GetSection(JwtOptions.SectionName));

builder.Services.AddOptions<DatabaseConnectionOptions>(DatabaseConnectionOptions.SectionName)
.Bind(config.GetSection(DatabaseConnectionOptions.SectionName));

builder.Services.AddOptions<JwtOptions>(JwtOptions.SectionName)
.Bind(config.GetSection(JwtOptions.SectionName));

builder.Services.AddOptions<DatabaseConnectionOptions>(DatabaseConnectionOptions.SectionName)
.Bind(config.GetSection(DatabaseConnectionOptions.SectionName));

inside here it should retrive the stuff from the appsettings.json
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"ConnectionStrings": {
"DefaultConnection": "Server=db;Database=postgres;User ID=admin;Password=admin;"
},
"Jwt": {
"Key": "HIDDEN_API_KEY"
},
"AllowedHosts": "*"
}
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"ConnectionStrings": {
"DefaultConnection": "Server=db;Database=postgres;User ID=admin;Password=admin;"
},
"Jwt": {
"Key": "HIDDEN_API_KEY"
},
"AllowedHosts": "*"
}
but whenever i hit my AddInfrastructure point i get an error that it would be null see her:
...
// Database
var options = serviceCollection.BuildServiceProvider().GetRequiredService<IOptions<DatabaseConnectionOptions>>();

if (string.IsNullOrWhiteSpace(options.Value.DefaultConnection))
{
throw new InvalidOperationException($"{DatabaseConnectionOptions.SectionName}:{nameof(DatabaseConnectionOptions.DefaultConnection)} cannot be null or whitespace");
}

serviceCollection.AddDbContext<AppDbContext>(dbOptions =>
{
dbOptions.UseNpgsql(options.Value.DefaultConnection);
});

serviceCollection.AddScoped<IUnitOfWork>(serviceProvider => serviceProvider.GetRequiredService<AppDbContext>());

return serviceCollection;
...
// Database
var options = serviceCollection.BuildServiceProvider().GetRequiredService<IOptions<DatabaseConnectionOptions>>();

if (string.IsNullOrWhiteSpace(options.Value.DefaultConnection))
{
throw new InvalidOperationException($"{DatabaseConnectionOptions.SectionName}:{nameof(DatabaseConnectionOptions.DefaultConnection)} cannot be null or whitespace");
}

serviceCollection.AddDbContext<AppDbContext>(dbOptions =>
{
dbOptions.UseNpgsql(options.Value.DefaultConnection);
});

serviceCollection.AddScoped<IUnitOfWork>(serviceProvider => serviceProvider.GetRequiredService<AppDbContext>());

return serviceCollection;
options classes have similar schema each:
public class DatabaseConnectionOptions
{
public const string SectionName = "ConnectionStrings";

public string? DefaultConnection { get; set; }
}
public class DatabaseConnectionOptions
{
public const string SectionName = "ConnectionStrings";

public string? DefaultConnection { get; set; }
}
someone knows how to fix this?
28 replies
CC#
Created by Dropps on 12/16/2023 in #help
✅ SSL Certificate for Docker for local development
hello there... i currently run into some issues with testing my app (asp.net web api and blazor web app) as i have both set to require an ssl certificate to run i would have to use some sort of local test cert well the issue with that is that when i run my apps using riders build in config starter everything is fine but as soon as i build the docker images for it and run it via that the development ssl certificate does not want to work anymore even tho i ran dotnet dev-certs https --trust before docker deploy anyone some ideas what could be wrong? env variables for both the api and the blazor web app are set to ASPNETCORE_ENVIRONMENT=Development on my local machine as well as in the docker compose file
55 replies
CC#
Created by Dropps on 11/27/2023 in #help
validation pipeline using ErrorOr package
public class ValidationBehaviour<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
where TRequest : IRequest<TResponse>
where TResponse : IErrorOr
{
private readonly IValidator<TRequest>? _validator;

public ValidationBehaviour(IValidator<TRequest>? validator)
{
_validator = validator;
}

public async Task<TResponse> Handle(
TRequest request,
RequestHandlerDelegate<TResponse> next,
CancellationToken cancellationToken)
{
if (_validator == null)
{
return await next();
}

ValidationResult? validationResult = await _validator.ValidateAsync(request, cancellationToken);

if (validationResult.IsValid)
{
return await next();
}

var errors = validationResult.Errors
.ConvertAll(validationFailure =>
Error.Validation(
validationFailure.PropertyName,
validationFailure.ErrorMessage));

return (dynamic)errors;
}
}
public class ValidationBehaviour<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
where TRequest : IRequest<TResponse>
where TResponse : IErrorOr
{
private readonly IValidator<TRequest>? _validator;

public ValidationBehaviour(IValidator<TRequest>? validator)
{
_validator = validator;
}

public async Task<TResponse> Handle(
TRequest request,
RequestHandlerDelegate<TResponse> next,
CancellationToken cancellationToken)
{
if (_validator == null)
{
return await next();
}

ValidationResult? validationResult = await _validator.ValidateAsync(request, cancellationToken);

if (validationResult.IsValid)
{
return await next();
}

var errors = validationResult.Errors
.ConvertAll(validationFailure =>
Error.Validation(
validationFailure.PropertyName,
validationFailure.ErrorMessage));

return (dynamic)errors;
}
}
i have the feeling using dynamic to make the compiler know that we have implicit operators for errors is not the best idea anyone has suggsetions on what to do instead?
27 replies
CC#
Created by Dropps on 11/9/2023 in #help
❔ question around testing techniques and ressources for it
good evening everyone i recently started learning more in depth about testing tdd ddd and corresponding frameworks which seems to me the best matching for how i wanna work on my projects long story short i would like to start here a small discussion around your toughts about tdd and ddd as well as their frameworks and ressources (for learning) for it currently iam building a sample application using principles of clean architecture around 3 daya ago i heard about tdd and how the red green blue cycle is supposed to work which i really liked in theory at least today i tried getting that into practice but there are a few issues concerning me: - missing ressources about E2E testing with web apis (spend 3h searching found nothing useful) - missing ressources about integrationtesting (spend 2h and found some small things but again nothing really useful or compatible to what iam doing) - unclear practices about unit testing (what defines a unit what types of dependencies to mock what to fake and what to leave as is) - except for playwright there seem to be no frameworks for automated UI tests in blazor webassembly i may be wrong there? last but not least iam eager to take courses or webinars teaching those things in depth so iam also interested in maybe some suggestions around courses or something like that to buy
155 replies