Created
September 28, 2014 12:21
-
-
Save codescribler/559949b1cddca3126cbe to your computer and use it in GitHub Desktop.
Example concurrency conflict resolution registry
This file contains hidden or 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 interface IConcurrencyConflictResolver | |
{ | |
bool ConflictsWith(Type eventToCheck, IEnumerable<Type> previousEvents); | |
void RegisterConflictList(Type eventDefinition, List<Type> conflictsWith); | |
} | |
public class ConcurrencyConflictResolver : IConcurrencyConflictResolver | |
{ | |
private readonly Dictionary<Type, List<Type>> _conflictRegister; | |
public ConcurrencyConflictResolver() | |
{ | |
_conflictRegister = new Dictionary<Type, List<Type>>(); | |
} | |
public bool ConflictsWith(Type eventToCheck, IEnumerable<Type> previousEvents) | |
{ | |
// If type not registered assume the worst and say it conflicts | |
if (!_conflictRegister.ContainsKey(eventToCheck)) return true; | |
return previousEvents.Any(previousEvent => _conflictRegister[eventToCheck].Any(et => et == previousEvent)); | |
} | |
public void RegisterConflictList(Type eventDefinition, List<Type> conflictsWith) | |
{ | |
if (!eventDefinition.IsSubclassOf(typeof(Event))) throw new ArgumentException("eventDefinition must be of type Event"); | |
if (conflictsWith.Any(c => c.IsSubclassOf(typeof(Event)) == false)) throw new ArgumentException("all conflicts with type must be of type Event"); | |
if (_conflictRegister.ContainsKey(eventDefinition)) | |
_conflictRegister.Remove(eventDefinition); | |
_conflictRegister.Add(eventDefinition, conflictsWith); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment