Skip to content

Instantly share code, notes, and snippets.

View mizrael's full-sized avatar
😼
my cat owns the keyboard.

David Guida mizrael

😼
my cat owns the keyboard.
View GitHub Profile
@mizrael
mizrael / Startup.cs
Created October 1, 2021 14:09
DI-friendly Factory Pattern in C#
.ConfigureServices((hostContext, services) =>
{
services.AddTransient<PdfReader>()
.AddTransient<PdfProcessor>()
.AddTransient<OdtReader>()
.AddTransient<OdtProcessor>();
services.AddSingleton<WordReader>()
.AddSingleton<WordProcessor>();
@mizrael
mizrael / factory-with-dependencies.cs
Created October 1, 2021 14:07
sample Factory Pattern in C#, handling services with dependencies.
public interface IDocumentProcessor { }
public class PdfProcessor : IDocumentProcessor {
public PdfProcessor(PdfReader reader) { }
}
public class OdtProcessor : IDocumentProcessor {
public OdtProcessor(OdtReader reader) { }
}
@mizrael
mizrael / simple-factory.cs
Created October 1, 2021 14:05
Simple Factory implementation in C#
public interface IShapeRenderer { }
public class CircleRenderer : IShapeRenderer { }
public class TriangleRenderer : IShapeRenderer { }
public class SquareRenderer : IShapeRenderer { }
public interface IShapeRendererFactory
{
IShapeRenderer Create(string type);
}
@mizrael
mizrael / HealthchecksEndpoint.cs
Last active April 3, 2020 02:33
custom health check .NET Core example
public void Configure(IApplicationBuilder app)
{
var healthCheckOptions = new HealthCheckOptions()
{
ResponseWriter = WriteReadinessResponse
};
app.UseEndpoints(endpoints =>
{
endpoints.MapHealthChecks("/ops/health", healthCheckOptions);
using (var tran = await _dbContext.BeginTransactionAsync())
{
try
{
var modelsToRemove = await _dbContext.QueryModels
.Where(f => /* some filter here */)
.ToArrayAsync();
if (modelsToRemove.Any())
_dbContext.QueryModels.RemoveRange(modelsToRemove);
@mizrael
mizrael / FaultyAsyncTransaction.cs
Last active September 3, 2019 09:17
an example of NOT to use a transaction with EF Core
using (var tran = await _dbContext.BeginTransactionAsync())
{
try
{
var modelsToRemove = await _dbContext.QueryModels
.Where(f => /* some filter here */)
.ToArrayAsync();
if (modelsToRemove.Any())
_dbContext.QueryModels.RemoveRange(modelsToRemove);
@mizrael
mizrael / parallel_calls_trycatch_aggregate.cs
Created April 29, 2019 10:27
handling multiple exceptions from Task.WhenAll()
Task aggregationTask = null;
try{
var task1 = DoSomethingAsync();
var task2 = DoSomethingElseAsync();
aggregationTask = Task.WhenAll(task1, task2);
await aggregationTask;
}catch(Exception ex){
if(aggregationTask?.Exception?.InnerExceptions != null && aggregationTask.Exception.InnerExceptions.Any())
foreach (var innerEx in aggregationTask.Exception.InnerExceptions){
// do your magic here
try{
var task1 = DoSomethingAsync();
var task2 = DoSomethingElseAsync();
await Task.WhenAll(task1, task2);
}catch(Exception ex){
// do something meaningful with the exception
}
var task1 = DoSomethingAsync();
var task2 = DoSomethingElseAsync();
await Task.WhenAll(task1, task2);
public interface IFoo{
void Bar();
}
public class Foo : IFoo{
public Bar(){
// whatever...
}
}