Persistate

Diverse and distributed collections

Hide Navigation Pane

Diverse and distributed collections

Previous topic Next topic No directory for this topic  

Diverse and distributed collections

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

Diverse collections

If you include the diverse qualifier in a collection of many or several objects in your definition file, then this allows that collection to contain objects of the stated class, or of any class derived from it.  For example, in An Example Definition File, the publications collection in the library class is defined as diverse, meaning the the collection can contain publications, periodicals or books.

In the code generated for this collection, the collection property is still of type IndexedList<Publication>, but of course this can contain objects of the class Publication, Periodical or Book, since the latter two are derived from the first.  When you use one of the methods generated for the Publications collection, the objects retrieved can be of any of these three classes.  Here is an example,

foreach (Publication publication in The.Library.PublicationsWithTitle(suppliedTitle)) {
   if (publication is Book)
      return ((Book)publication).Author.FullName;
   else if (publication is Periodical)
      return ((Periodical)publication).IssueDate.ToString();
}

For collections of many objects, Persistate generates a <collection plural name>Like method to retrieve objects bases on all discriminative members - see Collections of many objects for details.  If the collection is diverse, then this method has an extra parameter, which is the ObjectClass of the type of object you want to search for,  For example, in the Publications collection, you could search for all publications, or just books or just periodicals.  Here is an example which looks for a book by an author with surname Jones, and with case sensitive "Hopeless" and case insensitive "quest" in the title.

IndexedList<Choice> choices = The.Library.PublicationsLike(Book.ItsObjectClass, 
   "%Hopeless%", null, null, "Jones") // title, isbn, forename, surname;
foreach (Choice choice in choices)
   if (choice.Value1.ToLower().Contains("quest")) // Value1 contains title
      return (Book)choice.Target;

Distributed collections

A collection without the distributed modifier in its definition must contain objects from a single database, which must be the database of it parent object.  If you include the distributed modifier, elements can be stored in different databases.  Here is an example of how you store and then retrieve distributed objects.  This does not come from the Library sample, as that doesn't have any distributed collections.  It assumes that the Company.Departments collection is distributed.

Department sales = new Department("Sales")
sales.StoreInDatabase(The.Domain.Databases["SalesDatabase"]);
The.Company.Departments.Add(sales);
 
Department accounts = new Department("Accounts")
accounts.StoreInDatabase(The.Domain.Databases["AccountsDatabase"]);
The.Company.Departments.Add(accounts);
 
Department production = new Department("Production")
production.StoreInDatabase(The.Domain.Databases["ProductionDatabase"]);
The.Company.Departments.Add(production);
 
...
 
IndexedList<Department> departments = The.Company.Departments;
departments["Sales"].DoMonthEndProcessing();      // got from SalesDatabase
departments["Accounts"].DoMonthEndProcessing();   // got from AccountsDatabase
departments["Production"].DoMonthEndProcessing(); // got from ProductionDatabase

Caution

The above requires that the databases you use be able to handle distributed transactions.  For now, this limits you to SQL Server.  Persistate also supports MySQL, but unfortunately its .Net connector does not support distributed transactions.