Kevin McKelvin

The NH3 Music Store Part 2

28 August 2010

So my IMusicStoreContext is in place on all of the controllers now. I still need to actually write an implementation of IMusicStoreContext, but that can come later.

My next goal is to get all of the entity properties into my POCOs. The NHibernate way has typically been to pass instances of the object around instead of foreign key integers.

Using MVC means that I will at least need the primary key to be a part of my entity.

For example http://localhost/albums/1 must point to the album with id 1, so I’m going to need to keep my Id available to me in code. This is a design decision that’s important with NHibernate that isn’t possible with EF4.

Here’s the domain model I’ve come up with so far. This might change at some stage in the future. I had to add OrderDetail.

Notice that instead of passing around integer Id’s, I’m referencing the related types directly. For example – Genre on the Album class.

public class Album
{
    public virtual int AlbumId { get; set;}
    public virtual Genre Genre { get; set;}
    public virtual Artist Artist { get; set; }
    public virtual string Title { get; set; }
    public virtual decimal Price { get; set; }
    public virtual string AlbumArtUrl { get; set; }
}

public class Artist
{
    public virtual int ArtistId { get; set; }
    public virtual string Name { get; set; }
}

public class Cart
{
    public virtual int RecordId { get; set; }
    public virtual string CartId { get; set; }
    public virtual Album Album { get; set; }
    public virtual int Count { get; set; }
    public virtual DateTime DateCreated { get; set; }
}

public class Genre
{
    public virtual int GenreId { get; set; }
    public virtual string Name { get; set; }
    public virtual string Description { get; set; }
}

public class Order
{
    public virtual int OrderId { get; set; }
    public virtual DateTime OrderDate { get; set; }
    public virtual string Username { get; set; }
    public virtual string FirstName { get; set; }
    public virtual string LastName { get; set; }
    public virtual string Address { get; set; }
    public virtual string City { get; set; }
    public virtual string State { get; set; }
    public virtual string PostalCode { get; set; }
    public virtual string Country { get; set; }
    public virtual string Phone { get; set; }
    public virtual string Email { get; set; }
    public virtual string Total { get; set; }

    public virtual ICollection<OrderDetail> OrderDetails { get; set; }
}

public class OrderDetail
{
    public virtual int OrderDetailId { get; set; }
    public virtual Order Order { get; set; }
    public virtual Album Album { get; set; }
    public virtual int Quantity { get; set; }
    public virtual decimal UnitPrice { get; set; }
}

After all of this I’m down to 26 compiler errors. Yay!


Kevin McKelvin

These are the online musings of Kevin McKelvin. He is the CTO at Resource Guru, and lives in Newcastle upon Tyne, UK.