看到博客园有很多人用Spring.Net和NHibernate这两个框架,所以自己也想学习一下,这是我写的一个关于NHibernate和Spring.Net结合起来的小例子,比较简单,只实现了一个简单的增加信息的功能。不知道结合的合不合理,希望大家给予批评。
总体思路为:
1、编写实体类Person和映射文件Person.hbm.xml
2、使用NHibernate生成表T_Person
3、编写接口IPersonDao,用PersonDao类实现该接口
4、使用Spring.Net实现增加信息的功能
5、测试工具是用的Resharper里自带的测试工具。
该例子的框架如下:
实现步骤:
1、新建解决方案SpringNet_Lesson1,添加lib文件夹,里面有
这几个dll。
2、添加Domain类库项目,并编写Person类和映射文件,代码如下:
Person.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Domain { public class Person { public virtual Guid Id { get ; set ; } public virtual string Name { get ; set ; } } } |
Person.hbm.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <?xml version= "1.0" encoding= "utf-8" ?> <hibernate-mapping xmlns= "urn:nhibernate-mapping-2.2" assembly= "Domain" namespace = "Domain" > < class name= "Person" table= "T_Person" lazy= "true" > <id name= "Id" type= "Guid" column= "PersonID" > <generator class = "assigned" /> </id> <property name= "Name" type= "string" > <column name= "Name" length= "50" /> </property> </ class > </hibernate-mapping> |
将Person.hbm.xml文件的属性设置为嵌入的资源,
3、添加类库项目Dao,编写IPersonDao接口和PersonDao类:
IPersonDao.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using Domain; namespace Dao { public interface IPersonDao { object Save(Person person); } } |
PersonDao.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using Domain; using NHibernate; using NHibernate.Cfg; namespace Dao { public class PersonDao : IPersonDao { private ISessionFactory sessionFactory; public PersonDao() { var cfg = new Configuration().Configure( "Config/hibernate.cfg.xml" ); sessionFactory = cfg.BuildSessionFactory(); } public object Save(Person person) { using (ISession session = sessionFactory.OpenSession()) { var id = session.Save(person); session.Flush(); return id; } } } } |
4、添加类库Test,用于测试。
在类库下有一个Config文件夹,里面有这么一个文件hibernate.cfg.xml,包含了配置数据库的一些相关信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <?xml version= "1.0" encoding= "utf-8" ?> <hibernate-configuration xmlns= "urn:nhibernate-configuration-2.2" > <session-factory name= "Domain" > <property name= "connection.driver_class" >NHibernate.Driver.SqlClientDriver</property> <property name= "connection.connection_string" > server=(local);database=NHibernateDemo;uid=sa;pwd=123456; </property> <property name= "adonet.batch_size" >10</property> <property name= "show_sql" > true </property> <property name= "dialect" >NHibernate.Dialect.MsSql2008Dialect</property> <property name= "use_outer_join" > true </property> <property name= "command_timeout" >60</property> <property name= "hbm2ddl.auto" >update</property> <property name= "query.substitutions" > true 1, false 0, yes 'Y' , no 'N' </property> <property name= "proxyfactory.factory_class" >NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property> <mapping assembly= "Domain" /> </session-factory> </hibernate-configuration> |
添加app.config文件,在这个文件里包含了需要注入的对象,这里只注入了PersonDao:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <?xml version= "1.0" encoding= "utf-8" ?> <configuration> <configSections> <sectionGroup name= "spring" > <section name= "context" type= "Spring.Context.Support.ContextHandler, Spring.Core" /> <section name= "objects" type= "Spring.Context.Support.DefaultSectionHandler, Spring.Core" /> </sectionGroup> </configSections> <spring> <context> <resource uri= "config://spring/objects" /> </context> <objects xmlns= "http://www.springframework.net" > <description>一个简单的控制反转例子</description> < object id= "PersonDao" type= "Dao.PersonDao, Dao" /> </objects> </spring> </configuration> |
再编写PersonInit.cs类,用于生成表结构
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using NHibernate; using NHibernate.Cfg; using NUnit.Framework; namespace Test { [TestFixture] public class PersonInit { /// <summary> /// In order to generate table T_Person in database NHibernateDemo. /// </summary> [Test] public void Init() { var cfg = new Configuration().Configure( "Config/hibernate.cfg.xml" ); using (ISessionFactory sessionFactory = cfg.BuildSessionFactory()) { } } } } |
生成的表结构T_Person为:
接着编写DomainTest.cs类,用于添加数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using Dao; using Domain; using NHibernate; using NUnit.Framework; using Spring.Context; using Spring.Context.Support; namespace Test { [TestFixture] class DomainTest { private IPersonDao personDao; //[SetUp] //public void Init() //{ // personDao = DaoFactory.DataAccess.CreatePersonDao(); //} [Test] public void Save() { var person = new Person { Id = Guid.NewGuid(), Name = "李四" }; IApplicationContext context = ContextRegistry.GetContext(); personDao = context.GetObject( "PersonDao" ) as IPersonDao; if (personDao != null ) { var id = personDao.Save(person); Assert.NotNull(id); } } } } |
运行这个测试,可以看到数据已添加进去。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· [AI/GPT/综述] AI Agent的设计模式综述