n .sistence sistence sistence sistence command summary and

Reviews
Shared by: Terry Giulianni
Stats
views:
0
rating:
not rated
reviews:
0
posted:
6/7/2009
language:
English
pages:
0
n.sistence : command summary and sample code sistence Syntax new Repository( string fileName ) .Store( object item ) Return Type Repository None Description Creates or Opens the repository at the path provided by fileName Stores an object into the Repository; may be called multiple times for the same object Returns all objects of type T in the Repository Returns all objects of type T matching the query delegate Returns an object of type T matching the query delegate Removes the given object from the Repository Writes the Repository to disk at the location provided in the Repository constructor Restores the Repository to the state at the last Commit() .GetAll() .GetAll( Predicate predicate ) .GetOne( Predicate predicate ) .Remove( object item ) .Commit() List (0 or more items) List (0 or more items) Person (or null) None None .Rollback() None Note: nsistence.Repository will persist and query most object types. For illustration purposes, the following examples use a Person class (code follows) Syntax new Repository( “people.repo” ) .Store( person ) Return Type Repository None Description Creates or Opens the repository at the path provided by fileName Stores an object into the Repository; may be called multiple times for the same object Returns all Person objects in the Repository Returns all Person objects matching the query delegate Returns a Person object matching the query delegate Removes the given Person object from the Repository Writes the Repository to disk at the location provided in the Repository constructor Restores the Repository to the state at the last Commit() .GetAll() .GetAll( Person.IsMale() ) .GetOne( Person.IsMale() ) .Remove( person ) .Commit() List (0 or more items) List (0 or more items) Person (or null) None None .Rollback() None n.sistence : command summary and sample code sistence Person.cs: using System; using System.Collections.Generic; using System.Text; namespace Demo { [Serializable] public class Person { public string FirstName; public string LastName; public Gender Gender; public static Predicate HasGender( Gender gender ) { return new Predicate( delegate( Person person ) { return person.Gender == gender; } ); } public static Predicate IsMale() { return Person.HasGender( Gender.Male ); } } } Gender.cs: using System; using System.Collections.Generic; using System.Text; namespace Demo { public enum Gender { Male , Female } } Important: All classes to be persisted must be marked with the [Serializable] attribute. n.sistence : command summary and sample code sistence A demonstration of the Repository methods using the above class and enumeration: using using using using System; System.Collections.Generic; System.Text; SiGANIC.nsistence; namespace Demo { class Program { static void Main( string[] args ) { Repository repository = new Repository( "people.repo" ); // create a person named John Doe Person johnDoe = new Person(); johnDoe.FirstName = "John"; johnDoe.LastName = "Doe"; johnDoe.Gender = Gender.Male; // create his wife, Jane Doe Person janeDoe = new Person(); janeDoe.FirstName = "Jane"; janeDoe.LastName = "Doe"; janeDoe.Gender = Gender.Female; // create his neighbor, Mark Twain Person markTwain = new Person(); markTwain.FirstName = "Mark"; markTwain.LastName = "Twain"; markTwain.Gender = Gender.Male; // store them in the repository to make them queryable repository.Store( johnDoe ); repository.Store( janeDoe ); repository.Store( markTwain ); // persist the repository to disk repository.Commit(); // query for all objects of a type // results: johnDoe, janeDoe, markTwain List everyone = repository.GetAll(); // query using a predefined Predicate<> delegate // results: johnDoe, markTwain List theMen = repository.GetAll( Person.IsMale ); // query using a predefined Predicate<> closure // results: janeDoe List theWomen = repository.GetAll( Person.HasGender( Gender.Female ) ); n.sistence : command summary and sample code sistence // query using an anonymous delegate // results: johnDoe, janeDoe List theDoes = repository.GetAll( delegate( Person person ) { return person.LastName == "Doe"; } ); // query using a more complex anonymous delegate // results: markTwain List moreComplexResults = repository.GetAll( delegate( Person person ) { return ( person.FirstName.Contains( "a" ) ) && ( person.Gender != Gender.Female ); } ); // querying using a Predicate<> defined in the class // results: johnDoe, janeDoe Predicate hasShortLastName = new Predicate( delegate( Person person ) { return person.LastName.Length < 5; } ); List peopleWithShortLastNames = repository.GetAll( hasShortLastName ); // find a single object matching the given criteria // results: janeDoe Person aFemalePerson = repository.GetOne( Person.HasGender( Gender.Female ) ); // delete an object // results: janeDoe is deleted repository.Delete( aFemalePerson ); // find a single object, modify it, and update the repository // results: johnDoe is renamed "John Smith" Person matchingPerson = repository.GetOne( delegate( Person person ) { return ( person.FirstName == "John" ) && ( person.LastName == "Doe" ); } ); matchingPerson.LastName = "Smith"; repository.Store( matchingPerson ); // restore from our last Commit() // results: janeDoe is un-deleted, "John Smith" is restored to "John Doe" repository.Rollback(); } } }

Related docs
Command Summary
Views: 7  |  Downloads: 1
Unix Command Summary
Views: 3  |  Downloads: 1
Cisco Command Summary
Views: 76  |  Downloads: 5
SCSI Command
Views: 42  |  Downloads: 0
Working at a command line prompt
Views: 66  |  Downloads: 5
Cisco Command Summary
Views: 127  |  Downloads: 3
central command inc
Views: 20  |  Downloads: 4
Command Name
Views: 3  |  Downloads: 1
E10K Command
Views: 15  |  Downloads: 0
Unit 14 – The Exam Command Summary
Views: 2  |  Downloads: 1
SaturnInstrument Unit Command System
Views: 0  |  Downloads: 0
StarLogo Complete Command List (Printable)
Views: 5  |  Downloads: 0
IA Command Handbook.p65
Views: 0  |  Downloads: 0
premium docs
Other docs by Terry Giuliann...
Sample workplace AIDS policy
Views: 370  |  Downloads: 10
Numbered Notes
Views: 255  |  Downloads: 2
CHECK REGISTER
Views: 361  |  Downloads: 20
Shareholders Resolution Approving an Acquisition
Views: 318  |  Downloads: 10
Users marcsigal Desktop term papers pagemills
Views: 200  |  Downloads: 0
THE REVERSE MERGER: BACKING INTO WALL
Views: 649  |  Downloads: 36
Demand for Inspection of Books and Records
Views: 306  |  Downloads: 6