<?
xml version="1.0" encoding="utf-8"
?>
![](http://static.javashuo.com/static/loading.gif)
<
hibernate-mapping
xmlns
="urn:nhibernate-mapping-2.0"
>
![](http://static.javashuo.com/static/loading.gif)
<
class
name
="NHibernateWebDemo.Model.User, NHibernateWebDemo.Model"
table
="users"
>
![](http://static.javashuo.com/static/loading.gif)
<
id
name
="Id"
column
="LogonId"
type
="String"
length
="20"
>
![](http://static.javashuo.com/static/loading.gif)
<
generator
class
="assigned"
/>
![](http://static.javashuo.com/static/loading.gif)
</
id
>
![](http://static.javashuo.com/static/loading.gif)
<
property
name
="UserName"
column
= "Name"
type
="String"
length
="40"
/>
![](http://static.javashuo.com/static/loading.gif)
<
property
name
="Password"
type
="String"
length
="20"
/>
![](http://static.javashuo.com/static/loading.gif)
<
property
name
="EmailAddress"
type
="String"
length
="40"
/>
![](http://static.javashuo.com/static/loading.gif)
<
property
name
="LastLogon"
type
="DateTime"
/>
![](http://static.javashuo.com/static/loading.gif)
</
class
>
![](http://static.javashuo.com/static/loading.gif)
</
hibernate-mapping
>
學習資源
二.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也已經能夠被用於企業級程序開發。
學習資源
三.Castle ActiveRecord
ActiveRecord
是Castle中的一個子項目,如今的版本是RC1。它一樣是一個很是優秀的持久層框架,在底層封裝了NHibernate,改用Attribute來代替配置文件,這樣就不用再像NHibernate那樣去編寫複雜的配置文件。以下代碼片段所示:
[ActiveRecord(
"
Users
"
)]
public
class
User : ActiveRecordBase
![](http://static.javashuo.com/static/loading.gif)
{
private int _id;
private string _name;
private string _password;
private string _emailAddress;
private DateTime _lastLogon;
[PrimaryKey(PrimaryKeyType.Identity, "LogonID")]
public int Id
![](http://static.javashuo.com/static/loading.gif)
{
![](http://static.javashuo.com/static/loading.gif)
get
{ return _id; }
![](http://static.javashuo.com/static/loading.gif)
set
{ _id = value; }
}
[Property("LogonName")]
public string Name
![](http://static.javashuo.com/static/loading.gif)
{
![](http://static.javashuo.com/static/loading.gif)
get
{ return _name; }
![](http://static.javashuo.com/static/loading.gif)
set
{ _name = value; }
}
[Property("Password")]
public string Password
![](http://static.javashuo.com/static/loading.gif)
{
![](http://static.javashuo.com/static/loading.gif)
get
{ return _password; }
![](http://static.javashuo.com/static/loading.gif)
set
{ _password = value; }
}
[Property("EmailAddress")]
public string Address
![](http://static.javashuo.com/static/loading.gif)
{
![](http://static.javashuo.com/static/loading.gif)
get
{ return _emailAddress; }
![](http://static.javashuo.com/static/loading.gif)
set
{ _emailAddress = value; }
}
[Property("LastLogon")]
public DateTime LastLogon
![](http://static.javashuo.com/static/loading.gif)
{
![](http://static.javashuo.com/static/loading.gif)
get
{ return _lastLogon; }
![](http://static.javashuo.com/static/loading.gif)
set
{ _lastLogon = value; }
}
}
學習資源
TerryLee
的Castle開發系列:
四.iBATIS.NET
iBATIS.NET
分爲DataMapper和DataAccess兩部分,應該說DataMapper是這個框架的核心,DataMapper使用XML文件來實現從實體到SQL statements的映射,學習起來很是簡單,是用DataMapper後,咱們能夠自由的使用SQL語句或者存儲過程;DataAccess容許咱們經過一個簡單的接口來操做數據,而沒必要了解底層實現的細節。以下代碼片段:
[Serializable]
public
class
Person
![](http://static.javashuo.com/static/loading.gif)
{
private int id;
private string firstName;
private string lastName;
private DateTime? birthDate;
private double? weightInKilograms;
private double? heightInMeters;
![](http://static.javashuo.com/static/loading.gif)
public Person()
{ }
public int Id
![](http://static.javashuo.com/static/loading.gif)
{
![](http://static.javashuo.com/static/loading.gif)
get
{ return id; }
![](http://static.javashuo.com/static/loading.gif)
set
{ id = value; }
}
public string FirstName
![](http://static.javashuo.com/static/loading.gif)
{
![](http://static.javashuo.com/static/loading.gif)
get
{ return firstName; }
![](http://static.javashuo.com/static/loading.gif)
set
{ firstName = value; }
}
public string LastName
![](http://static.javashuo.com/static/loading.gif)
{
![](http://static.javashuo.com/static/loading.gif)
get
{ return lastName; }
![](http://static.javashuo.com/static/loading.gif)
set
{ lastName = value; }
}
public DateTime? BirthDate
![](http://static.javashuo.com/static/loading.gif)
{
![](http://static.javashuo.com/static/loading.gif)
get
{ return birthDate; }
![](http://static.javashuo.com/static/loading.gif)
set
{ birthDate = value; }
}
public double? WeightInKilograms
![](http://static.javashuo.com/static/loading.gif)
{
![](http://static.javashuo.com/static/loading.gif)
get
{ return weightInKilograms; }
![](http://static.javashuo.com/static/loading.gif)
set
{ weightInKilograms = value; }
}
public double? HeightInMeters
![](http://static.javashuo.com/static/loading.gif)
{
![](http://static.javashuo.com/static/loading.gif)
get
{ return heightInMeters; }
![](http://static.javashuo.com/static/loading.gif)
set
{ heightInMeters = value; }
}
}
映射文件以下:
<?
xml version="1.0" encoding="utf-8"
?>
<
sqlMap
namespace
="Person"
xmlns
="http://ibatis.apache.org/mapping"
xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
>
<
alias
>
<
typeAlias
alias
="Person"
type
="IBatisNetDemo.Domain.Person,IBatisNetDemo"
/>
</
alias
>
![](http://static.javashuo.com/static/loading.gif)
<
resultMaps
>
<
resultMap
id
="SelectAllResult"
class
="Person"
>
<
result
property
="Id"
column
="PER_ID"
/>
<
result
property
="FirstName"
column
="PER_FIRST_NAME"
/>
<
result
property
="LastName"
column
="PER_LAST_NAME"
/>
<
result
property
="BirthDate"
column
="PER_BIRTH_DATE"
/>
<
result
property
="WeightInKilograms"
column
="PER_WEIGHT_KG"
/>
<
result
property
="HeightInMeters"
column
="PER_HEIGHT_M"
/>
</
resultMap
>
</
resultMaps
>
![](http://static.javashuo.com/static/loading.gif)
<
statements
>
<
select
id
="SelectAllPerson"
resultMap
="SelectAllResult"
>
select
PER_ID,
PER_FIRST_NAME,
PER_LAST_NAME,
PER_BIRTH_DATE,
PER_WEIGHT_KG,
PER_HEIGHT_M
from PERSON
</
select
>
![](http://static.javashuo.com/static/loading.gif)
<
select
id
="SelectByPersonId"
resultClass
="Person"
parameterClass
="int"
>
select
PER_ID,
PER_FIRST_NAME,
PER_LAST_NAME,
PER_BIRTH_DATE,
PER_WEIGHT_KG,
PER_HEIGHT_M
from PERSON
where PER_ID = #value#
</
select
>
<
insert
id
="InsertPerson"
parameterclass
="Person"
>
<
selectKey
property
="Id"
type
="post"
resultClass
="int"
>
${selectKey}
</
selectKey
>
insert into Person
( PER_FIRST_NAME,
PER_LAST_NAME,
PER_BIRTH_DATE,
PER_WEIGHT_KG,
PER_HEIGHT_M)
values
(#FirstName#,#LastName#,#BirthDate#, #WeightInKilograms#, #HeightInMeters#)
</
insert
>
<
update
id
="UpdatePerson"
parameterclass
="Person"
>
<![CDATA[
update Person set
PER_FIRST_NAME =#FirstName#,
PER_LAST_NAME =#LastName#,
PER_BIRTH_DATE =#BirthDate#,
PER_WEIGHT_KG=#WeightInKilograms#,
PER_HEIGHT_M=#HeightInMeters#
where
PER_ID = #Id#
]]>
</
update
>
![](http://static.javashuo.com/static/loading.gif)
<
delete
id
="DeletePerson"
parameterclass
="Person"
>
delete from Person
where
PER_ID = #Id#
</
delete
>
</
statements
>
</
sqlMap
>
官方主頁:[url]http://ibatis.apache.org/[/url]
學習資源
五.DAAB
DAAB
是微軟Enterprise Library中的一個應用程序塊,可以幫助咱們實現通用的數據訪問,因此也把它列在這裏介紹一下。DAAB使應用程序中的數據訪問在不知道具體的數據庫系統的狀況下進行,相信不少朋友對DAAB都很熟性而且已經在項目中使用,就很少介紹了,看一個簡單的代碼片段:
public
string
GetCustomerList()
![](http://static.javashuo.com/static/loading.gif)
{
// 建立Database對象
Database db = DatabaseFactory.CreateDatabase();
// 使用SQL語句建立DbCommand對象
string sqlCommand = "Select CustomerID, Name, Address, City, Country, PostalCode " +
"From Customers";
DbCommand dbCommand = db.GetSqlStringCommand(sqlCommand);
StringBuilder readerData = new StringBuilder();
// 調用ExecuteReader方法
using (IDataReader dataReader = db.ExecuteReader(dbCommand))
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
{
while (dataReader.Read())
![](http://static.javashuo.com/static/loading.gif)
{
// Get the value of the 'Name' column in the DataReader
readerData.Append(dataReader["Name"]);
readerData.Append(Environment.NewLine);
}
}
return readerData.ToString();
}
學習資源
企業的幫助文檔和Hands On Lab
附加介紹:
DLinq
DLinq
雖然不能算是開源框架,可是說到數據持久,仍是提一下比較好,DLinq是微軟下一代數據庫集成查詢語言,在這以前微軟曾經嘗試過ObjectSpace,最後是不了了之。DLinq實現的方式有點相似於前面說過的ActiveRecord,不支持使用外部的XML配置文件,而是使用了Attribute的方式,以下代碼片段所示:
[Table(Name
=
"
Customers
"
)]
![](http://static.javashuo.com/static/loading.gif)
public
class
Customer
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
{
![](http://static.javashuo.com/static/loading.gif)
[Column(Id=true)]
![](http://static.javashuo.com/static/loading.gif)
public string CustomerID;
![](http://static.javashuo.com/static/loading.gif)
[Column]
![](http://static.javashuo.com/static/loading.gif)
public string City;
![](http://static.javashuo.com/static/loading.gif)
}
學習資源
最後值得一提的是,微軟又推出個Ado.net vNext,使用映射文件來配置,更加相似於NHibernate。關於持久層框架,還有不少,這裏就再也不介紹了,如Grove等。
Ctrl+Enter 發佈web
發佈sql
取消數據庫