下面把过程简单的说一下,新技术的最大门槛不是复杂,而是能否跨进这个门,高人们可以略过了。
但是我在最初的时候费了好大周折,用了一天时间,本文希望给后人个方便,少走弯路。
1、 新建工程 WindowsApplication2
2、 把该引用的都引用上
>
(1) HashCodeProvider
(2) Iesi.Collections
(3) Log4net
(4) NHibernate
1、 按照我们的架构图,从下至上第一步,用coolcoder生成实体层,如果你连什么叫Assembly Name等等名词都不懂,那么推荐你去读读《.net框架编程》吧
>
然后把生成的实体类文件和表述xml复制到项目文件中…(你可以用copy,xcopy,xcopy32,move,拖拽…….;p)
而且别忘记把这些生成的文件包含在项目中,如果是winform一定记得把xml文件的“生成操作”属性修改成“嵌入的资源”。
如果你生成的xml文件如下:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="WindowsApplication2.Customers, WindowsApplication2" table="Customers">
<property name="CustomerID" type="String(5)" column="CustomerID" />
<property name="CompanyName" type="String(40)" column="CompanyName" />
<property name="ContactName" type="String(30)" column="ContactName" />
<property name="ContactTitle" type="String(30)" column="ContactTitle" />
<property name="Address" type="String(60)" column="Address" />
<property name="City" type="String(15)" column="City" />
<property name="Region" type="String(15)" column="Region" />
<property name="PostalCode" type="String(10)" column="PostalCode" />
<property name="Country" type="String(15)" column="Country" />
<property name="Phone" type="String(24)" column="Phone" />
<property name="Fax" type="String(24)" column="Fax" />
</class>
</hibernate-mapping>
那么恭喜你,对了!但是呢~~~~呵呵,出错!为什么?我也不知道(有高人明白给说说吧),但是从张老三那里看来的,应该修改成如下:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="WindowsApplication2.Customers, WindowsApplication2" table="Customers">
<id name="CustomerID" column="CustomerID" type="String(5)">
<generator class="assigned" />
</id>
<property name="CompanyName" type="String(40)" column="CompanyName" />
<property name="ContactName" type="String(30)" column="ContactName" />
<property name="ContactTitle" type="String(30)" column="ContactTitle" />
<property name="Address" type="String(60)" column="Address" />
<property name="City" type="String(15)" column="City" />
<property name="Region" type="String(15)" column="Region" />
<property name="PostalCode" type="String(10)" column="PostalCode" />
<property name="Country" type="String(15)" column="Country" />
<property name="Phone" type="String(24)" column="Phone" />
<property name="Fax" type="String(24)" column="Fax" />
</class>
</hibernate-mapping>
1、 实体层是核心,搞定它其他就容易多了。如果你想用经典的基本操作也可以,可以就看我的“Nhibernate (1) 基本操作”。好在飞鹰大哥给了这么一个
好的类出来,真是造福苍生啊!----数据操作层EntityControl
新建一个类EntityControl.vb :
Imports System
Imports System.Reflection
Imports System.Data
Imports System.Data.SqlClient
Imports NHibernate
Imports NHibernate.Cfg
Imports NHibernate.Dialect
Imports NHibernate.Tool.hbm2ddl
Public Class EntityControl
Private Shared entity As EntityControl
Protected Shared myCfg As NHibernate.Cfg.Configuration
Protected Shared dialect As NHibernate.dialect.Dialect
Protected Shared sessions As ISessionFactory
Public Shared Function CreateControl() As EntityControl
If entity Is Nothing Then
BuildSessionFactory()
If entity Is Nothing Then
entity = New EntityControl
End If
End If
Return entity
End Function 'CreateControl
Private Shared Sub BuildSessionFactory()
ExportSchema(New String() {"Customers.hbm.xml"}, False)
End Sub 'BuildSessionFactory
Public Sub AddEntity(ByVal entity As Object)
Dim s As ISession = sessions.OpenSession()
Dim t As ITransaction = s.BeginTransaction()
Try
s.Save(entity)
t.Commit()
Catch e As Exception
t.Rollback()
Throw e
Finally
s.Close()
End Try
End Sub 'AddEntity
Public Sub UpdateEntity(ByVal entity As Object, ByVal key As Object)
Dim s As ISession = sessions.OpenSession()
Dim t As ITransaction = s.BeginTransaction()
Try
s.Update(entity, key)
t.Commit()
Catch e As Exception
t.Rollback()
Throw e
Finally
s.Close()
End Try
End Sub 'UpdateEntity
Public Sub DeleteEntity(ByVal entity As Object)
Dim s As ISession = sessions.OpenSession()
Dim t As ITransaction = s.BeginTransaction()
Try
s.Delete(entity)
t.Commit()
Catch e As Exception
t.Rollback()
Throw e
Finally
s.Close()
End Try
End Sub 'DeleteEntity
Public Function GetEntities(ByVal query As String) As IList
Dim lst As IList
Dim s As ISession = sessions.OpenSession()
Dim t As ITransaction = s.BeginTransaction()
lst = s.Find(query)
t.Commit()
s.Close()
Return lst
End Function 'GetEntities
Public Function GetEntity(ByVal theType As System.Type, ByVal id As Object) As Object
Dim obj As Object
Dim s As ISession = sessions.OpenSession()
Dim t As ITransaction = s.BeginTransaction()
obj = s.Load(theType, id)
t.Commit()
s.Close()
Return obj
End Function 'GetEntity
Private Overloads Shared Sub ExportSchema(ByVal files() As String) '
ExportSchema(files, True)
End Sub 'ExportSchema
Private Overloads Shared Sub ExportSchema(ByVal files() As String, ByVal exportSchema As Boolean)
myCfg = New NHibernate.Cfg.Configuration
Dim i As Integer
For i = 0 To files.Length - 1
myCfg.AddResource("WindowsApplication2." + files(i), [Assembly].Load("WindowsApplication2"))
Next i
If exportSchema Then
Dim se As New SchemaExport(myCfg)
se.Create(True, True)
End If
sessions = myCfg.BuildSessionFactory()
dialect = NHibernate.Dialect.Dialect.GetDialect()
End Sub 'ExportSchema
'/
'/ Drops the schema that was built with the TestCase's Configuration.
'/
Private Shared Sub DropSchema()
Dim se As New SchemaExport(myCfg)
se.Drop(True, True)
End Sub 'DropSchema
Private Overloads Shared Sub ExecuteStatement(ByVal sql As String)
ExecuteStatement(sql, True)
End Sub 'ExecuteStatement
Private Overloads Shared Sub ExecuteStatement(ByVal sql As String, ByVal [error] As Boolean)
Dim conn As IDbConnection = Nothing
Dim tran As IDbTransaction = Nothing
Try
If myCfg Is Nothing Then
myCfg = New NHibernate.Cfg.Configuration
End If
Dim prov As NHibernate.Connection.IConnectionProvider = NHibernate.Connection.ConnectionProviderFactory.NewConnectionProvider(myCfg.Properties)
conn = prov.GetConnection()
tran = conn.BeginTransaction()
Dim comm As IDbCommand = conn.CreateCommand()
comm.CommandText = sql
comm.Transaction = tran
comm.CommandType = CommandType.Text
comm.ExecuteNonQuery()
tran.Commit()
Catch exc As Exception
If Not (tran Is Nothing) Then
tran.Rollback()
End If
If [error] Then
Throw exc
End If
Finally
If Not (conn Is Nothing) Then
conn.Close()
End If
End Try
End Sub 'ExecuteStatement
End Class 'EntityControl
2、 下一步是业务层,我们每个人都会用了,这里就不罗嗦了,这里列出飞鹰的那个啦(偷懒中…请勿打扰)。
新建一个业务类,CustomerBR.vb
Public Class CustomerBR
Dim control As EntityControl
Sub New()
control = EntityControl.CreateControl()
End Sub
Public Sub Add(ByVal Customer As Customers)
Check(Customer)
control.AddEntity(Customer)
End Sub
Public Sub Update(ByVal Customer As Customers)
control.UpdateEntity(Customer, Customer.CustomerID)
End Sub
Public Sub Delete(ByVal Customer As Customers)
control.DeleteEntity(Customer)
End Sub
Public Function GetCustomers(ByVal query As String) As IList
Return control.GetEntities(query)
End Function
Public Function GetSingleCustomer(ByVal ID As String) As Customers
Return CType(control.GetEntity(GetType(Customers), ID), Customers)
End Function
Private Sub Check(ByVal Customer As Customers)
If Customer.CustomerID = "911" Then
Throw New ApplicationException("911 is not suitable to be used to CustomerID.")
End If
End Sub
End Class
最后是web UI了,你当然也可以写个测试代码,来看看喜人的绿色,我这里写个button,以免被人说这小子真懒!
>
Dim Cust As CustomerBR
Dim customer As Customers
Dim customerID As String
_
Public Sub init()
Cust = New CustomerBR
customerID = "Liuyuer@gmail.com"
End Sub
_
Public Sub Add()
customer = New Customers
Dim count As Integer
count = Cust.GetCustomers("from c in class Customers").Count
With customer
.CustomerID = customerID
.CompanyName = "GPCT"
.Country = " China "
End With
Cust.Add(customer)
'Assert.AreEqual(count + 1, Cust.GetCustomers("from c in class Customers").Count)
End Sub
_
Public Sub Modify()
customer = Cust.GetSingleCustomer(customerID)
'Assert.AreEqual(" China ", customer.Country)
customer.Country = " Japan "
Cust.Update(customer)
customer = Cust.GetSingleCustomer(customerID)
'Assert.IsFalse(customer.Country = " China ")
'Assert.AreEqual(" Japan ", customer.Country)
End Sub
_
Public Sub Delete()
customer = New Customers
customer.CustomerID = customerID
'Assert.AreEqual(1, Cust.GetCustomers("from c in class Customers where c.CustomerID = '" & customerID & "'").Count)
Cust.Delete(customer)
'Assert.AreEqual(0, Cust.GetCustomers("from c in class Customers where c.CustomerID = '" & customerID & "'").Count)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
init()
Add()
End Sub