Last active
January 7, 2018 21:53
-
-
Save Im5tu/51ce3280674e7b8f03939c5aaa0dd52f to your computer and use it in GitHub Desktop.
Correlation Part 1: The Context
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class CorrelationContext | |
{ | |
public static Stack<Guid> GetCorrelationStack() {} | |
public static Guid? GetCorrelationId() {} | |
public static IDisposable CreateCorrelation(Guid? correlationId = null) {} | |
public static IDisposable CreateCorrelationIfNotExists(Guid? correlationId = null) {} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
using(CorrelationContext.CreateCorrelationIfNotExists()) | |
{ | |
Console.WriteLine(CorrelationContext.GetCorrelationId()); | |
using(CorrelationContext.CreateCorrelationIfNotExists()) | |
{ | |
Console.WriteLine(CorrelationContext.GetCorrelationId()); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class CorrelationContext | |
{ | |
private static readonly AsyncLocal<Stack<Guid>> _correlationStack = new AsyncLocal<Stack<Guid>>(); | |
public static Stack<Guid> GetCorrelationStack() => _correlationStack.Value ?? new Stack<Guid>(); | |
public static Guid? GetCorrelationId() | |
{ | |
var stack = GetCorrelationStack(); | |
if(stack.Count > 0) | |
return stack.Peek(); | |
return null; // TODO :: decide what you are going to do | |
} | |
public static IDisposable CreateCorrelation(Guid? correlationId = null) | |
{ | |
lock(_correlationStack) | |
{ | |
if (_correlationStack.Value == null) | |
_correlationStack.Value = new Stack<Guid>(); | |
_correlationStack.Value.Push(correlationId ?? Guid.NewGuid()); | |
} | |
return new DisposeCorrelation(_correlationStack); | |
} | |
public static IDisposable CreateCorrelationIfNotExists(Guid? correlationId = null) | |
{ | |
if(GetCorrelationStack().Count > 0) | |
return new EmptyDisposable(); | |
return CreateCorrelation(correlationId); | |
} | |
private class EmptyDisposable : IDisposable { public void Dispose() { } } | |
private class DisposeCorrelation : IDisposable | |
{ | |
private readonly AsyncLocal<Stack<Guid>> _localStack; | |
public DisposeCorrelation(AsyncLocal<Stack<Guid>> localStack) | |
{ | |
_localStack = localStack; | |
} | |
public void Dispose() | |
{ | |
var stack = _localStack.Value ?? new Stack<Guid>(); | |
if(stack.Count > 0) | |
stack.Pop(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static IDisposable CreateCorrelation(Guid? correlationId = null) | |
{ | |
lock(_correlationStack) | |
{ | |
if (_correlationStack.Value == null) | |
_correlationStack.Value = new Stack<Guid>(); | |
_correlationStack.Value.Push(correlationId ?? Guid.NewGuid()); | |
} | |
return new DisposeCorrelation(_correlationStack); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static IDisposable CreateCorrelationIfNotExists(Guid? correlationId = null) | |
{ | |
if(GetCorrelationStack().Count > 0) | |
return new EmptyDisposable(); | |
return CreateCorrelation(correlationId); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private class DisposeCorrelation : IDisposable | |
{ | |
private readonly AsyncLocal<Stack<Guid>> _localStack; | |
public DisposeCorrelation(AsyncLocal<Stack<Guid>> localStack) | |
{ | |
_localStack = localStack; | |
} | |
public void Dispose() | |
{ | |
var stack = _localStack.Value ?? new Stack<Guid>(); | |
if(stack.Count > 0) | |
stack.Pop(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static Guid? GetCorrelationId() | |
{ | |
var stack = GetCorrelationStack(); | |
if(stack.Count > 0) | |
return stack.Peek(); | |
return null; // TODO :: decide what you are going to do | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static readonly AsyncLocal<Stack<Guid>> _correlationStack = new AsyncLocal<Stack<Guid>>(); | |
public static Stack<Guid> GetCorrelationStack() => _correlationStack.Value ?? new Stack<Guid>(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment