views:

97

answers:

1

Trying to wrap my head around nHibernate, curiuos how this scenerio would be handled:

Post (postID, title, content, dateCreated)
Category (categoryID, name, postCount)

post_to_categories (postID, categoryID)

If I create a Post, it should insert into Post, insert into post_to_categories and update the postCount in Category.

I am planning on using the iRepository pattern also.

How would this be done with nHibernate? (mapping file example?)

A: 

The "hard" part of this seems to be updating the post count in the category table... but why update it? You can just define the value like this (inside the mapping file for Category):

<property name="PostCount" formula="select count(*) from Posts p where p.Id = Id" />

If your post-to-category is many to many, it should be possible to extend the above to support that as well.

mookid8000