So I’ve been rolling my own blog engine just for the learning experience, and the plan was to use it to host this portfolio site. Once I got into the code, I realized how much I wanted to get out of it, and the feature set was large, so, I stepped back, and took my wife’s suggestion to just throw up a quick Wordpress blog, and keep working on the blog engine on the side, and post code examples along the way. So, that’s exactly what I’m doing.
So far it’s still in its infancy, but it’s coming along nicely. I’ve found that I’ve been most productive from 11p-3am, and I’m not sure why just yet. To construct this blog engine, I pulled together some frameworks that I knew, plus a few I wanted to learn:
- ASP.NET MVC
- Fluent NHibernate
- MbUnit
- Moq
- jQuery
- 960 Grid CSS framework
I wanted to touch on Fluent NHibernate for a bit, because I’m really loving my XML-less configurations, now that I’ve gotten the hang of it (I think). I’m only going to cover the current C# class definitions and the initial mapping I came up with, and I hope to get into more detail in subsequent posts. Here we go!
To start, I only needed three basic classes (and an interface). I imagine this will be changed and modified over time, but that’s part of the learning experience I suppose.
Managing the relationships between these classes in Fluent NHibernate was fairly simple. After I was done mapping, I had the following relationships setup:
- Blog
- Has an ID column
- Has a title
- Has one owner
- Has a collection of posts
- Post
- Has an ID
- Has a title
- Has a body (the HTML of the post)
- Has one author/user
- Has one blog
- RegisteredUser
- Has an ID
- Has a username
- Has an email
- Has a password
- Has a short name (or nickname)
- Has (potentially) many posts
- Has (potentially) many blogs
This way, if I have a user object, I can enumerate through all their blogs or posts very easily.
Here are the mappings of each of the classes, again, they will probably be modified over time, but this is where they stand right now:
public BlogMapping()
{
Id(x => x.BlogId).GeneratedBy.Native();
Map(x => x.Title);
References(x => x.Owner).ColumnName("OwnerId"); //many blogs to one owner
HasMany(x => x.Posts) //one blog has many posts
.Cascade.All()
.AsSet()
.Inverse();
}
//...
public PostMapping()
{
Id(x => x.PostId).GeneratedBy.Native();
Map(x => x.Title);
Map(x => x.Body);
References(x => x.Author).ColumnName("AuthorId"); //many posts have one author
References(x => x.Blog).ColumnName("BlogId"); //many posts have one blog
}
//...
public RegisteredUserMapping()
{
WithTable("User");
Id(x => x.UserId);
Map(x => x.UserName);
Map(x => x.Email);
Map(x => x.Password);
Map(x => x.ShortName);
HasMany(x => x.Posts)
.Inverse()
.Cascade.All()
.AsSet()
.KeyColumnNames.Add("AuthorId");
HasMany(x => x.Blogs)
.Inverse() //The blog object maintains whether or not this is the owner
.Cascade.All() //Cascade deletes, and updates if the owner is deleted or updated.
.AsSet() //We want a set, so that there are unique blogs in the collection.
.KeyColumnNames.Add("OwnerId"); //Column on the Blog row
}
public BlogMapping()
{
Id(x => x.BlogId).GeneratedBy.Native();
Map(x => x.Title);
References(x => x.Owner).ColumnName("OwnerId"); //many blogs to one owner
HasMany(x => x.Posts) //one blog has many posts
.Cascade.All()
.AsSet()
.Inverse();
}
//...
public PostMapping()
{
Id(x => x.PostId).GeneratedBy.Native();
Map(x => x.Title);
Map(x => x.Body);
References(x => x.Author).ColumnName("AuthorId"); //many posts have one author
References(x => x.Blog).ColumnName("BlogId"); //many posts have one blog
}
//...
public RegisteredUserMapping()
{
WithTable("User");
Id(x => x.UserId);
Map(x => x.UserName);
Map(x => x.Email);
Map(x => x.Password);
Map(x => x.ShortName);
HasMany(x => x.Posts)
.Inverse()
.Cascade.All()
.AsSet()
.KeyColumnNames.Add("AuthorId");
HasMany(x => x.Blogs)
.Inverse() //The blog object maintains whether or not this is the owner
.Cascade.All() //Cascade deletes, and updates if the owner is deleted or updated.
.AsSet() //We want a set, so that there are unique blogs in the collection.
.KeyColumnNames.Add("OwnerId"); //Column on the Blog row
}
And that’s it for now. As I develop more pieces and parts, I’ll post them here with some code snippets.
{ 2 trackbacks }
{ 0 comments… add one now }