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

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

1NHibernate

2NBear

3Castle ActiveRecord

4iBATIS.NET

5DAAB

附加介绍:DLinq

 

一.NHibernate

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

None.gif public   class  User
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public User()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.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.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
get dot.gifreturn id; }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
set dot.gif{ id = value; }
ExpandedSubBlockEnd.gif    }

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

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

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

InBlock.gif    
public DateTime LastLogon
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
get dot.gifreturn lastLogon; }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
set dot.gif{ 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 >

官方主页:http://www.nhibernate.org/

学习资源

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

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

AeroNhibernate学习手记系列:http://www.cnblogs.com/chwkai/category/32514.html

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

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

 

二.NBear

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

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

1NBear最适合开发各类基于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也已经可以被用于企业级程序开发。

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

学习资源

学习资源当然首推Teddy的个人Bloghttp://www.cnblogs.com/teddyma/

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

 

三.Castle ActiveRecord

ActiveRecordCastle中的一个子项目,现在的版本是RC1。它同样是一个非常优秀的持久层框架,在底层封装了NHibernate,改用Attribute来代替配置文件,这样就不用再像NHibernate那样去编写复杂的配置文件。如下代码片断所示:

None.gif [ActiveRecord( " Users " )]
None.gif
public   class  User : ActiveRecordBase
ExpandedBlockStart.gifContractedBlock.gif
dot.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.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
get dot.gifreturn _id; }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
set dot.gif{ _id = value; }
ExpandedSubBlockEnd.gif    }

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

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

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

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

ExpandedBlockEnd.gif}

官方主页:http://www.castleproject.org

学习资源

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

叶子的家:http://wj.cnblogs.com/

TerryLeeCastle开发系列:

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

Castle项目成员之一ayendeBloghttp://www.ayende.com/Blog/

 

四.iBATIS.NET

iBATIS.NET分为DataMapperDataAccess两部分,应该说DataMapper是这个框架的核心,DataMapper使用XML文件来实现从实体到SQL statements的映射,学习起来非常简单,是用DataMapper后,我们可以自由的使用SQL语句或者存储过程;DataAccess允许我们通过一个简单的接口来操作数据,而不必了解底层实现的细节。如下代码片断:

None.gif [Serializable]
None.gif
public   class  Person
ExpandedBlockStart.gifContractedBlock.gif
dot.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.gifContractedSubBlock.gif    
public Person() dot.gif{ }
InBlock.gif    
public int Id
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
get dot.gifreturn id; }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
set dot.gif{ id = value; }
ExpandedSubBlockEnd.gif    }

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

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

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

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

InBlock.gif    
public double? HeightInMeters
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
get dot.gifreturn heightInMeters; }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
set dot.gif{ 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 >
官方主页: http://ibatis.apache.org/


学习资源

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

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


五.DAAB

DAAB是微软Enterprise Library中的一个应用程序块,能够帮助我们实现通用的数据访问,所以也把它列在这里介绍一下。DAAB使应用程序中的数据访问在不知道具体的数据库系统的情况下进行,相信很多朋友对DAAB都很熟性并且已经在项目中使用,就不多介绍了,看一个简单的代码片断:

None.gif public   string  GetCustomerList()
ExpandedBlockStart.gifContractedBlock.gif
dot.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.gifContractedSubBlock.gif
dot.gif{
InBlock.gif    
while (dataReader.Read())
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.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}

官方主页:http://msdn.microsoft.com/practices/

学习资源

企业的帮助文档和Hands On Lab

TerryLeeEnterprise Library系列:http://www.cnblogs.com/Terrylee/archive/2006/08/01/Enterprise_Library.html

 

附加介绍:DLinq

DLinq虽然不能算是开源框架,但是说到数据持久,还是提一下比较好,DLinq是微软下一代数据库集成查询语言,在这之前微软曾经尝试过ObjectSpace,最后是不了了之。DLinq实现的方式有点类似于前面说过的ActiveRecord,不支持使用外部的XML配置文件,而是使用了Attribute的方式,如下代码片断所示:

None.gif [Table(Name = " Customers " )]
None.gif
None.gif
public   class  Customer
None.gif
ExpandedBlockStart.gifContractedBlock.gif
dot.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}

官方主页:http://msdn.microsoft.com/netframework/future/linq/

学习资源

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

ScottGuBloghttp://weblogs.asp.net/scottgu/default.aspx

 

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值