Search     Login     Register    

Download Documentation Tools News

Community Forum Bugs Requests


NPersist Example Code, VB.NET

Visual Basic.NET:

'The context manager is the main
'point of access to the NPersist framework. We
'must supply the context manager with the path
'to our XML file containing the object/relational
'mapping information:
Dim Context As New Context("C:\MyDomain.npersist")

'This is the persistent employee
Dim emp As New Employee()		


'Then we ask the context manager
'to create the object for us, eventually 
'adding the row to the 'tblEmployees'
'table in the database.
'however, at this point it will
'only start tracking the object,
'logging it as 'Up For Creation'
'and noting when you write to
'its properties that they become 'Dirty'.
Context.CreateObject(emp)

'We then set the properties
'for the new employee we want to
'add to the database
Employee.FirstName = "Mats"
Employee.LastName = "Helander"

'Only when we ask the context manager
'to 'do all its work' (persist all)
'will the new row actually be inserted
'into the database:
Context.PersistAll()

'By now the employee object's 'ID' property, 
'which is mapping to an auto-increasing field 
'in the database, has been filled with the
'new ID value from the database:
MsgBox("New employee ID: " & emp.ID)


'We declare a second employee variable
'(this is kind of stupid behavior,
'but it is just to demonstrate a feature...)
Dim emp2 As Employee

'Ask the context manager to fetch the employee
'with an ID matching the newly created ID...
'...this should yield another reference to 
'the same object in our second variable!
emp2 = Context.GetObject(emp.ID, GetType(Employee))

'Thanks to the 'uniquing' feature, implemented
'using the 'Identity Map' pattern
'the following code will work:
If emp Is emp2 Then

	MsgBox("Uniquing works!")

Else

	MsgBox("Uniquing failed!")

End If

'Now let's update the object
'(it doesn't matter which variable
'we use, emp or emp2, they point
'at the same object thanks to uniquing!)
emp.FirstName = "Some"
emp.LastName = "Guy"

'We don't have to call any particular
'"UpdateObject()" method on the context
'manager to tell it that the object
'needs updating - thanks to the property
'notification (see the source code example
'for the 'Employee' class) the context
'manager knows that the object is 'dirty'
'(which means updated...)

'Again, the row in the database will not
'actually be updated until we ask the
'context manager to do all its work by
'calling the PersistAll() method:
Context.PersistAll()


'Finally, let's ask the context manager
'to delete the object from the database:
Context.DeleteObject(emp)

'As always, the context manager will not
'do any actual work against the database
'until we ask it to:
Context.PersistAll()


'So, we have created, retrieved, 
'updated and deleted or object to/from
'the database- the full CRUD cycle.
'As a last example, let's look at 
'how we can fetch a list of employees
'matching a query using the NPersist
'"NPath" object/query language:

'create the query:
'This query will fetch all
'employees that is either me
'(their first/last name is 'Mats'
'and 'Helander') or whose first
'name begins with the letter 'A'.
Dim npath As New NPathQuery( _
	"Select * From Employee Where " & _
	"(FirstName = 'Mats' And " & _
	"LastName = 'Helander') Or " & _
	"FirstName LIKE 'A%' Order By LastName"

'declare the variable for holding
'the resulting list of employees.
Dim employees As ArrayList

'ask the context manager to execute 
'the npath query:
employees = Context.GetObjects( _
	npath, _
	GetType(Employee))

'Iterate through the collection of employees:
For Each emp In employees

	'Do stuff with each employee...
	'........

	'Ok, now in this very simple
	'example, the employee only
	'has three properties (FirstName,
	'LastName and ID) and none of
	'them is a reference property...
	'However, if our object had 
	'reference properties, we could
	'navigate them using the familiar
	'OO dot syntax:
	MsgBox(emp.Company.Name)

	'We can (when our objects have them)
	'traverse any number of reference properties
	'in this fashion. As each new property in
	'the line is accessed, the context manager
	'checks behind the scene to see that the property
	'has been loaded with a value from the database -
	'and if it hasn't, it loads the value (this is
	'called Lazy Loading
	MsgBox(emp.Company.Address.Country.CapitalCity.Name)


Next

'We must always end by disposing the 
'context manager, which in its turn
'will dispose of all its database
'connections.
Context.Dispose()


google search optimization price . Buy Provillus . Find unique glass pipes & color changing glass pipes worldwide.

Copyright © Mats Helander 2004 | MatsSoft


Косметология филлеры. Басманный.