一,摘要
这是ASP.NET MVC中使用Nhibernate的第二部分,你也可以查看第一部分
一,多对多关联
我们将继续创建我们的Post类的映射文件,相对于Category将增加一点难度,应为我们不得不去呈现一个多对对的关系,为了达到这个目的,我们在映射文件中使用Bagyu元素,下面是Post.hbm.xml 文件的代码:
1: xml version="1.0" encoding="utf-8" ?><!--CRLF-->
2: hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"<!--CRLF-->
3: namespace="Core.Domain.Model"<!--CRLF-->
4: assembly="Core"><!--CRLF-->
5:
<!--CRLF-->
6: class name="Post" table="Posts" dynamic-update="true"><!--CRLF-->
7: cache usage="read-write"/><!--CRLF-->
8: id name="Id" column="Id" type="Guid"><!--CRLF-->
9: generator class="guid"/><!--CRLF-->
10: id><!--CRLF-->
11: property name="Title" length="100"/><!--CRLF-->
12: property name="Body"/><!--CRLF-->
13: property name="CreationDate" type="datetime"/><!--CRLF-->
14: property name="IsPublic" type="bool"/><!--CRLF-->
15:
<!--CRLF-->
16: bag name="Categories" table="PostCategory" lazy="false" ><!--CRLF-->
17: key column="idPost" >key><!--CRLF-->
18: many-to-many class="Category" column="idCategory" >many-to-many><!--CRLF-->
19: bag><!--CRLF-->
20:
<!--CRLF-->
21: class><!--CRLF-->
22: hibernate-mapping><!--CRLF-->
bag元素包含在16-19行代码中,下面解释下上面的代码:
1.name属性是Post类的属性名,我们用来存储categories集合
2.table属性是数据库中的一个表,用来连接Post和Categories表
3.key中的column属性是Post的主键列
4.class属性是category模型的类名
5.many-to-many中的column属性是Categories的主键列
为了更好的理解这个映射文件,下面提供类图表和数据库图标:
从上面看到,我们不需创建PostCategory表的类
二,测试
下面提供完整的单元测试代码:
1: using System;<!--CRLF-->
2: using System.Text;<!--CRLF-->
3: using System.Collections.Generic;<!--CRLF-->
4: using System.Linq;<!--CRLF-->
5: using Microsoft.VisualStudio.TestTools.UnitTesting;<!--CRLF-->
6: using Core.Domain.Model;<!--CRLF-->
7: using Core.Domain.Repositories;<!--CRLF-->
8: using Core;<!--CRLF-->
9: amespace NHibernate101.Tests
<!--CRLF-->
10:
<!--CRLF-->
11: [TestClass]
<!--CRLF-->
12: public class RepositoriesTest<!--CRLF-->
13: {
<!--CRLF-->
14: IRepository<category> categoriesRepository;</category>
<!--CRLF-->
15: IRepository<post> postsRepository;</post>
<!--CRLF-->
16: Post testPost;
<!--CRLF-->
17: Category testCategory1;
<!--CRLF-->
18: Category testCategory2;
<!--CRLF-->
19:
<!--CRLF-->
20: public RepositoriesTest()<!--CRLF-->
21: {
<!--CRLF-->
22: }
<!--CRLF-->
23: private TestContext testContextInstance;<!--CRLF-->
24: public TestContext TestContext<!--CRLF-->
25: {
<!--CRLF-->
26: get
<!--CRLF-->
27: {
<!--CRLF-->
28: return testContextInstance;<!--CRLF-->
29: }
<!--CRLF-->
30: set
<!--CRLF-->
31: {
<!--CRLF-->
32: testContextInstance = value;<!--CRLF-->
33: }
<!--CRLF-->
34: }
<!--CRLF-->
35: [TestInitialize()]
<!--CRLF-->
36: public void CreateRepositories()<!--CRLF-->
37: {
<!--CRLF-->
38: categoriesRepository = new CategoryRepository();<!--CRLF-->
39: postsRepository = new PostRepository();<!--CRLF-->
40: }
<!--CRLF-->
41: [TestMethod]
<!--CRLF-->
42: [DeploymentItem("hibernate.cfg.xml")]<!--CRLF-->
43: public void CanCreateCategory()<!--CRLF-->
44: {
<!--CRLF-->
45: testCategory1 = new Category() { Name = "ASP.NET" };<!--CRLF-->
46: categoriesRepository.Save(testCategory1);
<!--CRLF-->
47:
<!--CRLF-->
48: }
<!--CRLF-->
49: [TestMethod]
<!--CRLF-->
50: [DeploymentItem("hibernate.cfg.xml")]<!--CRLF-->
51: public void CanCreatePost()<!--CRLF-->
52: {
<!--CRLF-->
53: testPost = new Post();<!--CRLF-->
54: testPost.Title = "ASP.NET MVC and NHibernate";<!--CRLF-->
55: 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.";<!--CRLF-->
56: testPost.CreationDate = DateTime.Now;
<!--CRLF-->
57: testPost.IsPublic = true;<!--CRLF-->
58: testCategory2 = new Category() { Name= "ASP.NET MVC"};<!--CRLF-->
59: categoriesRepository.Save(testCategory2);
<!--CRLF-->
60: testPost.Categories.Add(testCategory2);
<!--CRLF-->
61: postsRepository.Save(testPost);
<!--CRLF-->
62: }
<!--CRLF-->
63: }
<!--CRLF-->
如果上面的测试通过,我们将在数据库中看到新的post和categoty
随下的文章中我们将继续探索!