Created
December 10, 2012 11:15
-
-
Save ToJans/4250041 to your computer and use it in GitHub Desktop.
Ugly or not
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
//Add a comment | |
using System; | |
namespace Example | |
{ | |
public static class Guard | |
{ | |
public static void Against(bool assertion, string message) | |
{ | |
if (assertion) | |
throw new InvalidOperationException(message); | |
} | |
public static void That(bool assertion, string message) | |
{ | |
Guard.Against(!assertion, message); | |
} | |
public static void AgainstNullOrWhitespace(string s, string message) | |
{ | |
Guard.Against(string.IsNullOrWhiteSpace(s), message); | |
} | |
} | |
} |
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
namespace Example | |
{ | |
namespace Contracts | |
{ | |
namespace Members | |
{ | |
public interface ICommands | |
{ | |
void RegisterMember(string UserId); | |
} | |
public interface IEvents | |
{ | |
void MemberRegistered(string UserId); | |
} | |
public interface IState | |
{ | |
bool IsExistingMember(string UserId); | |
} | |
} | |
} | |
public class Member : Contracts.Members.ICommands | |
{ | |
Contracts.Members.IState State = null; | |
Contracts.Members.IEvents modifier = null; | |
public void RegisterMember(string UserId) | |
{ | |
Guard.AgainstNullOrWhitespace(UserId, "UserId has to contain at least one character"); | |
Guard.Against(State.IsExistingMember(UserId), "Duplicate userid"); | |
modifier.MemberRegistered(UserId); | |
} | |
} | |
public class MemberState : Contracts.Members.IState | |
{ | |
public const string UNIQUENESS_KEY = "members"; | |
public Contracts.Uniqueness.IState UniquenessState; | |
public MemberState() | |
{ | |
} | |
public bool IsExistingMember(string UserId) | |
{ | |
return UniquenessState.HasUniqueValue(UNIQUENESS_KEY, UserId); | |
} | |
} | |
public class MemberStateModifier : Contracts.Members.IEvents | |
{ | |
public Contracts.Uniqueness.IEvents Uniqueness = null; | |
public void MemberRegistered(string userId) | |
{ | |
Uniqueness.UniqueValueAdded(MemberState.UNIQUENESS_KEY, userId); | |
} | |
} | |
} |
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
using System.Collections.Generic; | |
namespace Example | |
{ | |
namespace Contracts | |
{ | |
namespace Uniqueness | |
{ | |
public interface ICommands | |
{ | |
void AddUniqueValue(string category, object uniquevalue); | |
void RemoveUniqueValue(string category, object uniquevalue); | |
} | |
public interface IEvents | |
{ | |
void UniqueValueAdded(string category, object value); | |
void UniqueValueRemoved(string category, object value); | |
} | |
public interface IState | |
{ | |
bool HasUniqueValue(string category, object value); | |
} | |
} | |
} | |
public class UniquenessImpl : Contracts.Uniqueness.ICommands | |
{ | |
public Contracts.Uniqueness.IEvents Modifier; | |
public Contracts.Uniqueness.IState State; | |
public UniquenessImpl() | |
{ | |
var stateImpl = new UniquenessState(); | |
this.State = stateImpl; | |
this.Modifier = new UniquenessModifier(stateImpl); | |
} | |
public void AddUniqueValue(string category, object uniquevalue) | |
{ | |
Guard.Against(State.HasUniqueValue(category, uniquevalue), "This value is not unique"); | |
Modifier.UniqueValueAdded(category, uniquevalue); | |
} | |
public void RemoveUniqueValue(string category, object uniquevalue) | |
{ | |
Guard.That(State.HasUniqueValue(category, uniquevalue), "The unique value is missing"); | |
Modifier.UniqueValueRemoved(category, uniquevalue); | |
} | |
} | |
public class UniquenessState : Contracts.Uniqueness.IState | |
{ | |
public Dictionary<string, List<object>> UniqueValues { get; private set; } | |
public UniquenessState() | |
{ | |
UniqueValues = new Dictionary<string, List<object>>(); | |
} | |
public bool HasUniqueValue(string category, object value) | |
{ | |
return UniqueValues.ContainsKey(category) && UniqueValues[category].Contains(value); | |
} | |
} | |
public class UniquenessModifier : Contracts.Uniqueness.IEvents | |
{ | |
UniquenessState state; | |
public UniquenessModifier(UniquenessState state) | |
{ this.state = state; } | |
public void UniqueValueAdded(string category, object value) | |
{ | |
if (!state.UniqueValues.ContainsKey(category)) | |
state.UniqueValues.Add(category, new List<object>()); | |
state.UniqueValues[category].Add(value); | |
} | |
public void UniqueValueRemoved(string category, object value) | |
{ | |
state.UniqueValues[category].Remove(value); | |
if (state.UniqueValues[category].Count == 0) | |
state.UniqueValues.Remove(category); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment