Microsoft Object Spaces
Building a Banking Application using Object Spaces

The Account Business Entity Class

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.

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. Given below is a persistent class for a Bank Account object written in C#.

Account.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:
//////////////////////////////////////////////////////
/// 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: https://gsraj.tripod.com/
/// email: gopalan@gmx.net

///
/// Here's how to build the assemply
/// <compile>
/// C:\WINNT\Microsoft.NET\Framework\v1.0.3705\csc /t:library /out:Account.dll Account.cs /r:system.dll /r:Microsoft.ObjectSpaces.dll
/// </compile>
///
//////////////////////////////////////////////////////

using System;
using Microsoft.ObjectSpaces;

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

    /** Attribute   INVALID_ACCOUNT_ */
    public static int INVALID_ACCOUNT_  = -1  ;


    /** Attribute   CHECKING_ACCOUNT_ */
    public static int CHECKING_ACCOUNT_  = 100  ;


    /** Attribute   SAVINGS_ACCOUNT_ */
    public static int SAVINGS_ACCOUNT_  = 200  ;


    /** Attribute   accountNo_ */
    [UniqueId] public abstract string AccountNo { get; set; }

    /** Attribute   accountType_ */
    public abstract int AccountType { get; set; }


    /** Attribute   customerNames_ */
    public abstract string CustomerNames { get; set; }


    /** Attribute   balance_ */
    public abstract int Balance { get; set; }


    /** Attribute   openedOn_ */
    public abstract DateTime OpenedOn { get; set; }

    /** Attribute   interestRate */
    public abstract int InterestRate { get; set; }

    //
    // Define the remaining properties and constructor method.
    //
    /** Override standard Object Space Event to set values at object creation time */
    public virtual void OnCreate(string newAccountNo,
      string newNames,
      int accountType,
      int startingBalance) {
      AccountType = accountType;
      AccountNo = newAccountNo;
      CustomerNames = newNames;
      Balance = startingBalance;
      OpenedOn = DateTime.Now;
    }

    /** Credit Amount into the Account */
    public int credit(int amount) {
      ///////////////////////////////////////////
      //// Start of User's Business Logic    ////
      ///////////////////////////////////////////
      if(AccountType == INVALID_ACCOUNT_) {
        throw new Exception("Invalid Account Type :"+AccountType);
      }
      if(amount < 0) {
        throw new Exception("Invalid Amount (in cents) :"+amount);
      }
      Balance += amount;
      return Balance;
      ///////////////////////////////////////////
      ////   End of User's Business Logic    ////
      ///////////////////////////////////////////    
    }

    /** Debit Amount from the Account */
    public int debit(int amount) {
      ///////////////////////////////////////////
      //// Start of User's Business Logic    ////
      ///////////////////////////////////////////
      if(AccountType == INVALID_ACCOUNT_) {
        throw new Exception("Invalid Account Type :"+AccountType);
      }
      if(amount < 0) {
        throw new Exception("Invalid Amount (in cents) :"+amount);
      }
      Balance -= amount;
      return Balance;
      ///////////////////////////////////////////
      ////   End of User's Business Logic    ////
      ///////////////////////////////////////////    
    }

    /** Calculate Interest for the Account */
    public int  calculateInterest() {
      ///////////////////////////////////////////
      //// Start of User's Business Logic    ////
      ///////////////////////////////////////////
      if(AccountType != Account.SAVINGS_ACCOUNT_) {
        throw new Exception("Invalid Account Type :"+AccountType);
      }
      return Balance*InterestRate/100;
      ///////////////////////////////////////////
      ////   End of User's Business Logic    ////
      ///////////////////////////////////////////  
    }

    /** Override the ToString method */
    public override string ToString() {
      string result = String.Format( "\nAccount Number: {0}\n", this.AccountNo );
      if(this.AccountType == Account.CHECKING_ACCOUNT_) {
        result += String.Format( "CHECKING_ACCOUNT\n" );
      }
      if(this.AccountType == Account.SAVINGS_ACCOUNT_) {
        result += String.Format( "SAVINGS_ACCOUNT\n" );
      }
      result += String.Format( "Customer Name : {0}\n", this.CustomerNames );
      result += String.Format( "Account Opening Date : {0:R}\n", this.OpenedOn );
      result += String.Format( "Balance (in USD) : {0:C}\n", this.Balance );
      return result;
    }

  }
}

At runtime, the ObjectSpaces framework creates a derived class from the persistent class and maps the data from a DataSet that it manages behind the scenes into and out of the members of the class. In addition, you'll notice that you can provide your own properties and methods (such as credit, debit, orcalculateInterest) that can be calculated from other members or can perform other business functions. The OnCreate, OnMaterialize, and OnDelete methods are called by the ObjectSpaces framework when the object is created, populated from the data store, and removed respectively. The OnCreate method is particularly useful for passing arguments to the class as it is instantiated. In above example, it is used to assign a client-generated primary key value to the object. As AccountNo  property is marked with UniqueId attribute it can only be assigned from OnCreate method.

Compile the File into a DLL.

Command Prompt
C:\MyProjects\ObjectSpaces\Bank>C:\WINNT\Microsoft.NET\Framework\v1.0.3705\csc /t:library /out:Account.dll Account.cs /r:system.dll /r:Microsoft.ObjectSp
aces.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>

 

Now you need to build a client application (Bank Teller) that can access this Persistent Business Entity component.

What is Microsoft Object Spaces
Building the Account Business Entity Class
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.

 


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.