Table of contents
- 1 Using a BelongsTo on the Post class
- 2 Using a HasMany on the Blog class
Adding the relationships
Now it is time to tell ActiveRecord that the classes Blog and Post have a relationship.
As stated before the relationships are one-to-many and many-to-one. For the former we will use the HasManyAttribute, and for the latter we will use the BelongsToAttribute.
Using a BelongsTo on the Post class
We need to include a field and a property on the Post class to hold the Blog instance that - semantically - owns the Post.
[ActiveRecord] public class Post : ActiveRecordBase<Post> { ... private Blog blog; ... public Blog Blog { get { return blog; } set { blog = value; } }
Now add the BelongsToAttribute to the property to tell ActiveRecord that this is a many-to-one relationship. Note that by default the column name is the property name. In our case the column name is BlogId so we need to inform that explicitly to not use the default.
[BelongsTo("BlogId")] public Blog Blog { get { return blog; } set { blog = value; } }
That was very simple. Now let's see the other side of the relation.
Using a HasMany on the Blog class
Seamlessly, we need to include a field and a property on the Blog class to hold all Post instances that - semantically - it has.
using System; using System.Collections.Generic; using Castle.ActiveRecord; [ActiveRecord] public class Blog : ActiveRecordBase<Blog> { ... private IList<Post> posts = new List<Post>(); ... public IList<Post> Posts { get { return posts; } set { posts = value; } }
Add the HasManyAttribute to the property to tell ActiveRecord that this is a one-to-many relationship:
[HasMany] public IList<Post> Posts { get { return posts; } set { posts = value; } }
The HasManyAttribute has a few useful attributes that should be used to have more control over the relationship behavior. For example: this is a bidirectional relationship as both classes have the relations. What side should "control" the relation? Do you intend to create a relation between a Blog and a Post by adding a post to the blog.Posts collection or use the Post.Blog property? I would say that using Post.Blog is preferable in our scenario. The posts collection is informative, and should be read-only. In order to do that, use the Inverse property:
[HasMany(Inverse=true)] public IList<Post> Posts { get { return posts; } set { posts = value; } }
You might also want to define the cascade behavior. What should NHibernate do with the posts when you delete the blog that owns them (for example)? Let's say that it must delete all:
[HasMany(Inverse=true, Cascade=ManyRelationCascadeEnum.AllDeleteOrphan)] public IList<Post> Posts { get { return posts; } set { posts = value; } }
Another thing you should know: Castle ActiveRecord will infer the table and the column that has the relation on the other side. It is capable of doing that because the other side (read the Post class) has a BelongsToAttribute that points back the the Blog. If this is not the case, then you would have to include the table and column information, and it would look like the following:
[HasMany(
Table="Posts", ColumnKey="blogid",
Inverse=true, Cascade=ManyRelationCascadeEnum.AllDeleteOrphan)]
public IList<Post> Posts
{
get { return posts; }
set { posts = value; }
}
Now it is time to start the framework and start playing with what we have coded so far.
Proceed with Initializing the framework.