Using ASP.NET MVC and NHibernate (Part 2)

This is the second part of a series of articles about using NHibernate in an ASP.NET MVC application, you can read the first part here.

 

Put it on the Bag

We will continue by creating a map file for the Post class, this is a little bit different than the Category map file since now we have to represent a many-to-many relationship between Posts and Categories, we accomplish this by using the BAG element in the map file, this is the code of the Post.hbm.xml file:

IMPORTANT: Set the build action of each map file to “Embedded Resource”, this way NHibernate can find the correct file in the  assembly.

Post.hbm.xml

   1: <?xml version="1.0" encoding="utf-8" ?>

   2: <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"

   3:                                     namespace="Core.Domain.Model"

   4:                                     assembly="Core">

   5:  

   6:   <class name="Post" table="Posts" dynamic-update="true">

   7:     <cache usage="read-write"/>

   8:     <id name="Id" column="Id" type="Guid">

   9:       <generator class="guid"/>

  10:     </id>

  11:     <property name="Title" length="100"/>

  12:     <property name="Body"/>

  13:     <property name="CreationDate" type="datetime"/>

  14:     <property name="IsPublic" type="bool"/>

  15:  

  16:     <bag name="Categories" table="PostCategory" lazy="false" >

  17:       <key column="idPost" ></key>

  18:       <many-to-many class="Category" column="idCategory" ></many-to-many>

  19:     </bag>

  20:     

  21:   </class>

  22: </hibernate-mapping>

The bag element is in lines 16 to 19, here is an explanation of what are we doing:

  • The name attribute is the name of the property of the Post class wehere we will store the categories collection.
  • The table attribute is the name of the table in the datbase that joins the Posts and Categories tables.
  • The key.column attribtue is the name of the key column for the Posts.
  • The class attribute is the name of the Category object model class.
  • The many-to-many.column is the name of the key column for the Categories.

These are both the class diagram (left)  and the database diagram (right) so you can better understand this map file:

Mapping PostsCategories Model

As you can see above, we DON’T need to create a class for the PostCategory table.

 

Test it

Now we will test our repositories and make sure we can create posts, this is the complete unit test code for this example:

   1: using System;

   2: using System.Text;

   3: using System.Collections.Generic;

   4: using System.Linq;

   5: using Microsoft.VisualStudio.TestTools.UnitTesting;

   6: using Core.Domain.Model;

   7: using Core.Domain.Repositories;

   8: using Core;

   9:  

  10: namespace NHibernate101.Tests

  11: {

  12:     [TestClass]

  13:     public class RepositoriesTest

  14:     {

  15:         IRepository<Category> categoriesRepository;

  16:         IRepository<Post> postsRepository;

  17:         Post testPost;

  18:         Category testCategory1;

  19:         Category testCategory2;

  20:  

  21:         public RepositoriesTest()

  22:         {

  23:         }

  24:  

  25:         private TestContext testContextInstance;

  26:  

  27:         public TestContext TestContext

  28:         {

  29:             get

  30:             {

  31:                 return testContextInstance;

  32:             }

  33:             set

  34:             {

  35:                 testContextInstance = value;

  36:             }

  37:         }

  38:  

  39:  

  40:         [TestInitialize()]

  41:         public void CreateRepositories()

  42:         {

  43:             categoriesRepository = new CategoryRepository();

  44:             postsRepository = new PostRepository();

  45:         }

  46:  

  47:  

  48:         [TestMethod]

  49:         [DeploymentItem("hibernate.cfg.xml")]

  50:         public void CanCreateCategory()

  51:         {

  52:             testCategory1 = new Category() { Name = "ASP.NET" };

  53:             categoriesRepository.Save(testCategory1);

  54:  

  55:         }

  56:  

  57:         [TestMethod]

  58:         [DeploymentItem("hibernate.cfg.xml")]

  59:         public void CanCreatePost()

  60:         {

  61:             testPost = new Post();

  62:             testPost.Title = "ASP.NET MVC and NHibernate";

  63:             testPost.Body = "In this article I’m going to cover how to install and configure NHibernate and use it in a ASP.NET MVC application.";

  64:             testPost.CreationDate = DateTime.Now;

  65:             testPost.IsPublic = true;

  66:  

  67:             testCategory2 = new Category() { Name= "ASP.NET MVC"};

  68:  

  69:             categoriesRepository.Save(testCategory2);

  70:             testPost.Categories.Add(testCategory2);

  71:  

  72:             postsRepository.Save(testPost);

  73:  

  74:         }

  75:     }

  76: }

 

Run the test and if it passes, we should see the new post and category in our database:

PostAndCategory

 

This are the basics about NHibernate, I encourage you to continue reading about NHibernate in the community site. In the next part of this tutorial we will begin building our ASP.NET MVC application for posts and categories!

 

Download source code of PART 2

Advertisements
Comments
2 Responses to “Using ASP.NET MVC and NHibernate (Part 2)”
Trackbacks
Check out what others are saying...
  1. […] « Using ASP.NET MVC and Ajax to check names availability. Using ASP.NET MVC and NHibernate (Part 2) […]

  2. […] Using ASP.NET MVC and NHibernate (Part 2) November 2009 1 comment 5 […]



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: