Skip to content

Instantly share code, notes, and snippets.

@ChrisMcKee
Created January 4, 2013 13:41
Show Gist options
  • Select an option

  • Save ChrisMcKee/4452665 to your computer and use it in GitHub Desktop.

Select an option

Save ChrisMcKee/4452665 to your computer and use it in GitHub Desktop.
namespace NUSSL.NUSExtra.Core.Model.Base
{
using System;
using System.Diagnostics;
public sealed class Check
{
#region Constructors
private Check()
{
}
#endregion
#region Fields
private static bool useAssertions;
#endregion
#region Properties
public static bool UseAssertions
{
get { return useAssertions; }
set { useAssertions = value; }
}
private static bool UseExceptions
{
get { return !useAssertions; }
}
#endregion
#region Methods
public static void Require(bool assertion, string message)
{
if (UseExceptions)
{
if (!assertion) throw new PreconditionException(message);
}
else
{
Trace.Assert(assertion, "Precondition: " + message);
}
}
public static void Require(bool assertion, string message, Exception inner)
{
if (UseExceptions)
{
if (!assertion) throw new PreconditionException(message, inner);
}
else
{
Trace.Assert(assertion, "Precondition: " + message);
}
}
public static void Require(bool assertion)
{
if (UseExceptions)
{
if (!assertion) throw new PreconditionException("Precondition failed.");
}
else
{
Trace.Assert(assertion, "Precondition failed.");
}
}
public static void Ensure(bool assertion, string message)
{
if (UseExceptions)
{
if (!assertion) throw new PostconditionException(message);
}
else
{
Trace.Assert(assertion, "Postcondition: " + message);
}
}
public static void Ensure(bool assertion, string message, Exception inner)
{
if (UseExceptions)
{
if (!assertion) throw new PostconditionException(message, inner);
}
else
{
Trace.Assert(assertion, "Postcondition: " + message);
}
}
public static void Ensure(bool assertion)
{
if (UseExceptions)
{
if (!assertion) throw new PostconditionException("Postcondition failed.");
}
else
{
Trace.Assert(assertion, "Postcondition failed.");
}
}
public static void Invariant(bool assertion, string message)
{
if (UseExceptions)
{
if (!assertion) throw new InvariantException(message);
}
else
{
Trace.Assert(assertion, "Invariant: " + message);
}
}
public static void Invariant(bool assertion, string message, Exception inner)
{
if (UseExceptions)
{
if (!assertion) throw new InvariantException(message, inner);
}
else
{
Trace.Assert(assertion, "Invariant: " + message);
}
}
public static void Invariant(bool assertion)
{
if (UseExceptions)
{
if (!assertion) throw new InvariantException("Invariant failed.");
}
else
{
Trace.Assert(assertion, "Invariant failed.");
}
}
public static void Assert(bool assertion, string message)
{
if (UseExceptions)
{
if (!assertion) throw new AssertionException(message);
}
else
{
Trace.Assert(assertion, "Assertion: " + message);
}
}
public static void Assert(bool assertion, string message, Exception inner)
{
if (UseExceptions)
{
if (!assertion) throw new AssertionException(message, inner);
}
else
{
Trace.Assert(assertion, "Assertion: " + message);
}
}
public static void Assert(bool assertion)
{
if (UseExceptions)
{
if (!assertion) throw new AssertionException("Assertion failed.");
}
else
{
Trace.Assert(assertion, "Assertion failed.");
}
}
#endregion
#region Events
#endregion
}
#region Exceptions
public class DesignByContractException : ApplicationException
{
protected DesignByContractException()
{
}
protected DesignByContractException(string message)
: base(message)
{
}
protected DesignByContractException(string message, Exception inner)
: base(message, inner)
{
}
}
public class PreconditionException : DesignByContractException
{
public PreconditionException()
{
}
public PreconditionException(string message)
: base(message)
{
}
public PreconditionException(string message, Exception inner)
: base(message, inner)
{
}
}
public class PostconditionException : DesignByContractException
{
public PostconditionException()
{
}
public PostconditionException(string message)
: base(message)
{
}
public PostconditionException(string message, Exception inner)
: base(message, inner)
{
}
}
public class InvariantException : DesignByContractException
{
public InvariantException()
{
}
public InvariantException(string message)
: base(message)
{
}
public InvariantException(string message, Exception inner)
: base(message, inner)
{
}
}
public class AssertionException : DesignByContractException
{
public AssertionException()
{
}
public AssertionException(string message)
: base(message)
{
}
public AssertionException(string message, Exception inner)
: base(message, inner)
{
}
}
#endregion
}
using System;
[Serializable]
public abstract class DomainObject<IdT, DomainT>
{
private Type domainType = typeof (DomainT);
public virtual IdT Id { get; protected set; }
public abstract override int GetHashCode();
public override bool Equals(object obj)
{
var compareTo = obj as DomainObject<IdT, DomainT>;
return (compareTo != null) &&
(HasSameNonDefaultIdAs(compareTo) ||
(((IsTransient()) || compareTo.IsTransient()) &&
HasSameBusinessSignatureAs(compareTo)));
}
protected virtual bool IsTransient()
{
return Id == null || Id.Equals(default(IdT));
}
private bool HasSameBusinessSignatureAs(DomainObject<IdT, DomainT> compareTo)
{
Check.Require(compareTo != null, "compareTo may not be null");
return GetHashCode().Equals(compareTo.GetHashCode());
}
private bool HasSameNonDefaultIdAs(DomainObject<IdT, DomainT> compareTo)
{
Check.Require(compareTo != null, "compareTo may not be null");
return compareTo != null && ((Id != null && !Id.Equals(default(IdT))) &&
(compareTo.Id != null && !compareTo.Id.Equals(default(IdT))) &&
Id.Equals(compareTo.Id));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment