Make your own free website on Tripod.com

Microsoft Object Spaces
Building a Banking Application using Object Spaces

The Bank Teller Object Space Application

Gopalan Suresh Raj

Note
To work with any of these samples, you will need the following:
.........................................Microsoft .NET SDK
.........................................
The objectspaces.msi package.
.........................................ObjectSpaces (
Beta) Installation.

The Microsoft ObjectSpaces framework consists of a set of a classes and interfaces grouped in a namespace called Microsoft.ObjectSpaces (not available in current .NET Framework) that provide an object-oriented mapping layer to access data. This framework builds on the classes of the .NET Framework, particularly those in the System.Xml and System.Data namespaces, to provide access to both XML data sources and relational data sources.

Let us see how to create an ObjectSpaces Application by implementing a complete Banking Application with a Teller client invoking operations on an Account Business Entity object.

1. Create the Business Entity persistent class (Account.cs)

2. Create the XML File used by the ObjectSpaces framework (Source.xml)

3. Create the ObjectSpaces Client Application (Teller.cs)

Let us see how this is done in steps:

  Create the Business Entity persistent class (Account.cs)

In the ObjectSpaces framework, defining the object is accomplished by creating an abstract persistent class that defines the properties, methods and events available to a client (Teller in this example). Create the Account.cs Business Entity persistent class and compile it to a DLL.

  Create the XML File used by the ObjectSpaces framework (Source.xml)

Create an XML file which will be used by the ObjectSpace framework. This XML file (Source.xml) is used to provide location of the file used to store the XML database file. The db.xml file will be used to store the object (the file will be created when the first instance of Account is stored). Create the Source.xml file as below.

Source.xml
1:
2:
3:
 
<objectspace type="Microsoft.ObjectSpaces.XmlObjectSpace" xmlns="http://www.microsoft.com/ObjectSpaces-v1">
<arg>db.xml</arg>
</objectspace>

 

  Create the ObjectSpaces Client Application (Teller.cs)

Create the Teller.cs as shown below.

Teller.cs
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
//////////////////////////////////////////////////////
/// The following example illustrates an Object Spaces example
/// application developed using C# and the .NET Framework.
///
/// author: Gopalan Suresh Raj
/// Copyrights (c), 2002. All Rights Reserved.
/// URL: http://gsraj.tripod.com/
/// email: gopalan@gmx.net
///
/// Here's how to build the assemply
/// <compile>
/// C:\WINNT\Microsoft.NET\Framework\v1.0.3705\csc Teller.cs /r:system.dll /r:Microsoft.ObjectSpaces.dll /r:Account.dll
/// </compile>
///
//////////////////////////////////////////////////////

using System;
using Microsoft.ObjectSpaces;

namespace Bank {
  /// <summary>
  /// Summary description for Teller.
  /// </summary>
  public class Teller {

    IObjectSpace objectSpaces_;

    public Teller(String sourceFile) {
      try {
        // Create an instance of an interface to the ObjectSpace.
        this.objectSpaces_ = ObjectSpaceFactory.CreateObjectSpace(sourceFile);
      }
      catch (Exception exception) {
        // Handle any exceptions.
        Console.WriteLine(exception);
      }
    }

    /**
     * Create Checking or Savings Account
     */

    public void createAccount(string accountNo,
      String customerNames,
      int startingBalance,
      int accountType) {
      Account account = null;

      ////////////////////////////////////
      // Perform  ObjectSpaces operations
      ////////////////////////////////////
      try {
        account = (Account) this.objectSpaces_.CreateObject(typeof(Account),
          accountNo, customerNames, accountType, startingBalance);
        // Commit the changes
        this.objectSpaces_.BeginTransaction();
        this.objectSpaces_.Update(account);
        this.objectSpaces_.CommitTransaction();
        Console.WriteLine(account.ToString()+"\nSuccessfully created.");
      } catch(Exception exception) {
        Console.WriteLine(exception);
        if(account != null) {
          this.objectSpaces_.RollbackTransaction();
        }
      }
    }

    /**
     * Delete and close the account
     */

    public void deleteAccount(string accountNumber) {

      Account account = null;
      string filter = "AccountNo='"+accountNumber+"'";

      ////////////////////////////////////
      // Perform  ObjectSpaces operations
      ////////////////////////////////////
      try {
        account = (Account) this.objectSpaces_.GetObject(typeof(Account), filter);
        if(account != null) {
          // Invoke Delete Persistent on the object
          this.objectSpaces_.DeleteObject(account);
        }
        // Commit the changes
        this.objectSpaces_.BeginTransaction();
        this.objectSpaces_.Update(account);
        this.objectSpaces_.CommitTransaction();
        Console.WriteLine("Successfully deleted Account No: "+accountNumber);
      } catch(Exception exception) {
        Console.WriteLine(exception);
        if(account != null) {
          this.objectSpaces_.RollbackTransaction();
        }
      }
    }

    /**
     * Transfer Money between Accounts
     */

    public void transferMoney(string fromAccountNo, string toAccountNo, int amount) {
      Account fromAccount = null;
      Account toAccount = null;
      string fromFilter = "AccountNo='"+fromAccountNo+"'";
      string toFilter = "AccountNo='"+toAccountNo+"'";

      ////////////////////////////////////
      // Perform  ObjectSpaces operations
      ////////////////////////////////////
      fromAccount = (Account) this.objectSpaces_.GetObject(typeof(Account), fromFilter);
      toAccount = (Account) this.objectSpaces_.GetObject(typeof(Account), toFilter);

      try {
        if((fromAccount != null) && (toAccount != null) ) {
          if(amount < fromAccount.Balance) {
            this.objectSpaces_.BeginTransaction();
            fromAccount.debit(amount);
            toAccount.credit(amount);
            // Commit the changes
            this.objectSpaces_.UpdateAll();
            this.objectSpaces_.CommitTransaction();
            Console.WriteLine("Successfully transfered $"+amount+
              " from Account No: "+fromAccountNo+
              " to Account No: "+toAccountNo);
          }
        }
      }
      catch(Exception exception) {
        Console.WriteLine(exception);
        if((fromAccount != null) || (toAccount != null)) {
          this.objectSpaces_.RollbackTransaction();
        }
      }
    }

    /**
     * List Account Details
     */

    public void listAccounts(string filter) {
                          
      ////////////////////////////////////
      // Perform  ObjectSpaces operations
      ////////////////////////////////////
      // Iterate through the list of Accounts.
      foreach(Account accounts in this.objectSpaces_.GetObjects(typeof(Account), filter)) {
        Console.WriteLine(accounts.ToString());
      }
    }

  }

  /**
   * Main Entry Point to the Object Space Application
   */

  public class Bank {
    public static void Main(string[] args) {

      // DbSource.xml is the default source file.
      string sourceFile = "Source.xml";

      // If an argument is supplied (for example, Bank.exe 'MyFile.xml'), use that argument
      // as the source file.
      if (args.Length > 0) {
        sourceFile = args[0];
      }
      Teller teller = new Teller(sourceFile);
      // Create Checking Accounts
      teller.createAccount("C1000", "Gopalan Suresh Raj", 1000000, Account.CHECKING_ACCOUNT_);
      teller.createAccount("C1001", "Athul Suresh Raj", 1000000, Account.CHECKING_ACCOUNT_);

      // Create Savings Accounts
      teller.createAccount("S1000", "Gopalan Suresh Raj", 1000000, Account.SAVINGS_ACCOUNT_);
      teller.createAccount("S1001", "Athul Suresh Raj", 1000000, Account.SAVINGS_ACCOUNT_);

      // List Checking Account Details
      string filter = "AccountType="+Account.CHECKING_ACCOUNT_;
      Console.WriteLine("\nListing All Checking Accounts\n-----------------------------");
      teller.listAccounts(filter);
      Console.WriteLine("-----------------------------");

      // List Savings Account Details
      filter = "AccountType="+Account.SAVINGS_ACCOUNT_;
      Console.WriteLine("\nListing All Savings Accounts\n----------------------------");
      teller.listAccounts(filter);
      Console.WriteLine("----------------------------");

      // Delete some Accounts
      teller.deleteAccount("C1000");
      teller.deleteAccount("S1001");

      // Perform Transfer Money operations on some accounts
      teller.transferMoney("S1000", "C1001", 10000);

      // List all Account Details
      filter = "";
      Console.WriteLine("\nListing All Accounts\n--------------------");
      teller.listAccounts(filter);
      Console.WriteLine("--------------------");

    }
  }
}

First, a reference to the IObjectSpace instance is retreived as per the information metioned in Source.xml (Line 31) .

You can create new rows in a datastore using ObjectSpaces. This is done using the CreateObject method of the IObjectSpace interface. The code above (on lines 52-53)  creates a new Account and populates its properties before calling the Update method to persist the object in the data store (see line 56).

The ObjectSpace framework also provides standard transaction management methods to enable you to include operations that perform actions on the underlying datastore in a transaction. Those changes can then be rolled back or committed depending on how you construct your business logic. Lines 55-63 demonstrate how to handle transaction management when using ObjectSpaces.

A client can easily query a single Account using the IObjectSpace's GetObject method as shown on Line 79. You'll notice that a filter of type the string "AccountNo = C1001" is passed as the second argument. This syntax is referred to as OPath, a derivative of XPath that Microsoft developed specifically for the ObjectSpaces framework. Behind the scenes, the ObjectSpaces framework uses the combination of the OPath query and the mapping and connect files to connect to the datastore and formulate a SELECT statement to retrieve the Account identified with the id of C1001. The data is then retrieved from the datastore, loaded into a DataSet, and mapped into the account object.

You can also delete existing rows in a datastore using ObjectSpaces. This is done using the DeleteObject method of the IObjectSpace interface. The code above (on line 82)  deletes an existing Account and calls the Update method to persist the changes to the data store (see line 86).

Similarly, multiple objects can be queried using the GetObjects() method of IObjectSpace as illustrated on line 144.

Compile the File into an Application EXE.

Command Prompt
C:\MyProjects\ObjectSpaces\Bank>C:\WINNT\Microsoft.NET\Framework\v1.0.3705\csc Teller.cs /r:system.dll /r:Microsoft.ObjectSpaces.dll /r:Account.dll
Microsoft (R) Visual C# .NET Compiler version 7.00.9466
for Microsoft (R) .NET Framework version 1.0.3705
Copyright (C) Microsoft Corporation 2001. All rights reserved.


C:\MyProjects\ObjectSpaces\Bank>

Run the Object Spaces Application.

Command Prompt
C:\MyProjects\ObjectSpaces\Bank>Teller

Account Number: C1000
CHECKING_ACCOUNT
Customer Name : Gopalan Suresh Raj
Account Opening Date : Mon, 19 Aug 2002 00:49:13 GMT
Balance (in USD) : $1,000,000.00

Successfully created.

Account Number: C1001
CHECKING_ACCOUNT
Customer Name : Athul Suresh Raj
Account Opening Date : Mon, 19 Aug 2002 00:49:13 GMT
Balance (in USD) : $1,000,000.00

Successfully created.

Account Number: S1000
SAVINGS_ACCOUNT
Customer Name : Gopalan Suresh Raj
Account Opening Date : Mon, 19 Aug 2002 00:49:13 GMT
Balance (in USD) : $1,000,000.00

Successfully created.

Account Number: S1001
SAVINGS_ACCOUNT
Customer Name : Athul Suresh Raj
Account Opening Date : Mon, 19 Aug 2002 00:49:13 GMT
Balance (in USD) : $1,000,000.00

Successfully created.

Listing All Checking Accounts
-----------------------------

Account Number: C1000
CHECKING_ACCOUNT
Customer Name : Gopalan Suresh Raj
Account Opening Date : Mon, 19 Aug 2002 00:49:13 GMT
Balance (in USD) : $1,000,000.00


Account Number: C1001
CHECKING_ACCOUNT
Customer Name : Athul Suresh Raj
Account Opening Date : Mon, 19 Aug 2002 00:49:13 GMT
Balance (in USD) : $1,000,000.00

-----------------------------

Listing All Savings Accounts
----------------------------

Account Number: S1000
SAVINGS_ACCOUNT
Customer Name : Gopalan Suresh Raj
Account Opening Date : Mon, 19 Aug 2002 00:49:13 GMT
Balance (in USD) : $1,000,000.00


Account Number: S1001
SAVINGS_ACCOUNT
Customer Name : Athul Suresh Raj
Account Opening Date : Mon, 19 Aug 2002 00:49:13 GMT
Balance (in USD) : $1,000,000.00

----------------------------
Successfully deleted Account No: C1000
Successfully deleted Account No: S1001
Successfully transfered $10000 from Account No: S1000 to Account No: C1001

Listing All Accounts
--------------------

Account Number: C1001
CHECKING_ACCOUNT
Customer Name : Athul Suresh Raj
Account Opening Date : Mon, 19 Aug 2002 00:49:13 GMT
Balance (in USD) : $1,010,000.00


Account Number: S1000
SAVINGS_ACCOUNT
Customer Name : Gopalan Suresh Raj
Account Opening Date : Mon, 19 Aug 2002 00:49:13 GMT
Balance (in USD) : $990,000.00

--------------------

C:\MyProjects\ObjectSpaces\Bank>
 

In the above example, persistence is handled by ObjectSpace framework. For more complex data (say from multiple tables) you can handle the persistance yourself by deriving a class from the ObjectCustomizer abstract class and by implementing methods such as CreateRow, GetRow, GetRows, InsertRows, UpdateRows, DeleteRows, GetChildRows, and GetParentRow. The class is then referenced in the type element of the mapping file so that the framework can instantiate it and call its methods when appropriate.

What is Microsoft Object Spaces
Building the Account Business Entity Class - Account.cs
Building a complete Object Spaces Application that makes use of the Account Class - The Bank Teller Application

 

Download the entire source code as a zip file.

 

click here to go to
My Advanced C#/.NET Tutorial Page...

About the Author...
Gopalan Suresh Raj is a Software Architect, Developer and an active Author. He has co-authored a number of books including "Professional JMS", "Enterprise Java Computing-Applications and Architecture" and "The Awesome Power of JavaBeans". His expertise spans enterprise component architectures and distributed object computing. Visit him at his Web Cornucopia site (http://gsraj.tripod.com/) or mail him at gopalan@gmx.net.

 


Go to the Component Engineering Cornucopia page

This site was developed and is maintained by Gopalan Suresh Raj

This page has been visited times since December 31, 2001.

Last Updated : Dec 31, '01

If you have any questions, comments, or problems regarding this site, please write to me I would love to hear from you.


Copyright (c) 1997-2001, Gopalan Suresh Raj - All rights reserved. Terms of use.

All products and companies mentioned at this site are trademarks of their respective owners.