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 / AbstractHandler.cs
Created September 19, 2022 19:06
A quick gist with generic methods for encapsulating try catch blocks and/or polly sync or async calls.
using Polly;
using Polly.Contrib.WaitAndRetry;
namespace CSharp.Tools
{
public abstract class AbstractHandler
{
/// <summary>
/// Regular "do work"
/// </summary>
@grgoncal
grgoncal / ClassTools.cs
Last active December 15, 2023 08:17
A simple get property value snippet to retrieve a class property using a string as an input. Use dots to access properties from child classes (e.g. Child.Property1).
public static class ClassUtils
{
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 / BaseMock.cs
Last active December 7, 2022 18:10
BaseMock class for unit testing
using Moq;
namespace BaseMock
{
public partial class BaseMock<TInterface, TAdapter>
where TInterface : class
where TAdapter : class, new()
{
protected readonly Mock<TInterface> mock = new();
@grgoncal
grgoncal / BuildEvents.sh
Created November 30, 2022 23:46
Build events for publishing libraries using Visual Studio
// Pre build events
if $(ConfigurationName) == Release (
del *.nupkg
echo "[PRE-BUILD] Deleted old *.nupkg"
)
// Post Build events
echo "[POST-BUILD] Started"
@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)
@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 / 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 / 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 / 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 / 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;
}