Search     Login     Register    

Download Documentation Tools News

Community Forum Bugs Requests


NPersist Example Code, C#

C#:

//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:
Context Context = 
    new Context("C:\MyDomain.npersist");

//This is the persistent employee
Employee emp = 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:
MessagegBox.Show("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...)
Employee emp2;

//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, typeof(Employee));

//Thanks to the 'uniquing' feature, implemented
//using the 'Identity Map' pattern
//the following code will work:
if (emp == emp2) {

	MessageBox.Show("Uniquing works!");

} else {

	MessageBox.Show("Uniquing failed!");

}

//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'.
NPathQuery npath = 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.
ArrayList employees;

//ask the context manager to execute 
//the npath query:
employees = Context.GetObjects( 
	npath, 
	typeof(Employee));

//Iterate through the collection of employees:
foreach (Employee empl 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:
	MessageBox.Show(empl.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
	MessageBox.Show(
		empl.Company.Address.Country.CapitalCity.Name);


}

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


Toronto e s c o r t s . batterie HTC Rezound 1900 mah . We bet you never thought about such an amount of different bets movies existence.

Copyright © Mats Helander 2004 | MatsSoft