摘要

在实际的项目中,经常是将NHibernate的实体关系映射类做成独立的工程(assembly dll),只对外提供Session调用的接口。这个程序集作为数据访问层,可以被上面的多个工程(ASP.Net、Windows Form、Windows Serviice等)调用。

这篇文章介绍如何设计NHibernate数据访问层的工程,以及如何架构数据访问层和上面的应用层的关系。

本文章的所有代码可以到第一个NHibernate程序下载。

步骤:

1)为了后面文章的程序演示方便,删除Customer表,新建主键类型是int类型,主键生成方法是IDENTITY的Customer表。

CREATE TABLE [dbo].[Customer](
[Id] [int] IDENTITY(1,1) NOT NULL,
[FirstName] [nvarchar](10) NOT NULL,
[LastName] [nvarchar](10) NOT NULL,
[Points] [int] NULL,
[HasGoldStatus] [bit] NULL,
[MemberSince] [date] NULL,
[CreditRating] [nchar](20) NULL,
[AverageRating] [decimal](18, 4) NULL,
[Street] [nvarchar](100) NULL,
[City] [nvarchar](100) NULL,
[Province] [nvarchar](100) NULL,
[Country] [nvarchar](100) NULL,
CONSTRAINT [PK_Customer] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

2)添加Class Library:NHibernateDemoAPP.XML.Entities。删除Class1.cs文件。

这里使用XML映射,因此这里用NHibernateDemoAPP.XML.Entities作为工程名。实际项目的工程名称应该没有"XML"。

3)在NHibernateDemoAPP.XML.Entities工程里添加文件夹Domain和hbm。也可以创建名称为Mapping的文件夹,替代这里的hbm文件夹,看个人喜好。

4)将NHibernateDemoApp工程的文件Address.cs、Customer.cs移动到Domain文件夹。将文件Customer.hbm.xml移动到hbm文件夹。在NHibernateDemoApp工程里删除文件Address.cs、Customer.cs和Customer.hbm.xml。

5)修改NHibernateDemoApp工程的文件hibernate.hbm.xml。修改mapping节点的assembly属性。

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.connection_string_name">default</property>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
<mapping assembly="NHibernateDemoAPP.XML.Entities"/>
</session-factory>
</hibernate-configuration>

6)修改Customer.cs的namespace。将Id属性改成int。

 using System;

 namespace NHibernateDemoAPP.XML.Entities.Domain
{
public class Customer
{
public virtual int Id { get; set; }
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
public virtual double AverageRating { get; set; }
public virtual int Points { get; set; }
public virtual bool HasGoldStatus { get; set; }
public virtual DateTime MemberSince { get; set; }
public virtual CustomerCreditRating CreditRating { get; set; }
public virtual Address Address { get; set; }
} public enum CustomerCreditRating
{
Excellent, VeryVeryGood, VeryGood, Good, Neutral, Poor, Terrible
}
}

7)修改Customer.hbm.xml文件。修改根节点hibernate-mapping的assembly属性和namespace属性。

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernateDemoAPP.XML.Entities" namespace="NHibernateDemoAPP.XML.Entities.Domain">
<class name="Customer" table="Customer">
<id name="Id">
<generator class="native"/>
</id>
<property name="FirstName" not-null="true"/>
<property name="LastName" not-null ="true"/>
<property name="AverageRating"/>
<property name="Points"/>
<property name="HasGoldStatus"/>
<property name="MemberSince"/>
<property name="CreditRating" type="CustomerCreditRating"/>
<component name="Address">
<property name="Street"/>
<property name="City"/>
<property name="Province"/>
<property name="Country"/>
</component>
</class>
</hibernate-mapping>

8)为工程NHibernateDemoApp添加工程NHibernateDemoAPP.XML.Entities的引用。

9)修改Program.cs文件。

 using NHibernate;
using NHibernate.Cfg;
using NHibernateDemoAPP.XML.Entities.Domain;
using System;
using System.Collections.Generic; namespace NHibernateDemoApp
{
class Program
{
private static ISessionFactory _sessionFactory; private static ISession _session; public static ISessionFactory SessionFactory
{
get
{
if (_sessionFactory == null)
{
var cfg = new Configuration();
cfg.Configure();
_sessionFactory = cfg.BuildSessionFactory();
}
return _sessionFactory;
}
} public static ISession Session
{
get
{
if (_session == null)
{
_session = SessionFactory.OpenSession();
}
return _session;
}
} static void Main(string[] args)
{
HibernatingRhinos.Profiler.Appender.NHibernate.NHibernateProfiler.Initialize(); using (var session = SessionFactory.OpenSession())
{
var customer = CreateCustomer();
object custoemrId = session.Save(customer);
session.Flush(); customer = session.Get<Customer>(custoemrId);
Console.WriteLine(customer.LastName);
} Console.WriteLine("Completed");
Console.ReadLine();
} private static Customer CreateCustomer()
{
var customer = new Customer
{
FirstName = "Daniel",
LastName = "Tang",
Points = ,
HasGoldStatus = true,
MemberSince = new DateTime(, , ),
CreditRating = CustomerCreditRating.Good,
AverageRating = 42.42424242,
Address = new Address
{
Street = "123 Somewhere Avenue",
City = "Nowhere",
Province = "Alberta",
Country = "Canada"
}
}; return customer;
} private static void UpdateInTransaction(Customer customer)
{
using (var transaction = Session.BeginTransaction())
{
try
{
Session.Update(customer);
transaction.Commit();
}
catch (HibernateException e)
{
transaction.Rollback();
throw e;
}
}
} private static IList<Customer> GetAll()
{
using (var session = SessionFactory.OpenSession())
{
IList<Customer> list = session.CreateCriteria<Customer>().List<Customer>();
return list;
}
} private static Customer GetById(Guid id)
{
using (var session = SessionFactory.OpenSession())
{
Customer customer = session.Get<Customer>(id);
return customer;
}
} private static Customer LoadById(Guid id)
{
using (var session = SessionFactory.OpenSession())
{
Customer customer = session.Load<Customer>(id);
return customer;
}
} private static int Insert(Customer customer)
{
using (var session = SessionFactory.OpenSession())
{
var identifier = session.Save(customer);
session.Flush();
return Convert.ToInt32(identifier);
}
} private static void Update(Customer customer)
{
using (var session = SessionFactory.OpenSession())
{
session.SaveOrUpdate(customer);
session.Flush();
}
} private static void Delete(int id)
{
using (var session = SessionFactory.OpenSession())
{
var customer = session.Get<Customer>(id);
session.Delete(customer);
session.Flush();
}
}
}
}

9)执行程序,将一条Customer对象插入到数据库记录中。

下一篇文章介绍如何在应用层(ASP.Net MVC, ASP.Net Web, Windows Form, Windows Service)中管理SessionFactory对象和Session对象。

NHibernate系列文章十六:使用程序集管理NHibernate项目(附程序下载)的更多相关文章

  1. NHibernate系列文章十:NHibernate对象二级缓存下

    摘要 上一节对NHibernate二级缓存做了简单介绍,NHibernate二级缓存是由SessionFactory管理的,所有Session共享.这一节介绍二级缓存其他两个方面:二级缓存查询和二级缓 ...

  2. NHibernate系列文章十九:NHibernate关系之多对多关系(附程序下载)

    摘要 NHibernate的多对多关系映射由many-to-many定义. 从这里下载本文的代码NHibernate Demo 1.修改数据库 添加Product表 添加ProductOrder表 数 ...

  3. NHibernate系列文章十五:NHibernate组件

    摘要 前面文章介绍了NHibernate对简单.net数据类型的映射对照表.NHibernate也可以映射复杂数据类型,这里介绍通过组件映射NHibernate值对象. 1. NHibernate引用 ...

  4. NHibernate系列文章十八:NHibernate关系之一对多(附程序下载)

    摘要 这篇文章介绍NHibernate最实用的内容:关系映射. NHibernate的关系映射方式有三种: Set:无序对象集合,集合中每一个元素不能重复. List:有序对象集合,集合中的元素可以重 ...

  5. NHibernate系列文章十二:Load/Get方法

    摘要 NHibernate提供两个方法按主键值查找对象:Load/Get. 1. Load/Get方法的区别 Load: Load方法可以对查询进行优化. Load方法实际得到一proxy对象,并不立 ...

  6. NHibernate系列文章十四:NHibernate事务

    摘要 NHibernate实现事务机制非常简单,调用ISession.BeginTransaction()开启一个事务对象ITransaction,使用ITransaction.Commit()提交事 ...

  7. Web 前端开发人员和设计师必读精华文章【系列二十六】

    <Web 前端开发精华文章推荐>2014年第5期(总第26期)和大家见面了.梦想天空博客关注 前端开发 技术,分享各类能够提升网站用户体验的优秀 jQuery 插件,展示前沿的 HTML5 ...

  8. NHibernate系列文章目录

    第一章:NHibernate基础 NHibernate介绍 第一个NHibernate工程 简单的增删改查询 运行时监控 NHibernate配置 数据类型映射 Get/Load方法 NHiberna ...

  9. linux基础-第十六单元 yum管理RPM包

    第十六单元 yum管理RPM包 yum的功能 本地yum配置 光盘挂载和镜像挂载 本地yum配置 网络yum配置 网络yum配置 Yum命令的使用 使用yum安装软件 使用yum删除软件 安装组件 删 ...

随机推荐

  1. SQL取行最大值

    create table T(A decimal(10,1), B decimal(10,1), C decimal(10,1), D decimal(10,1), E decimal(10,1)) ...

  2. windows核心编程---第四章 进程

    上一章介绍了内核对象,这一节开始就要不断接触各种内核对象了.首先要给大家介绍的是进程内核对象.进程大家都不陌生,它是资源和分配的基本单位,而进程内核对象就是与进程相关联的一个数据结构.操作系统内核通过 ...

  3. windows核心编程---第二章 字符和字符串处理

        使用vc编程时项目-->属性-->常规栏下我们可以设置项目字符集合,它可以是ANSI(多字节)字符集,也可以是unicode字符集.一般情况下说Unicode都是指UTF-16.也 ...

  4. 1、webservice的简单使用

    1.新建一个web端项目 2.点击添加项,选择web服务 3.在已经建好的项目中写一个方法 4.发布(发布方法选文件系统,web需要管理员权限) 生成文件夹: 5.配置IIS(略) 6.调用webse ...

  5. net_device 结构体分析

    /* * The DEVICE structure. * Actually, this whole structure is a big mistake. It mixes I/O * data wi ...

  6. viewPager动态加载listview数据

    废话不多说,先上效果图.(代码见附件) 代码是修改自某大神的,我做了很多修改,之前只能向右滑动,现在可以左右无限滑动,只要数据没加载完就可以一直滑动.过程不算复杂,代码主要的地方都有注释. 附件dem ...

  7. PAT (Basic Level) Practise:1023. 组个最小数

    [题目链接] 给定数字0-9各若干个.你可以以任意顺序排列这些数字,但必须全部使用.目标是使得最后得到的数尽可能小(注意0不能做首位).例如:给定两个0,两个1,三个5,一个8,我们得到的最小的数就是 ...

  8. my Js

    1. __doPostBack是.net自动生成的(当页面中有LinkButton.DropDownList(AutoPostBack)等时:Button和ImageButton不会生成它,也不会调用 ...

  9. Android——数据存储(课堂代码整理:SharedPreferences存储和手机内部文件存储)

    layout文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:an ...

  10. 论文笔记之: Hierarchical Convolutional Features for Visual Tracking

    Hierarchical Convolutional Features for Visual Tracking  ICCV 2015 摘要:跟卢湖川的那个文章一样,本文也是利用深度学习各个 layer ...