Skip to content

Instantly share code, notes, and snippets.

Davide Guida mizrael

Block or report user

Report or block mizrael

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View AsyncTransaction.cs
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 Sep 3, 2019
an example of NOT to use a transaction with EF Core
View FaultyAsyncTransaction.cs
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 Apr 29, 2019
handling multiple exceptions from Task.WhenAll()
View parallel_calls_trycatch_aggregate.cs
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
View parallel_calls_trycatch.cs
try{
var task1 = DoSomethingAsync();
var task2 = DoSomethingElseAsync();
await Task.WhenAll(task1, task2);
}catch(Exception ex){
// do something meaningful with the exception
}
View parallel_calls.cs
var task1 = DoSomethingAsync();
var task2 = DoSomethingElseAsync();
await Task.WhenAll(task1, task2);
View decorator_example.cs
public interface IFoo{
void Bar();
}
public class Foo : IFoo{
public Bar(){
// whatever...
}
}
View instrumentation_example1.cs
var sw = new Stopwatch();
sw.Start();
Foo();
sw.Stop();
// now just log the results somewhere
@mizrael
mizrael / ComplexBuilderUsage.cs
Created Jul 13, 2018
Complex builder pattern usage
View ComplexBuilderUsage.cs
var builder = new Builder();
builder.WithSomeProperty(42);
// ...throw some business logic here...
builder.WithSomeOtherProperty(computedValue);
// ...some other logic here too...
@mizrael
mizrael / ImmutableBuilder.cs
Last active Sep 19, 2018
Immutable Builder Pattern in C#
View ImmutableBuilder.cs
public class Vehicle {
private Vehicle(string model, int wheelsCount){
this.Model = model;
this.WheelsCount = wheelsCount;
}
public string Model { get; }
public int WheelsCount { get; }
public VehicleBuilder {
@mizrael
mizrael / feature-gating-strategy.ts
Created Mar 6, 2018
Feature Gating - check the gates
View feature-gating-strategy.ts
export class Foo implements IFoo{
constructor(private readonly barStrategy:()=>void){}
public bar(){
this.barStrategy();
}
};
// composition root
const featureService:IFeatureService = new FeatureService(),
strategy1 = ():void =>{ /*...strategy 1...*/ },
You can’t perform that action at this time.