Skip to content

Instantly share code, notes, and snippets.

View grgoncal's full-sized avatar

Guilherme Rocha Goncalves grgoncal

  • Growin -> Engie
  • Brussels, Belgium
View GitHub Profile
@grgoncal
grgoncal / post-commit
Last active September 29, 2025 09:35
Git post-commit for erasing the past automatically
#!/bin/sh
#
# post-commit hook to keep only the last N commits individually
# WARNING: rewrites history! Pushes will require --force
#
if [ "$SKIP_POST_COMMIT" = "1" ]; then
exit 0
fi
@grgoncal
grgoncal / AbstractRequestHandler.cs
Last active September 25, 2024 09:20
An abstract request handler for Mediator
using MediatR;
using System;
using System.Threading;
using System.Threading.Tasks;
public abstract class AbstractRequestHandler<TRequest> : IRequestHandler<TRequest, Response>
where TRequest : IRequest<Response>
{
internal abstract Task<Response> ExecuteAsync(Response response, TRequest request, CancellationToken cancellationToken);
internal virtual Task<Response> HandleErrorAsync(Response response, Exception exception) => Task.FromResult(response.AddError(exception));
@grgoncal
grgoncal / ClassTools.cs
Last active June 18, 2024 16:04
Clone tools for lazy devs
public static class ClassTools
{
private static List<string> Enumerables = new List<string>() { "IList", "ICollection", "IEnumerable", "IReadOnlyList", "IReadOnlyCollection" };
public static object GetPropertyValue<T>(T source, string property)
{
if (!property.Contains("."))
return GetProperty(source, property);
var path = property.Split(new[] { '.' }, 2);
@grgoncal
grgoncal / IEnumerableExtensions.cs
Last active January 9, 2024 14:16
Method for grouping "foreachs". This is useful for processing in batches, logging and a lot of other usages. Thanks to "Kyle W" at this [StackOverflow Thread](https://stackoverflow.com/questions/30694476/process-a-list-with-a-loop-taking-100-elements-each-time-and-automatically-less) this solution
public static class IEnumerableExtensions
{
public static IEnumerable<IEnumerable<T>> MakeGroupsOf<T>(this IEnumerable<T> source, int take)
{
var grouping = new List<T>();
foreach ( var item in source)
{
grouping.Add(item);
if (grouping.Count == take)
@grgoncal
grgoncal / ConfigurationFactory.cs
Created April 6, 2023 11:12
Simple configuration factory to be used in UnitTesting. It builds an actual IConfiguration using appsettings.json.
public static class ConfigurationFactory
{
public static IConfiguration GetConfiguration(string file = null)
{
var configuration = new ConfigurationBuilder()
.AddJsonFile(file ?? "appsettings.json")
.Build();
return configuration;
}
@grgoncal
grgoncal / FilterExtensions.cs
Created March 17, 2023 19:41
Generic extensions for DynamicExpressions (Huge thanks for zHaytam for creating the github repo https://github.com/zHaytam/DynamicExpressions)
public static class FilterExtensions
{
public static DynamicFilterBuilder<T> Filter<T>(this T sourceEntity, Action<DynamicFilterBuilder<T>> filter)
{
var filterBuilder = new DynamicFilterBuilder<T>();
return filterBuilder.And(filter);
}
public static Expression<Func<T, bool>> Build<T>(this DynamicFilterBuilder<T> filterBuilder)
@grgoncal
grgoncal / BaseRepository.cs
Created December 15, 2022 15:59
BaseRepository for EF Core implementations
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection.Metadata;
using System.Threading.Tasks;
namespace BaseRepository
{
@grgoncal
grgoncal / run-sonarqube.txt
Created December 14, 2022 20:34
Sonarqube tests coverage method
--------------- dependencies
dotnet tool install --global dotnet-sonarscanner --version 5.2.0
dotnet tool update dotnet-reportgenerator-globaltool -g --version 4.8.7
--------------- runsonnar.bat
dotnet sonarscanner begin /k:"<project name>" /d:sonar.host.url="http://localhost:9000" /d:sonar.login="<login>" /d:sonar.coverageReportPaths=".\sonarqubecoverage\SonarQube.xml"
dotnet build
dotnet test --no-build --collect:"XPlat Code Coverage"
@grgoncal
grgoncal / ServiceProviderFactory.cs
Last active August 30, 2023 09:06
A ServiceProvider factory for mocking IServiceProvider in a HostedService .net core application. Useful when dealing with scopes. This code is not mine, shout out to "dimaaan" user on SO post https://stackoverflow.com/questions/44336489/moq-iserviceprovider-iservicescope
using Microsoft.Extensions.DependencyInjection;
using Moq;
using System;
namespace ServiceProviderFactory
{
public static class ServiceProviderFactory
{
public static Mock<IServiceProvider> GetServiceProvider(params (Type @interface, object service)[] services)
{
@grgoncal
grgoncal / PrivateObject.cs
Last active January 16, 2024 13:17
PrivateObject implementation for testing private methods in .NET Core. This code is not mine, thanks to "JJ" and "Milo Van der pas" in SO post: https://stackoverflow.com/questions/53273228/the-type-or-namespace-name-privateobject-could-not-be-found
public class PrivateObject<T>
{
private object o;
public PrivateObject(object o)
{
this.o = o;
}
public object Invoke(string methodName, params object[] args)