Click here to Skip to main content
Click here to Skip to main content

An introduction to a post-relational database for .NET : Matisse - Part 5

By , 29 Mar 2004
 

Introduction

Part 3 and Part 4 in this series of articles introduced .NET programming with the Matisse post-relational database; inserting objects using the "Object APIs" and retrieving objects from the database using ADO.NET and SQL without using mapping tools, respectively. In this article, I will show how to use the Object APIs for updating and deleting objects, looking up objects using an index, and for relationship navigation.

The Object APIs provide object-oriented interfaces to manipulate Matisse objects. The APIs are generated by the mt_stbgen code generation tool. When should we use the Object APIs (or ADO.NET)? Here are my answers:

  • When you use data-aware controls, you need to use ADO.NET.
  • Use the Object APIs for creation, update, and deletion of objects. You may also use SQL stored procedures for that purpose. I will talk about this in an upcoming article.
  • Use ADO.NET for retrieving objects. However, if you can use an index to look up objects, use the Object APIs for indexes for better performance.
  • If you can navigate from an object to another, use the Object APIs for navigation. It is much faster than issuing another SQL query to get the same object.

Most of the time, the Object APIs perform better than ADO.NET. Also, the Object APIs minimize the risk of errors in your program because a lot of things can be checked at compile time, not at run time.

Here is the list of previous articles:

  • Part 1: Overview of Matisse
  • Part 2: Schema definition
  • Part 3: Using .NET programming to insert objects
  • Part 4: Using ADO.NET to retrieve objects

Updating Attributes

The following code segment is part of the class Project that was generated by the mt_stbgen code generation tool. (I keep using the same database schema as the one I have been using in previous articles) No need to focus on the details of the code, just note that it defines a C# property.

namespace MatisseApp {
    using System;
    using com.matisse.reflect;
    using com.matisse.Data;
    using com.matisse;  
   
    //  A class generated by Matisse
    /// <summary>
    /// Project is a schema class generated by Matisse.
    /// </summary>
    public class Project : MtObject {
 
        //  Generated constructor
        /// <summary>
        /// Default constructor provided as an example.  
        /// You may modify or delete this constructor
        /// </summary>
        public Project(MtDatabase db) : 
                base(GetClass(db)) {
        }   
      
        //  Generated property, do not modify
        /// <summary>
        /// Property for attribute 'Budget'
        /// </summary>
        public System.Decimal Budget {
            get {
                return GetNumeric(GetBudgetAttribute(Database));
            }
            set {
                SetNumeric(GetBudgetAttribute(Database), value);
            }
        }

Since the class Project has been defined with an attribute Budget of type decimal in the database, the generated C# class has the property Budget as shown above. When you update an attribute value of a Matisse object, you simply use the set accessor of the property:

    Project prj;
 
    // Retrieve a Project object from the database and assign it to prj
    // Update the value of the project's budget
    prj.Budget = prj.Budget + 200000;


Relationship Navigation and Update

One of the big advantages of using a post-relational database is that you can navigate from object to object effortlessly, e.g., get a Manager object from a Project object. The next code is part of the class Project that defines properties to access two relationships (Members and ManagedBy). Again, let us not focus on the details of the code, but please notice that it defines two C# properties.

        //  Generated property, do not modify
        /// <summary>
        /// Property 'Members': Gets and sets the 'Members' successors
        /// </summary>
        public Employee[] Members {
            get {
                return ((Employee[])(GetSuccessors(
GetMembersRelationship(Database), typeof(Employee))));
            }
            set {
                SetSuccessors(GetMembersRelationship(Database), value);
            }
        }
 
        //  Generated property, do not modify
        /// <summary>
        /// Property 'ManagedBy': Gets the 'ManagedBy' successor
        /// </summary>
        public Manager ManagedBy {
            get {
                return ((Manager)(GetSuccessor(
GetManagedByRelationship(Database))));
            }
            set {
                SetSuccessor(GetManagedByRelationship(Database), value);
            }
        }

The following program retrieves a manager and members of a project, and then set other manager and members for the project:

    Project prj;
    // Retrieve a Project object from the database and assign it to prj
    Manager mgr = prj.ManagedBy;
    Employee[] members = prj.Members;
 
    // Update the relationships
    Manager otherManager;
    Employee[] otherMembers;
    prj.ManagedBy = otherManager;
    prj.Members = otherMembers;

Other APIs for Relationships

There are a few more APIs to access relationships generated by the code generator. The list below is the methods generated for the Members relationship of the class Project.

APIs for the relationship Members

Description

MembersSize Returns the number of objects (successors) in the relationship, i.e., the number of members of the project
MembersEnumerator Returns an enumerator to iterate over the objects in the relationship
PrependMembers Add new member(s) to the relationship at the beginning
AppendMembers Add new member(s) to the relationship at the end
RemoveMembers Remove member(s)
ClearMembers Remove all the members in the relationship

Looking up Objects with an Index

The Matisse SQL optimizer utilizes indexes to optimize query performance, but you may also use the index APIs generated by the code generator. The index APIs perform slightly faster than dynamic SQL because they do not require SQL statement compilation, and your program becomes simpler. However, you can use the APIs only when your search criteria are contained in a single index; otherwise, you need to use SQL queries.

Suppose you have defined an index ProjNameIndex on the ProjectName attribute. The next line of code returns a Project object whose name is "Whidbey":

    Project prj = Project.LookupProjNameIndex (dbconn, "Whidbey");

This is simple. Note that there are also other APIs that allow you to do range query.

Full-Text Search

Matisse has a full-text indexing feature, which is called "Entry point dictionary". For example, if you need to perform full-text search on the project description attribute, define an entry point dictionary as follows:

CREATE ENTRY_POINT DICTIONARY proj_desc_ep_dict
  ON Project (Description)
  MAKE_ENTRY "make-full-text-entry";

The next code retrieves all the projects whose description contains the work ".NET":

    foreach (Project prj in Project.Proj_desc_ep_dictEnumerator(
        dbconn, ".NET") {
        // ...
    }

Summary and Next Article

This article showed the basics of database programming with Matisse using the "Object APIs". Although almost anything can be done by using either ADO.NET or Object APIs, ADO.NET is usually better suited for querying while the Object APIs are more compact and efficient for the rest.

In my next article, I will show a few examples of ASP.NET programming with Matisse.

<< Back | Next >>

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

John Sasak
Web Developer
United States United States
Member
John is a software consultant and currently working for a large .NET project. He has an extensive experience in object-oriented technologies for more than 15 years ranging from Smalltalk, C++, Java, .NET to databases.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralError with .NET 1.0sussJacob Buys12 Jun '04 - 9:10 
Hi, first off, thanks for this great article. I have been looking into alternative database systems and have found your articles of great value. However, I could not get the data modification samples to work on my machine. (I am still using VS.net 2002 But I do have .NET Framework 1.1 installed [I'm on a low budget I'm afraid]. I am using Matisse 7.0.5)
 
The problem is that for some mysterious reason, the sample in part 4 throws a null-reference exception in "Unknown Module", and then informs me that there is no source code for the current code location, even though the earlier samples work. (I am assuming that this means the error is somewhere in the Matisse module). I have been able to figure out that this happens upon execution of the myCmd.CommandText assignment each case (see part 4 of the series). I have checked that all the relevant objects are instantiated properly, and they are.
 
This is really baffling me and I would appreciate any help, as I am quite eager to start working with Matisse.
 
Thanks in advance,
 
Jacob Buys jacob@prioritymail.co.za

 
Errors brought to you all the way from sunny South Africa, courtesy of Microsoft Airlines. Wink | ;)
GeneralRe: Error with .NET 1.0memberJohn Sasak14 Jun '04 - 8:18 
Matisse requires .NET Framework 1.1.
 
Even if you have .NET Framework 1.1 installed, VS.NET 2002 still keeps using .NET Framework 1.0.
Look at http://www.eggheadcafe.com/articles/20030412.asp, for example.
(By the way, when I upgraded VS.NET 2002 to 2003 last year, it was about $30, I think)
 
There may be a trick to use .NET Framework 1.1 with VS.NET 2002, but I don't know...
GeneralScripting LanguagememberGiles Bradshaw12 May '04 - 14:00 
Can you program objects within Matisse - ie does it have its own object scripting language?
 

MOO!!


GeneralRe: Scripting LanguagememberJohn Sasak13 May '04 - 8:09 
Yes, Matisse has SQL PSM, which is the standard defined in SQL-99. With it, you can program objects within Matisse.

GeneralImporting XSD SchemasmemberGiles Bradshaw12 May '04 - 13:53 
I've been playing with Cache from intersystems. One great feature is that you can import complex XSD schemas into the database. This means that you can then serve the data as a web service which conforms to the XSD schemas. Does Matisse have a feature like this?
 

MOO!!


GeneralRe: Importing XSD SchemasmemberJohn Sasak13 May '04 - 8:16 
As far as I know, Matisse does not have that feature.
But, Matisse may have a plan to implement it. Try to contact the company.
GeneralPart 6membermarkmoss13 Apr '04 - 9:29 
John
 

When will you get to Part 6 ????????????
 
By the way these articles have been great.
 
Smile | :)
 
Mark Moss
GeneralRe: Part 6memberJohn Sasak13 Apr '04 - 12:25 
Thank you for your words, Mark.
I'm working on Part 6...
 
John
GeneralRe: Part 6memberJohn Sasak3 May '04 - 10:45 
Hi Mark,
 
The article #6 is now up.
Thank you for waiting.
 
I hope you will enjoy it.
 
John
QuestionHow about performancememberYuancai (Charlie) Ye12 Apr '04 - 4:53 
Hi, John:
Your articles are attractive to me, and open my eyes to see handling databases in different ways. I have one concern about post-relational database. What about its performance in comparison with classical relational database management systems. Do you have plans to talk about its performance.
Thanks a lot.
 
Regards,
 
Yuancai (Charlie) Ye
AnswerRe: How about performancememberJohn Sasak12 Apr '04 - 8:10 
I'm happy to hear that you find the articles eye-opening to the new database technologies.
 
Regarding the preformance, the post-relational database is as good as relational databases or better than relational databases. It depends.
For example, if you execute a simple query like: SELECT * FROM Employees WHERE Name = 'Foo', you don't see any difference in performance. But, if you execute a query that involves more than 2 tables (with relational), you will see a big performance benefit (more than 10x) with the post-relational database because the post-relational can use navigation instead of costly join operations.
GeneralRe: How about performancememberAlois Kraus22 Apr '04 - 2:40 
There are some vendors of object oriented databases around (e.g. Versant DB). It has turned out that the theoretical benefit of object oriented databases are in real world applications neglible because the object model will become very complex in a big application. This is the cause of poor performance when searching and updating the DB. The search capabilities in object oriented DBs are also rather limited.
GeneralRe: How about performancememberJohn Sasak26 Apr '04 - 14:05 
I am talking about Matisse, not Versant or other object databases.
Are your statements based on your experience with Versant?

GeneralRe: How about performancememberAza26 Apr '04 - 22:37 
The Versant and similar object databases have trouble with performance because of roundtrips to the database on open objects. Is this a situation with Matisse?
 
Johannes

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 30 Mar 2004
Article Copyright 2004 by John Sasak
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid