(c) 2002 Visual Studio Magazine 
Fawcette Technical Publications

Issue: May 2002
Section: Build Classy Web Services
Author: Jon Rauschenberger

C#	Execute Trades
Listing 1	The Executions collection is a strongly-typed collection that extends the CollectionBase class.  It adheres to all the rules required by the XMLSerializer for classes that expose the iCollection interface.

public class Trade
{
	//Public Properties
	public int ID;
	public string Symbol;
	public string Side;
	public int Quantity;
	public double Price;

	//Property holders
	private ExecutionsColl ExecutionsHolder = new 
		ExecutionsColl();

	//Public Collection
	public ExecutionsColl Executions
	{
		get
		{
			return  ExecutionsHolder;
 		}
	}
}

public class ExecutionsColl : System.Collections.CollectionBase
{
	//Indexer
	public Execution this [int index]
	{
		get
		{
			return (Execution)this.List[index];
		}
	}

	//Strongly typed Add method
	public void Add (Execution NewExecution)
	{
		this.List.Add (NewExecution);
	}
}

public class Execution
{
	//Public Properties
	public int Quantity;
	public double Price;
}

C#	Process Trades
Listing 2	The TradeProcessor contains all the logic to process new trades and return existing trades.  It consumes and returns Trade objects.

public class TradeProcessor
{
	public Trade Create()
	{
		return new Trade();
	}

	public Trade Add(Trade NewTrade)
	{
		//Set the TradeID
		NewTrade.ID =1;
		return NewTrade;
	}

	public Trade Load(int ID)
	{
		//Create a new Trade object
		Trade NewTrade = new Trade();

		//Set the peoperties of the Trade
		NewTrade.ID=ID;
		NewTrade.Symbol="MSFT";
		NewTrade.Side="B";
		NewTrade.Quantity=500;
		NewTrade.Price=100;
			
		//Create and populate the first Execution
		Execution NewExecution = new Execution();
		NewExecution.Quantity = 200;
		NewExecution.Price = 100;
			
		//Add the new execution to the Executions collection
		NewTrade.Executions.Add(NewExecution);
	
		//Create and populate the second Execution
		NewExecution = new Execution();
		NewExecution.Quantity=300;
		NewExecution.Price=100;

		//Add the new execution to the Executions collection
		NewTrade.Executions.Add(NewExecution);
		
		return NewTrade;
	}
}

XML	Transform the Trade Object into XML
Listing 3	The XMLSerializer exposes the Execution collection as an array of Execution types.  The Trade object is represented as a complex XML type.

<s:element name="CreateResponse">
<s:complexType>
	<s:sequence>
		<s:element minOccurs="0" maxOccurs="1" 
			name="CreateResult" type="s0:Trade" /> 
	</s:sequence>
</s:complexType>
</s:element>
<s:complexType name="Trade">
	<s:sequence>
		<s:element minOccurs="1" maxOccurs="1" name="ID" 
			type="s:int" /> 
		<s:element minOccurs="0" maxOccurs="1" 
			name="Symbol" type="s:string" /> 
		<s:element minOccurs="0" maxOccurs="1" name="Side" 
			type="s:string" /> 
		<s:element minOccurs="1" maxOccurs="1" 
			name="Quantity" type="s:int" /> 
		<s:element minOccurs="1" maxOccurs="1" name="Price" 
			type="s:double" /> 
<s:element minOccurs="0" maxOccurs="1" name="Executions" 
			type="s0:ArrayOfExecution" /> 
	</s:sequence>
</s:complexType>
<s:complexType name="ArrayOfExecution">
	<s:sequence>
		<s:element minOccurs="0" maxOccurs="unbounded" 
			name="Execution" nillable="true" type="s0:Execution" /> 
	</s:sequence>
</s:complexType>
<s:complexType name="Execution">
	<s:sequence>
		<s:element minOccurs="1" maxOccurs="1" 
			name="Quantity" type="s:int" /> 
		<s:element minOccurs="1" maxOccurs="1" name="Price" 
			type="s:double" /> 
	</s:sequence>
</s:complexType>
