Created
February 15, 2011 17:49
-
-
Save chrisnicola/827898 to your computer and use it in GitHub Desktop.
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
/// <summary> | |
/// An entity which references a Category | |
/// </summary> | |
public class CategoryRef : Entity<Catalog> | |
{ | |
string _slug; | |
public CategoryRef(Catalog catalog, Category category, Category parentCategory) : base(catalog, Guid.NewGuid()) | |
{ | |
ApplyEvent(new CategoryAddedToCatalog {Category = category, Slug = category.Name.ToSlug() }); | |
} | |
public void SetSlug(string slug) | |
{ | |
ApplyEvent(new CategorySlugChanged { Slug = slug }); | |
} | |
public Category Category { get; private set; } | |
void On(CategoryAddedToCatalog evnt) | |
{ | |
Category = evnt.Category; | |
_slug = evnt.Slug; | |
} | |
void On(CategorySlugChanged evnt) { _slug = evnt.Slug; } | |
} | |
/// <summary> | |
/// This is a Value Object representing a category. | |
/// - A category may have a parent Category, but no ancestor may have the same name as this Category | |
/// - A category must be made of only alpha characters and single spaces | |
/// - Leading/trailing whitespace will be trimmed automatically | |
/// </summary> | |
public class Category | |
{ | |
public Category(string name, Category parent = null) | |
{ | |
ValidateCategoryName(name); | |
ValidateParentCategory(parent, name); | |
Name = name.CleanNameString(); | |
Parent = parent; | |
} | |
public string Name { get; private set; } | |
public Category Parent { get; private set; } | |
static void ValidateParentCategory(Category parent, string name) | |
{ | |
var cat = parent; | |
while (cat != null); | |
{ | |
if (cat.Name == name) | |
throw new DomainException("A new category cannot have the same name as one of it's ancestors"); | |
cat = cat.Parent; | |
} | |
} | |
static void ValidateCategoryName(string name) | |
{ | |
var validated = name.Trim(); | |
if (!Regex.Match(validated, "[a-zA-Z ]*").Success) | |
throw new DomainException("A category name must be made up of only letters and spaces"); | |
} | |
public bool Equals(Category other) | |
{ | |
if (ReferenceEquals(null, other)) return false; | |
if (ReferenceEquals(this, other)) return true; | |
return Equals(other.Name, Name) && Equals(other.Parent, Parent); | |
} | |
public override bool Equals(object obj) | |
{ | |
if (ReferenceEquals(null, obj)) return false; | |
if (ReferenceEquals(this, obj)) return true; | |
if (obj.GetType() != typeof (Category)) return false; | |
return Equals((Category) obj); | |
} | |
public override int GetHashCode() | |
{ | |
unchecked | |
{ | |
return ((Name != null ? Name.GetHashCode() : 0) * 397) ^ (Parent != null ? Parent.GetHashCode() : 0); | |
} | |
} | |
public static bool operator ==(Category left, Category right) { return Equals(left, right); } | |
public static bool operator !=(Category left, Category right) { return !Equals(left, right); } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment