Persistate

Creating objects

Hide Navigation Pane

Creating objects

Previous topic Next topic No directory for this topic  

Creating objects

Previous topic Next topic Topic directory requires JavaScript JavaScript is required for the print function Mail us feedback on this topic!  

Creating new persistent objects is very easy.  You use the new operator as you would when creating normal objects.  When generating your defined classes, Persistate generates a zero parameter default constructor for you.  You can add any number of other constructors for your classes, but they should all call the default constructor, which initialises any contained scalar members and creates empty contained collections.  Here is an example of the definition and use of a constructor

// Constructor for the author class
public Author(string surname, string forename) 
   : this()         // Initialises contained members
{
   this.surname = surname;
   this.forename = forename;
}
 
// Create an Author object for Charles Dickens
public static Author WhatTheDickens() 
{
   return new Author("Dickens", "Charles");
}

Once you have created an object, you must attach it to the persistent tree - it must become the contained object of an object already in the tree.  You can either set it as the value of a contained (as opposed to associated) scalar object member – normally quite rare – or more likely, add it to an contained collection member.  Here is an example of adding a new object to a persistent collection.

Author newAuthor = Author.WhatTheDickens();
The.Library.Authors.Add(newAuthor);

You can create an entire sub-tree of objects and attach the entire tree just by attaching the root object.  For example.

// create the book, associate the desired author and genre objects
Book newBook = new Book();
newBook.Title = "Bleak House";
newBook.Author = newAuthor;
newBook.Genre = The.Library.Genres["Classic Fiction"];
// create four stock copies, and add to book's collection
for (int i = 0; i < 4; i++) {
   StockCopy copy = new StockCopy();
   copy.LendStatus = LendStatus.Available;
   newBook.StockCopies.Add(copy);
}
// attach book and stock copies to the persistent tree
The.Library.Publications.Add(newBook);

You can also set a new object into a particular position in a collection using the one parameter indexer.  When you do this, Persistate removes any object which was already in that position from the collection.  If it is an actual collection, then Persistate also deletes the removed object – see Deleting objects.  Here is an example.

The.Library.Authors[3] = newAuthor;

Persistate automatically keeps lists of new, modified and deleted objects.  It also takes care of assigning new primary keys and creating any links which are required to link the author to the library.  By default, Persistate stores objects in the same database as their parent object.  However you can change that easily.  Say you want to store your new author in a database called "Literati".  You could do this as follows:

Author newAuthor = Author.WhatTheDickens();
Database literati = The.Domain.Databases["Literati"];
newAuthor.StoreInDatabase(literati);
The.Library.Authors.Add(newAuthor);

Note that you must call StoreInDatabase before you add the new author to its parent collection.  You must also ensure that the Literati database is set up to store objects from the Library namespace. See the Installing and running the Persistate Server walkthrough for an example of doing this.

Information

Note that to perform the above without error, the Authors collection would have to be defined as distributed, which in our Library sample it isn’t.

There is only one further thing you have to do, and that is to send changes you have made to persistent storage.  See Synchronising with persistent storage for details.  When you insert an object into the persistent tree, Persistate raises events associated with this.  See the Persistent events section for full details of this and other events raised by Persistate.