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(); } } }