Microsoft Object Spaces
Building a Banking
Application using Object Spaces
The Bank Teller Object Space Application
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 | ||
|
Create the ObjectSpaces Client Application (Teller.cs)
Create the Teller.cs as shown below.
Teller.cs | ||
|
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 (https://gsraj.tripod.com/) or mail him at gopalan@gmx.net. |
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 |
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. |