Click here to Skip to main content
Licence 
First Posted 2 Mar 2004
Views 60,810
Downloads 794
Bookmarked 27 times

An introduction to a post-relational database for .NET, Matisse - Part 3

By John Sasak | 25 Mar 2004
Step by step guide for .NET programming with a Post-Relational Database
1 vote, 3.8%
1

2
2 votes, 7.7%
3
2 votes, 7.7%
4
21 votes, 80.8%
5
4.77/5 - 26 votes
1 removed
μ 4.38, σa 1.65 [?]

Introduction

In the first article of this series, I gave a quick overview of the Matisse post-relational database and showed how to install it. In the second one, I covered the basics of schema definition for the post-relational database. In this third article, I will demonstrate how to write .NET programs that access a Matisse database, using the schema defined in the second article's appendix.

Although Matisse works well with ADO.NET, I find it easier and more maintainable to use "object APIs" to insert objects into a database instead of ADO.NET. This article focuses on how to create and store objects in a database using the "object APIs", and the next article will show how to insert or retrieve objects using ADO.NET.

Installation

If you have not installed Matisse yet, get the two files from the download page:

  • Intel - MS Windows (file name is matisse70x.exe)
  • .NET binding (file name is matisseDotNet70x.exe)

And, install matisse70x.exe first, then matisseDotNet70x.exe. The destination folders for both installations need to be the same (C:\Program Files\Matisse by default). Then, you need to install the .NET assembly file MatisseNet.dll into GAC (Global Assembly Cache). The file is located in C:\Program Files\Matisse\bin by default. You can simply drag and drop the file into the GAC folder or use the gacutil command:

  > gacutil /i "C:\Program Files\Matisse\bin\MatisseNet.dll"

The .NET binding installation comes with several demo applications, which are accessible from the menu.

Sample screenshot

There are two documents for the .NET binding:

Code Generation

After you have defined a database schema, the first step you need to take is to generate stub classes from the database using the mt_stbgen command line utility. The generation is quite simple. You do not need to specify any mapping rules like object-relational mapping. Execute the mt_stbgen utility with your hostname, database, and an option to specify which language (C# or VB) you want to use:

  > mt_stbgen your_host your_db C#

This will generate four C# classes (Project, Employee, Manager, and Task) in the folder you executed the utility. These classes contain methods and properties that map to attributes and relationships in the database schema, and provide access to the database.

There are a few options you can specify with the utility. One of them is '-p'. With '-p', you can specify a namespace in which stub classes are defined. For instance, the next command generates stub classes in the namespace MatisseApp.

  > mt_stbgen your_host your_db C# -p MatisseApp

A part of a generated stub class looks like this:

namespace MatisseApp {
    using System;
    using com.matisse.reflect;
    using com.matisse.Data;
    using com.matisse;

    //  A class generated by Matisse
    /// <summary>
    /// Manager is a schema class generated by Matisse.
    /// </summary>
    public class Manager : Employee {

 
        //  Generated constructor
        /// <summary>
        /// Default constructor provided as an example.
        /// You may modify or delete this constructor
        /// </summary>
        public Manager(MtDatabase db) : 
                base(GetClass(db)) {
        }

For more information about the mt_stbgen utility, run mt_stbgen without any parameter, or refer to the ‘Code Generation and Data Classes’ section in the Matisse .NET Programming Guide

Create a New Project

For this article, I created a new solution with two projects: an application project DemoApp and a class library PersistentClasses for the generated stub classes. The projects look like this:

Sample screenshot

In order to add MatisseNet.dll to References, right-click on References to select the Add Reference… menu, then click the Browse… button at the top-right corner and select MatisseNet.dll located in C:\Program Files\Matisse\bin by default.

Store Objects into Database

Now, we are ready to create .NET objects and store them into a database. The next program creates two Employee objects and a Manager object:

    static void Main(string[] args)
    {
      MtDatabase dbcon;
      // --(A), see below for notes
      dbcon = new MtDatabase("localhost", "example", 
        new MtPackageObjectFactory("MatisseApp,PersistentClasses")); 
      dbcon.Open(); // connect to the database
      dbcon.BeginTransaction(); // start a transaction
 
      // create an Employee object
      Employee emp1  = new Employee(dbcon); // --(B)
      emp1.Name      = "John Venus";
      emp1.BirthDate = new DateTime(1955, 10, 1);
 
      // create another Employee object
      Employee emp2  = new Employee(dbcon); // --(C)
      emp2.Name      = "Amy Mars";
      emp2.BirthDate = new DateTime(1965, 9, 25);
 
      // create a Manager object
      Manager mgr   = new Manager(dbcon);   // --(D)
      mgr.Name      = "Ken Jupiter";
      mgr.BirthDate = new DateTime(1952, 12, 15);
      mgr.Title     = "Director";

A couple of explanations about this program:

Line (A) creates an MtDatabase object that represents a database connection to a Matisse database. The third parameter of the MtDatabase constructor is required by the Matisse .NET binding so that it can find the stub classes, such as Employee, in the namespace MatisseApp in the assembly named PersistentClasses.

Lines (B), (C), and (D) are creating objects that will be stored in the database when the transaction is committed. They are using the default constructors generated by the mt_stbgen utility, which take a MtDatabase object as its parameter to specify to which database the object be stored.

Set Relationships between Objects

The next segment of code is the continuing part of the above program. It creates a Project object, and assigns the above Manager object and two Employee objects to the project as the manager and members, respectively.

      Employee[] members = new Employee[2] {emp1, emp2};
 
      // create a Project object, assign the two employees to the
      // project, and set the manager for the project
      Project prj = new Project(dbcon);
      prj.ProjectName = "Whidbey";
      prj.ManagedBy   = mgr;     // --(E)
      prj.Members     = members; // --(F)
 
      dbcon.Commit(); // commit the transaction
      dbcon.Close();  // disconnect from the database

Instead of managing Primary Key – Foreign Key (with an intermediate table), you simply assign the Manager and Employee objects to the project object’s properties (line (E) and (F)).

After you successfully compile the Visual Studio .NET project and run it, execute an SQL query to select all the employees. You will see the result like this:

Sample screenshot

You can see each employee that participates in the “Whidbey” project.

Summary and Next Article

This article demonstrated how to generate stub classes (persistent classes) from a database, and how to store .NET objects in the database.

The next article will show how to retrieve objects from a database into your .NET application using ADO.NET and/or object-oriented APIs as well as how to insert objects using ADO.NET.

<< 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. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMatisse and Far Cache PinmemberFrankSz2:16 17 Sep '08  
GeneralMatisse database forum PinsussAnonymous5:18 19 Dec '04  
GeneralVery interesting... PinmemberVerdant12317:20 6 Mar '04  
GeneralRe: Very interesting... Pinmemberthecaptain8:13 26 Mar '04  
GeneralRe: Very interesting... PinmemberGiles23:57 26 Mar '04  
GeneralNot THAT expensive really! Pinmemberthudriact16:15 21 Aug '05  
If the price mentionned is real (17k$/cpu) is real, then it may sound very expensive, but do have a look at ms sql sever/ibm db2/oracle licensing - it may very well cost a LOT more per CPU (several times this price).
 
IIRC, the prices (for full blown, enterprise versions with all bells and whistles: OLAP, data mining, etc) are approximately:
 
ms sql: 20k$/cpu
ibm db2: 120k$/cpu (the data mining alone costs 60k$)
oracle: 96k$/cpu (and dual core CPUs count as 2 cpus and so on!)
 
Then you start adding CAL's and such... The featuresets / performace aren't quite the same either... Either ways, matisse doesn't seems as expensive now that it's put in context does it? Of course, it's still a large amount to justify, and you would have to make significant use of it to justify the cost, especially if you already have mssql/db2/ora licenses (and that the server can handle the extra load).
 
I've used oracle, but never in a production environment (too f'in expensive!). DB2 doesn't seem extremely popular lately (especially compared to mssql in windows-only shops). Most of the time ms sql is adequate for the job (features wise and very high performance too). Oracle seems marginally bettr, but at several times the cost... I just discovered Matisse, so I cannot say too much about it yet. I doubt I'll see it in a production environment though - especially since we tend to stick with the names we trust.
 
Also, we have to keep in mind that initial cost (incl. CALs) != TCO (total cost of ownership)...
 
[Not actually my user account - using BugMeNot, sorry...]

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

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