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#.
////////////////////////////////////////////////////// /// 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 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.