You can find me on twitter via @carl_furrow

Rolling my own blog engine, part 2

by Carl on May 21, 2009

Initially I was going to jump into my tests that I had written to exercise my data access layer via FluentNHibernate, but, I’ve decided that I wanted to get to AutoMapping first before I move ahead. Before I get to that though, I have to do a little housekeeping.

I wanted to get rid of the “RegisteredUser” class, and just have a “User” class. At the same time I saw a place to abstract a little code through inheritence, so I created a DomainEntity class (code is below).  While I was at it, I made my other classes, Blog and Post, inherit from DomainEntity as well.

public class User : DomainEntity
{
    public virtual IList<Blog> Blogs { get; set; }
    public virtual IList<Post> Posts { get; set; }
    public virtual string Email { get; set; }
    public virtual string Username { get; set; }
    public virtual string Password { get; set; }

    public User()
    {
        Blogs = new List<Blog>();
        Posts = new List<Post>();
        Email = string.Empty;
        Username = string.Empty;
        Password = string.Empty;
    }

}
public class DomainEntity : IEquatable<DomainEntity>
{
    public virtual Guid Id { get; set; }

    public virtual bool Equals(DomainEntity other)
    {
        if (ReferenceEquals(null, other))
        {
            return false;
        }

        if (ReferenceEquals(this, other))
        {
            return true;
        }

        return other.Id.Equals(Id);
    }

    public override bool Equals(object obj)
    {
        if (ReferenceEquals(null, obj))
        {
            return false;
        }

        if (ReferenceEquals(this, obj))
        {
            return true;
        }

        if (obj.GetType() != GetType())
        {
            return false;
        }

        return Equals((DomainEntity)obj);
    }

    public override int GetHashCode()
    {
        return Id.GetHashCode();
    }

    public static bool operator ==(DomainEntity left, DomainEntity right)
    {
        return Equals(left, right);
    }

    public static bool operator !=(DomainEntity left, DomainEntity right)
    {
        return !Equals(left, right);
    }
}

Now we’ve got a more simply named class (User) and I added a layer of abstraction by inheriting from a DomainEntity class.  The DomainEntity will become the base class of our other classes; Blog and Post and will handle things such as the Id property, as well as equality validation.  Their new code makeover, is as follows:

public class Blog : DomainEntity
{
    public virtual IList<Post> Posts { get; set; }
    public virtual string Title { get; set; }
    public virtual string Description { get; set; }
    public virtual DateTime LastUpdate { get; set; }
    public virtual User Owner { get; set; }

    public Blog()
    {
        Posts = new List<Post>();
        Title = string.Empty;
        Description = string.Empty;
        LastUpdate = DateTime.MinValue;
        Owner = new User();
    }
}
public class Post : DomainEntity
{
    public virtual string Title { get; set; }
    public virtual string Body { get; set; }
    public virtual DateTime LastUpdate { get; set; }
    public virtual DateTime PublishDate { get; set; }
    public virtual User Author { get; set; }

    public Post()
    {
        Author=new User();
        Title = string.Empty;
        Body = string.Empty;
        LastUpdate = DateTime.MinValue;
        PublishDate = DateTime.MinValue;
    }
}

Great, now things are setup in a way that we can move on to AutoMapping these classes to the database using a convention over configuration.  Last time I had a lot of code that I had to generate by hand in order to map each class to the database, and in turn, a table in that database.  It wasn’t terribly backbreaking work, but, there’s a simpler way, especially when you have more and more classes that need mapping to tables in the database.

I’ll go over my AutoMapping solution next time.

{ 1 trackback }

Rolling my own blog engine, part 3 — CarlFurrow.com
June 1, 2009 at 10:30 pm

{ 0 comments… add one now }

Leave a Comment

Previous post:

Next post: