Skip to content

Instantly share code, notes, and snippets.

@azborgonovo
azborgonovo / SQL Server handy scripts.sql
Created December 7, 2022 16:18
SQL Server handy scripts
-- Check the BlkBy column
EXEC sp_who2
-- Get all requests that have blocking sessions
SELECT *
FROM sys.dm_exec_requests
WHERE DB_NAME(database_id) = 'YourDBName'
AND blocking_session_id <> 0
-- Get the queries themselves
@azborgonovo
azborgonovo / PerformanceLog.cs
Created August 4, 2020 13:15
Performance logging
var before0 = GC.CollectionCount(0);
var before1 = GC.CollectionCount(1);
var before2 = GC.CollectionCount(2);
var sw = Stopwatch.StartNew();
// Do...
sw.Stop();
@azborgonovo
azborgonovo / Service.cs
Last active November 8, 2019 15:57
Async WCF Operation Cancellation Pattern
[ServiceContract]
public interface IMyService
{
[OperationContract]
Task DoExpensiveOperation(Guid requestId);
[OperationContract(IsOneWay=true)]
void CancelExpensiveOperation(Guid requestId);
}
@azborgonovo
azborgonovo / ForEachAsyncWithoutPromises.js
Last active October 26, 2016 19:47
NodeJS commands and Javascript snippets
function forEach(array, asyncAction, callback) {
var size = array.length;
var count = 0;
var nextLoop = true;
array.forEach(function (item) {
if (nextLoop == true) {
asyncAction(item, function (err) {
if (err) {
nextLoop = false;
@azborgonovo
azborgonovo / Enumeration.cs
Created March 30, 2016 14:01
Enumeration classes
// https://lostechies.com/jimmybogard/2008/08/12/enumeration-classes/
public abstract class Enumeration : IComparable
{
private readonly int _value;
private readonly string _displayName;
protected Enumeration()
{
}
@azborgonovo
azborgonovo / NuGetPackAndPush
Last active July 17, 2020 21:33
NuGet packing and pushing
// References
// https://www.visualstudio.com/get-started/package/nuget/publish
// https://docs.nuget.org/create/creating-and-publishing-a-package
// https://docs.nuget.org/Create/using-a-gui-to-build-packages
// Accesing NuGet folder
cd {NuGet.exe folder}
// Packing from a .csproj file
nuget pack {relative file name}.csproj -Prop Configuration=Release
@azborgonovo
azborgonovo / Transactional.cs
Created March 10, 2016 15:09
Transactional code with a Volatile Resource Manager
// http://www.codeguru.com/csharp/.net/net_data/sortinganditerating/article.php/c10993/SystemTransactions-Implement-Your-Own-Resource-Manager.htm
// https://msdn.microsoft.com/en-us/library/ms229975(v=vs.85).aspx
public class VolatileRM : IEnlistmentNotification
{
private int memberValue = 0;
private int oldMemberValue = 0;
public int MemberValue
{
@azborgonovo
azborgonovo / Disposable.cs
Created March 7, 2016 16:54
Disposable abstract class in C#
/// <summary>
/// Abstraction for an easier Disposable pattern implementation.
/// Consumers should override the DisposeManagedObjects and DisposeUnmanagedObjects
/// to dispose the desired objects
/// </summary>
/// <remarks>https://msdn.microsoft.com/pt-br/library/fs2xkftw(v=vs.110).aspx</remarks>
public abstract class Disposable : IDisposable
{
protected bool disposed = false;
@azborgonovo
azborgonovo / UnitOfWork.cs
Last active April 20, 2024 13:54
Creating the 'best' Unit of Work and Repository implementation on C#
// Basic unitOfWork pattern as described by Martin Fowler (http://martinfowler.com/eaaCatalog/unitOfWork.html)
// Other methos as 'registerNew' are going to be managed by each repository
public interface IUnitOfWork : IDisposable
{
void Commit();
Task CommitAsync();
void Rollback();
}
public interface IUnitOfWorkFactory
@azborgonovo
azborgonovo / Validation.cs
Last active July 17, 2020 22:01
Validation in C# with System.ComponentModel
public interface IObjectValidator<T>
{
IEnumerable<ValidationResult> Validate(T @object);
}
public bool Validate(T objeto, out ICollection<ValidationResult> results)
{
var validationResults = new List<ValidationResult>();
// Default validation