Welcome Guest, you are in: Login

dbones docs

RSS RSS

Navigation (ArmChair)





Search the wiki
»

Entity - Default Rules

RSS
Modified on 2014/09/14 02:48 by Dave Categorized as Uncategorized

outline

There are some things know about when modelling your entities. This page will show the guidelines to follow with your classes, followed with a example.

Guidelines

Your entities must abide by the following:

  • Id Field on the Aggregate Root
  • Revision Field on the Aggregate Root
  • Parameter-less constructor is required

When designing your entities consider the following

  • Serialization is against the fields, not the properties (it does support auto properties)
  • Design your entities, using Aggregate Roots. This will compliment Json storage
  • Id's are generated when you add the entity into a session
  • Revision is generated after commit
  • Follow SOLID principals (ArmChair does not enforce you to use virtual)


Simple Example



Book

The following example shows the Book class, which is a Aggregate Root, it follows all the rules listed above.

note that the Person class, is another Root, and therefor the Book will only relate to it via the Persons Id.


/// <summary>
/// base for all domain object which use this Uow
/// </summary>
public abstract class EntityRoot
{
    public virtual string Id { get; set; }
    public virtual string Rev { get; set; }
}

public class Book : EntityRoot
{
    private readonly IList<Contributor> _contributors = new List<Contributor>();
    private readonly IList<Edition> _editions = new List<Edition>();

    public Book() { }

    public Book(string title, Person author)
    {
        Title = title;
        _contributors.Add(new Contributor(author, ContributorType.Author));
    }

    public virtual string Title { get; private set; }
    public virtual IEnumerable<Contributor> Contributors { get { return _contributors; } }
    public virtual IEnumerable<Edition> Editions { get { return _editions; } }

    public virtual void AddContributor(Person contributor, ContributorType contributorType)
    {
        if (_contributors.Any(x => x.ContributorId == contributor.Id))
            return;

        _contributors.Add(new Contributor(contributor, contributorType));
    }

    public virtual void AddEdition(Edition edition)
    {
        if (_editions.Contains(edition))
        {
            _editions.Remove(edition);
        }
        _editions.Add(edition);
    }
}


Contributor

The Contributor class is not an Aggregate Root, so it does not require the ID and Revision Attributes


public class Contributor
{
    protected Contributor() { }

    public Contributor(string id, string name, ContributorType contribution)
    {
        ContributorId = id;
        Name = name;
        Contribution = contribution;
    }

    public Contributor(Person person, ContributorType contribution)
        : this(person.Id, person.Name, contribution)
    {

    }

    public virtual string Name { get; private set; }
    public virtual ContributorType Contribution { get; private set; }
    public virtual string ContributorId { get; private set; }
}

Edition

You can override the Equals and Hash as you would normally in simple POCO's


public class Edition
{
    protected int _hash;

    protected Edition() { }

    public Edition(string name, EditionType editionType)
    {
        Name = name;
        Type = editionType;
        _hash = Name.GetHashCode() + Type.GetHashCode();
    }
        
    public virtual DateTime ReleaseDate { get; set; }
    public virtual string Name { get; private set; }
    public virtual EditionType Type { get; private set; }

    public override bool Equals(object obj)
    {
        var other = obj as Edition;
        if (other == null)
        {
            return false;
        }

        return other.GetHashCode() == GetHashCode();
    }

    public override int GetHashCode()
    {
        return _hash;
    }
}
dbones docs, is a sub-site of dbones.co.uk, all its content belongs to dbones.co.uk, this also includes logo's.