当前位置: 首页 > news >正文

.NET开源项目介绍及资源推荐:数据持久层

.NET平台下,关于数据持久层框架非常多,本文主要对如下几种做简要的介绍并推荐一些学习的资源:

1 NHibernate

2 NBear

3 Castle ActiveRecord

4 iBATIS.NET

5 DAAB

附加介绍:DLinq

 

一.NHibernate

提起NHibernate,相信大家都不陌生,NHibernate来源于非常优秀的基于JavaHibernate关系型持久化工具,它从数据库底层来持久化.Net对象到关系型数据库,NHibernate为我们完成这一切,而不用自己写SQL语句去操作数据库对象,所写的代码仅仅和对象关联,NHibernat自动产生SQL语句,并确保对象提交到正确的表和字段中去.大量减少开发时人工使用SQLADO.NET处理数据的时间. NHibernate可以帮助消除或者包装那些针对特定数据库的SQL代码,并且把结果集从表格的表示形式转换到一系列的对象去。NHibernate采用XML文件配置的方式,每一个实体类都会对应一个映射文件,如下面的例子:

None.gif public   class  User
ExpandedBlockStart.gif
{
InBlock.gif    
public User()
ExpandedSubBlockStart.gif    
{
ExpandedSubBlockEnd.gif    }

InBlock.gif    
private string id;
InBlock.gif    
private string userName;
InBlock.gif    
private string password;
InBlock.gif    
private string emailAddress;
InBlock.gif
private DateTime lastLogon;
InBlock.gif    
public string Id
ExpandedSubBlockStart.gif    
{
ExpandedSubBlockStart.gif        
get return id; }
ExpandedSubBlockStart.gif        
set { id = value; }
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public string UserName
ExpandedSubBlockStart.gif    
{
ExpandedSubBlockStart.gif        
get return userName; }
ExpandedSubBlockStart.gif        
set { userName = value; }
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public string Password
ExpandedSubBlockStart.gif    
{
ExpandedSubBlockStart.gif        
get return password; }
ExpandedSubBlockStart.gif        
set { password = value; }
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public string EmailAddress
ExpandedSubBlockStart.gif    
{
ExpandedSubBlockStart.gif        
get return emailAddress; }
ExpandedSubBlockStart.gif        
set { emailAddress = value; }
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public DateTime LastLogon
ExpandedSubBlockStart.gif    
{
ExpandedSubBlockStart.gif        
get return lastLogon; }
ExpandedSubBlockStart.gif        
set { lastLogon = value; }
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}
它对应的 .hbm.xml文件如下:
None.gif <? xml version="1.0" encoding="utf-8"  ?>
None.gif
None.gif
< hibernate-mapping  xmlns ="urn:nhibernate-mapping-2.0" >
None.gif
None.gif  
< class  name ="NHibernateWebDemo.Model.User, NHibernateWebDemo.Model"  table ="users" >
None.gif
None.gif    
< id  name ="Id"  column ="LogonId"  type ="String"  length ="20" >
None.gif
None.gif      
< generator  class ="assigned"   />
None.gif
None.gif    
</ id >
None.gif
None.gif    
< property  name ="UserName"  column = "Name"  type ="String"  length ="40" />
None.gif
None.gif    
< property  name ="Password"  type ="String"  length ="20" />
None.gif
None.gif    
< property  name ="EmailAddress"  type ="String"  length ="40" />
None.gif
None.gif    
< property  name ="LastLogon"  type ="DateTime" />
None.gif
None.gif  
</ class >
None.gif
None.gif
</ hibernate-mapping >
官方主页: [url]http://www.nhibernate.org/[/url]

学习资源

园子里首推DDLBlog[url]http://www.cnblogs.com/renrenqq/[/url],有NHibernate文档的中文翻译以及DLL写的一些非常优秀的NHibernate文章。

大名鼎鼎的张老三:[url]http://blog.csdn.net/billy_zh/category/22383.aspx[/url]

Aero Nhibernate学习手记系列:[url]http://www.cnblogs.com/chwkai/category/32514.html[/url]

无心之柳的Blog也非常值得推荐:[url]http://www.cnblogs.com/9527/[/url]

博客园O/R Mapping团队:[url]http://www.cnblogs.com/team/ORMapping.html[/url]

 

二.NBear

园子里Teddy开发的NBear大家都非常熟悉,现在已经发布了3.0正式版。NBear包含的组件不仅仅是数据持久层,还包含了IOC,分布式组件和Web组件。看一下Teddy对于NBear的介绍:

NBear 的核心包括一个泛型、强类型的的ORM数据持久化接口、一组相关的Entity相关组件、高性能分布式组件、Web组件,因此:

1 NBear最适合开发各类基于ASP.NET 2.0,对性能要求较高的Web程序。NBear.Web组件提供了许多加速Web开发的组件,将使您基于标准 ASP.NET方式的开发效率大大提高;同时,简单易用、性能突出的泛型持久化支持,则将使您能够将更多注意力集中到业务开发,同时也不会有传统ORM持久化框架的性能问题和繁琐配置需要(NBear几乎不需手动配置,性能则接近DAAB)。

2 、基于MQ.Net Remoting的高性能分布式组件,将使您开发和维护分布式程序更加容易。一个基于NBear.IoC模块的开发的应用程序甚至无需重新编译就能部属为真正的负载均衡的分布式程序。

3 、对于桌面应用程序,NBear同样是一个几乎没有什么学习曲线(多少人会为写一个小小的日历程序而仔细研究透彻Hibernate的参考手册?)、实用高效的数据持久化方案。

4 、随着NBearV3带来的全面的ORM支持、更详细的文档和教程,和全面的代码生成工具,NBear也已经可以被用于企业级程序开发。

官方首页:[url]http://teddyma.cnblogs.com/articles/Ilungasoft_Framework.html[/url]

学习资源

学习资源当然首推Teddy的个人Blog[url]http://www.cnblogs.com/teddyma/[/url]

博客园NB团队:[url]http://nbteam.cnblogs.com/[/url]

 

三.Castle ActiveRecord

ActiveRecord Castle中的一个子项目,现在的版本是RC1。它同样是一个非常优秀的持久层框架,在底层封装了NHibernate,改用Attribute来代替配置文件,这样就不用再像NHibernate那样去编写复杂的配置文件。如下代码片断所示:
None.gif [ActiveRecord( " Users " )]
None.gif
public   class  User : ActiveRecordBase
ExpandedBlockStart.gif
{
InBlock.gif    
private int _id;
InBlock.gif    
private string _name;
InBlock.gif    
private string _password;
InBlock.gif    
private string _emailAddress;
InBlock.gif    
private DateTime _lastLogon;
InBlock.gif    [PrimaryKey(PrimaryKeyType.Identity, 
"LogonID")]
InBlock.gif    
public int Id
ExpandedSubBlockStart.gif    
{
ExpandedSubBlockStart.gif        
get return _id; }
ExpandedSubBlockStart.gif        
set { _id = value; }
ExpandedSubBlockEnd.gif    }

InBlock.gif    [Property(
"LogonName")]
InBlock.gif    
public string Name
ExpandedSubBlockStart.gif    
{
ExpandedSubBlockStart.gif        
get return _name; }
ExpandedSubBlockStart.gif        
set { _name = value; }
ExpandedSubBlockEnd.gif    }

InBlock.gif    [Property(
"Password")]
InBlock.gif    
public string Password
ExpandedSubBlockStart.gif    
{
ExpandedSubBlockStart.gif        
get return _password; }
ExpandedSubBlockStart.gif        
set { _password = value; }
ExpandedSubBlockEnd.gif    }

InBlock.gif    [Property(
"EmailAddress")]
InBlock.gif    
public string Address
ExpandedSubBlockStart.gif    
{
ExpandedSubBlockStart.gif        
get return _emailAddress; }
ExpandedSubBlockStart.gif        
set { _emailAddress = value; }
ExpandedSubBlockEnd.gif    }

InBlock.gif    [Property(
"LastLogon")]
InBlock.gif    
public DateTime LastLogon
ExpandedSubBlockStart.gif    
{
ExpandedSubBlockStart.gif        
get return _lastLogon; }
ExpandedSubBlockStart.gif        
set { _lastLogon = value; }
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}
官方主页: [url]http://www.castleproject.org[/url]

学习资源

官方文档:[url]http://www.castleproject.org/activerecord/documentation/v1rc1/index.html[/url]

叶子的家:[url]http://wj.cnblogs.com/[/url]

TerryLee Castle开发系列:

[url]http://terrylee.cnblogs.com/archive/2006/04/28/castl_ioc_article.html[/url]

Castle 项目成员之一ayendeBlog[url]http://www.ayende.com/Blog/[/url]

 

四.iBATIS.NET

iBATIS.NET 分为DataMapperDataAccess两部分,应该说DataMapper是这个框架的核心,DataMapper使用XML文件来实现从实体到SQL statements的映射,学习起来非常简单,是用DataMapper后,我们可以自由的使用SQL语句或者存储过程;DataAccess允许我们通过一个简单的接口来操作数据,而不必了解底层实现的细节。如下代码片断:
None.gif [Serializable]
None.gif
public   class  Person
ExpandedBlockStart.gif
{
InBlock.gif    
private int id;
InBlock.gif    
private string firstName;
InBlock.gif    
private string lastName;
InBlock.gif    
private DateTime? birthDate;
InBlock.gif    
private double? weightInKilograms;
InBlock.gif    
private double? heightInMeters;
ExpandedSubBlockStart.gif    
public Person() { }
InBlock.gif    
public int Id
ExpandedSubBlockStart.gif    
{
ExpandedSubBlockStart.gif        
get return id; }
ExpandedSubBlockStart.gif        
set { id = value; }
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public string FirstName
ExpandedSubBlockStart.gif    
{
ExpandedSubBlockStart.gif        
get return firstName; }
ExpandedSubBlockStart.gif        
set { firstName = value; }
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public string LastName
ExpandedSubBlockStart.gif    
{
ExpandedSubBlockStart.gif        
get return lastName; }
ExpandedSubBlockStart.gif        
set { lastName = value; }
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public DateTime? BirthDate
ExpandedSubBlockStart.gif    
{
ExpandedSubBlockStart.gif        
get return birthDate; }
ExpandedSubBlockStart.gif        
set { birthDate = value; }
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public double? WeightInKilograms
ExpandedSubBlockStart.gif    
{
ExpandedSubBlockStart.gif        
get return weightInKilograms; }
ExpandedSubBlockStart.gif        
set { weightInKilograms = value; }
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public double? HeightInMeters
ExpandedSubBlockStart.gif    
{
ExpandedSubBlockStart.gif        
get return heightInMeters; }
ExpandedSubBlockStart.gif        
set { heightInMeters = value; }
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}
映射文件如下:

None.gif <? xml version="1.0" encoding="utf-8"  ?>
None.gif
< sqlMap  namespace ="Person"  xmlns ="http://ibatis.apache.org/mapping"  
None.gifxmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"   >
None.gif  
< alias >
None.gif    
< typeAlias  alias ="Person"  type ="IBatisNetDemo.Domain.Person,IBatisNetDemo"   />
None.gif  
</ alias >
None.gif
None.gif  
< resultMaps >
None.gif    
< resultMap  id ="SelectAllResult"  class ="Person" >
None.gif      
< result  property ="Id"  column ="PER_ID"   />
None.gif      
< result  property ="FirstName"  column ="PER_FIRST_NAME"   />
None.gif      
< result  property ="LastName"  column ="PER_LAST_NAME"   />
None.gif      
< result  property ="BirthDate"  column ="PER_BIRTH_DATE"   />
None.gif      
< result  property ="WeightInKilograms"  column ="PER_WEIGHT_KG"   />
None.gif      
< result  property ="HeightInMeters"  column ="PER_HEIGHT_M"   />
None.gif    
</ resultMap >
None.gif  
</ resultMaps >
None.gif
None.gif  
< statements >
None.gif    
< select  id ="SelectAllPerson"  resultMap ="SelectAllResult" >
None.gif      select
None.gif      PER_ID,
None.gif      PER_FIRST_NAME,
None.gif      PER_LAST_NAME,
None.gif      PER_BIRTH_DATE,
None.gif      PER_WEIGHT_KG,
None.gif      PER_HEIGHT_M
None.gif      from PERSON
None.gif    
</ select >
None.gif
None.gif    
< select  id ="SelectByPersonId"  resultClass ="Person"  parameterClass ="int" >
None.gif      select
None.gif      PER_ID,
None.gif      PER_FIRST_NAME,
None.gif      PER_LAST_NAME,
None.gif      PER_BIRTH_DATE,
None.gif      PER_WEIGHT_KG,
None.gif      PER_HEIGHT_M
None.gif      from PERSON
None.gif      where PER_ID = #value#
None.gif    
</ select >
None.gif    
None.gif    
< insert  id ="InsertPerson"   parameterclass ="Person"   >
None.gif      
< selectKey  property ="Id"  type ="post"  resultClass ="int" >
None.gif        ${selectKey}
None.gif      
</ selectKey >
None.gif      insert into Person
None.gif      ( PER_FIRST_NAME,
None.gif      PER_LAST_NAME,
None.gif      PER_BIRTH_DATE,
None.gif      PER_WEIGHT_KG,
None.gif      PER_HEIGHT_M)
None.gif      values
None.gif      (#FirstName#,#LastName#,#BirthDate#, #WeightInKilograms#, #HeightInMeters#)
None.gif    
</ insert >
None.gif    
None.gif    
< update  id ="UpdatePerson"  parameterclass ="Person" >
None.gif      
<![CDATA[  update Person set
None.gif      PER_FIRST_NAME =#FirstName#,
None.gif      PER_LAST_NAME =#LastName#,
None.gif      PER_BIRTH_DATE =#BirthDate#,
None.gif      PER_WEIGHT_KG=#WeightInKilograms#,
None.gif      PER_HEIGHT_M=#HeightInMeters#
None.gif      where
None.gif      PER_ID = #Id# 
]]>
None.gif    
</ update >
None.gif
None.gif    
< delete  id ="DeletePerson"  parameterclass ="Person" >
None.gif      delete from Person
None.gif      where
None.gif      PER_ID = #Id#
None.gif    
</ delete >
None.gif  
</ statements >
None.gif
</ sqlMap >
官方主页: [url]http://ibatis.apache.org/[/url]

 
学习资源

官方文档: [url]http://opensource.atlassian.com/confluence/oss/display/IBATIS/Quick+Start+Guide[/url]

善友的iBATIS.NET开发指南系列: [url]http://www.cnblogs.com/shanyou/archive/2006/04/29/388610.html[/url]


五.DAAB

DAAB 是微软Enterprise Library中的一个应用程序块,能够帮助我们实现通用的数据访问,所以也把它列在这里介绍一下。DAAB使应用程序中的数据访问在不知道具体的数据库系统的情况下进行,相信很多朋友对DAAB都很熟性并且已经在项目中使用,就不多介绍了,看一个简单的代码片断:
None.gif public   string  GetCustomerList()
ExpandedBlockStart.gif
{
InBlock.gif
// 创建Database对象
InBlock.gif
Database db = DatabaseFactory.CreateDatabase();
InBlock.gif
// 使用SQL语句创建DbCommand对象
InBlock.gif
string sqlCommand = "Select CustomerID, Name, Address, City, Country, PostalCode " +
InBlock.gif    
"From Customers";
InBlock.gifDbCommand dbCommand 
= db.GetSqlStringCommand(sqlCommand);
InBlock.gifStringBuilder readerData 
= new StringBuilder();
InBlock.gif
// 调用ExecuteReader方法
InBlock.gif
using (IDataReader dataReader = db.ExecuteReader(dbCommand))
ExpandedSubBlockStart.gif
{
InBlock.gif    
while (dataReader.Read())
ExpandedSubBlockStart.gif    
{
InBlock.gif        
// Get the value of the 'Name' column in the DataReader
InBlock.gif
        readerData.Append(dataReader["Name"]);
InBlock.gif        readerData.Append(Environment.NewLine);
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif}

InBlock.gif
return readerData.ToString();
ExpandedBlockEnd.gif}
官方主页: [url]http://msdn.microsoft.com/practices/[/url]

学习资源

企业的帮助文档和Hands On Lab

TerryLee Enterprise Library系列:[url]http://www.cnblogs.com/Terrylee/archive/2006/08/01/Enterprise_Library.html[/url]

 

附加介绍: DLinq

DLinq 虽然不能算是开源框架,但是说到数据持久,还是提一下比较好,DLinq是微软下一代数据库集成查询语言,在这之前微软曾经尝试过ObjectSpace,最后是不了了之。DLinq实现的方式有点类似于前面说过的ActiveRecord,不支持使用外部的XML配置文件,而是使用了Attribute的方式,如下代码片断所示:
None.gif [Table(Name = " Customers " )]
None.gif
None.gif
public   class  Customer
None.gif
ExpandedBlockStart.gif
{
InBlock.gif
InBlock.gif    [Column(Id
=true)]
InBlock.gif
InBlock.gif    
public string CustomerID;
InBlock.gif
InBlock.gif    [Column]
InBlock.gif
InBlock.gif    
public string City;
InBlock.gif
ExpandedBlockEnd.gif}
官方主页: [url]http://msdn.microsoft.com/netframework/future/linq/[/url]

学习资源

下载LINQ May CTP版:[url]http://msdn.microsoft.com/data/ref/linq/[/url]

ScottGu Blog[url]http://weblogs.asp.net/scottgu/default.aspx[/url]

 

最后值得一提的是,微软又推出个Ado.net vNext,使用映射文件来配置,更加类似于NHibernate。关于持久层框架,还有很多,这里就不再介绍了,如Grove等。










本文转自lihuijun51CTO博客,原文链接:  http://blog.51cto.com/terrylee/67592 ,如需转载请自行联系原作者

相关文章:

  • Wireshark网络抓包(二)——过滤器
  • Qt之JSON生成与解析1
  • command for cut
  • ubuntu 11.10下载和编译Android源码
  • 【移动开发】Android应用开发者应该知道的东西
  • Android开发之旅:组件生命周期(二)
  • LAMP 全功能编译安装 for CentOS6.3笔记(更新)
  • springmvc的@RequestMapping、@PathVariable、@RequestParam
  • 多播、组播、广播优缺点分析
  • 向C#的String类添加按字节截取字符串的扩展方法
  • 根据要素选择集,创建新图层
  • windows2003建立隐藏管理员用户
  • Hbase 之 HBase 的整体架构
  • AgileEAS.NET之敏捷并行开发方法
  • Zabbix监控屏幕全屏显示多个监控项
  • 【RocksDB】TransactionDB源码分析
  • 【跃迁之路】【669天】程序员高效学习方法论探索系列(实验阶段426-2018.12.13)...
  • CentOS 7 防火墙操作
  • css选择器
  • es6要点
  • Java IO学习笔记一
  • k8s 面向应用开发者的基础命令
  • mac修复ab及siege安装
  • mysql innodb 索引使用指南
  • Python socket服务器端、客户端传送信息
  • Python_网络编程
  • Spring核心 Bean的高级装配
  • vue脚手架vue-cli
  • webpack4 一点通
  • 机器学习学习笔记一
  • 今年的LC3大会没了?
  • 聊聊sentinel的DegradeSlot
  • 聊一聊前端的监控
  • 盘点那些不知名却常用的 Git 操作
  • 前端面试总结(at, md)
  • 容器服务kubernetes弹性伸缩高级用法
  • 使用Gradle第一次构建Java程序
  • 1.Ext JS 建立web开发工程
  • 好程序员web前端教程分享CSS不同元素margin的计算 ...
  • ​configparser --- 配置文件解析器​
  • #FPGA(基础知识)
  • #宝哥教你#查看jquery绑定的事件函数
  • #我与Java虚拟机的故事#连载16:打开Java世界大门的钥匙
  • (06)Hive——正则表达式
  • (1)(1.19) TeraRanger One/EVO测距仪
  • (附源码)ssm失物招领系统 毕业设计 182317
  • (附源码)计算机毕业设计SSM基于java的云顶博客系统
  • (附源码)计算机毕业设计SSM疫情下的学生出入管理系统
  • (没学懂,待填坑)【动态规划】数位动态规划
  • (三)mysql_MYSQL(三)
  • (原)本想说脏话,奈何已放下
  • (原创)攻击方式学习之(4) - 拒绝服务(DOS/DDOS/DRDOS)
  • (转)ORM
  • *** 2003
  • ****** 二 ******、软设笔记【数据结构】-KMP算法、树、二叉树