The process simply said that the maximum threshold for new technologies is not complicated, but can be across this door, and the high people can be skilled.
But I had a big turnfor in the initial time. I took a day. This article hopes to give it convenient to the future.
1, New Project WindowsApplication2
2, put the references to all reference
>
(1) HashcodeProvider
(2) Iesi.collections
(3) log4net
(4) NHibernate
1. Follow our architecture map, from the bottom to the first step, generate the entity layer with Coolcoder, if you call Assembly Name, etc., don't understand, then you should read ".NET Framework Programming"
>
Then copy the generated physical class file and the expression XML to the project file ... (you can use Copy, Xcopy, Xcopy32, Move, drag and drop ... .; p)
And don't forget to include these files in the project, if WinForm must remember to modify the "generation operation" attribute of the XML file into "embedded resource".
If the XML file you generated is as follows:
XML Version = "1.0" encoding = "UTF-8"?>
clas>
hibernate-maping>
So congratulations, right! But ~~~~ Oh, an error! why? I don't know (some people understand it), but from Zhang Lao San, it should be modified as follows:
XML Version = "1.0" encoding = "UTF-8"?>
id>
clas>
hibernate-maping>
1. The physical layer is the core, and it is easy to get it. If you want to use a classic basic action, you can see my "NHIBERNATE (1) Basic Operation". Fortunately, I gave such a big brother.
Good classes come out, it's really benefiting! ---- Data Operating Layer EntityControl
Create a new class EntityControl.vb:
Imports system
Imports system.reflection
Imports system.data
Imports system.data.sqlclient
Imports nhibernate
Imports nhibitionnate.cfg
Imports nhibitionnate.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.diaforct
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 Thet AS ISTEM.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 nhibitionnate.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 kil
DIM SE As New SchemaExport (Mycfg)
SE.CREATE (TRUE, TRUE)
END IF
Sessions = mycfg.buildsessionFactory ()
Diagect = NHIBERNATE.DIALECT.DIALECT.GETDIALECT () End Sub 'Exportschema
'/
'/ Drops the schema tria 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 nhibitionnate.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, the next step is the business layer, each of us will use, here is not Luo, here is the one of the flying eagle (lazy ... Do not disturb).
Create a new business class, Customerbr.vb
Public Class Customerbr
DIM Control as entryControl
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.GeTities (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
Finally, it is the web ui. Of course you can also write a test code, come and see the gratifying green, I write button here, so as to say that this is really lazy! >
Dim Cust As Customerbr
Dim Customer As Customers
DIM Customerid As String
_
Public subinit ()
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.Arequal (Count 1, Cust.getCustomers ("from c in class customers"). Count)
End Sub
_
Public Sub Modify ()
Customer = Cust.getsinglecustomer (Customerid)
'Assert.Arequal ("
China
", Customer.Country)
Customer.country = "
Japan
"
Cust.Update (Customer)
Customer = Cust.getsinglecustomer (Customerid)
'Assert.isfalse (Customer.Country = "
China
")
'Assert.Arequal ("
Japan
", Customer.Country)
End Sub
_
Public SUB Delete ()
Customer = New Customers
Customer.customerid = Customerid
'Assert.arequal (1, Cust.getCustomers ("from c in class customers where c.customerid ='" & customerid & "'") .count)
Cust.delete (Customer)
'Assert.arequal (0, Cust.getCustomers ("from c in class customers where c.customerid ='" & customerid & "'") .count)
End Sub
Private sub button1_click (byvale as system.object, byval e as system.eventargs) Handles Button1.click
init ()
Add ()
End Sub