I think this is what you've requested (And then some):
/// <summary>
/// Collection object for TBL_ASSET
/// </summary>
[Serializable(), XmlRoot("ASSETS")]
public class AssetCollection : DataObjectCollection
{
/// <summary>
/// Select command that will select all records from the TBL_ASSET with joined tables (TBL_ASSET_TYPE,TBL_MANUFACTURE)
/// </summary>
public override string SelectStatement()
{// temp
return "SELECT " +
// SerialNumber
"TBL_ASSET.SERIAL_NUMBER AS SERIAL_NUMBER, " +
// ModelNumber
"TBL_ASSET.MODEL_NUMBER AS MODEL_NUMBER, " +
// LessorAssetNumber
"TBL_ASSET.LESSOR_ASSET_NUMBER AS LESSOR_ASSET_NUMBER," +
// EquipDescription
"TBL_ASSET.EQUIP_DESCRIPTION AS EQUIP_DESCRIPTION," +
// AssetType
"TBL_ASSET.ASSET_TYPE AS ASSET_TYPE," +
// CostMonthly
"TBL_ASSET.COST_MONTHLY AS COST_MONTHLY, " +
// ManufactureId
"TBL_ASSET.MANUFACTURE_ID AS MANUFACTURE_ID," +
// ProductNumber
"TBL_ASSET.PRODUCT_NUMBER AS PRODUCT_NUMBER, " +
// AssetTypeDescription (AssetType)
"TBL_ASSET_TYPE.DESCRIPTION AS ASSET_TYPE_DESCRIPTION, " +
// ManufactureDescription (ManufactureId)
"TBL_MANUFACTURE.DESCRIPTION AS MANUFACTURE_DESCRIPTION " +
/* ANSI*/
"FROM LEASETRACK.TBL_ASSET INNER JOIN LEASETRACK.TBL_ASSET_TYPE ON TBL_ASSET_TYPE.ID = TBL_ASSET.ASSET_TYPE " +
"INNER JOIN LEASETRACK.TBL_MANUFACTURE on TBL_MANUFACTURE.ID = TBL_ASSET.MANUFACTURE_ID";
/*
"FROM LEASETRACK.TBL_ASSET, LEASETRACK.TBL_ASSET_TYPE, LEASETRACK.TBL_MANUFACTURE" +
"WHERE TBL_ASSET_TYPE.ID = TBL_ASSET.ASSET_TYPE AND TBL_MANUFACTURE.ID = TBL_ASSET.MANUFACTURE_ID;";
*/
}
/* Implement the required method Add */
public override int Add(System.Data.IDataReader reader)
{
while (reader.Read())
{
object[] values = new object[reader.FieldCount];
// copy
if (reader.GetValues(values) > 0)
{
// add Asset
List.Add(new Asset(values));
}
}
return List.Count;
}
... needed prior to manual serialization
public int Add(Asset asset)
{
return List.Add(asset);
}
public Asset this[int index]
{
get { return (Asset)List[index]; }
... setter removed (wasn't wanted anyway)
}
/* Default Serialization (didn't work)
public static void Serialize(System.IO.Stream stream, AssetCollection assets)
{
System.Xml.Serialization.XmlSerializer writer = new XmlSerializer(assets.GetType(),new Type[] { typeof(Asset) });
writer.Serialize(stream, assets);
}
public static AssetCollection Deserialize(System.IO.Stream stream)
{
System.Xml.Serialization.XmlSerializer reader = new XmlSerializer(typeof(AssetCollection),
new Type[] { typeof(Asset) });
return (AssetCollection)reader.Deserialize(stream);
}
*/
public static AssetCollection Deserialize(System.IO.Stream stream)
{
AssetCollection col = new AssetCollection();
System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument();
xDoc.Load(stream);
System.Xml.XmlElement root = xDoc.DocumentElement;
foreach (System.Xml.XmlNode node in root.ChildNodes)
{
... load nodes
}
return col;
}
public static void Serialize(System.IO.Stream stream, AssetCollection assets)
{
// manual serialization
System.Xml.XmlDocument xDoc = assets.GetXML();
// write xml to stream
System.Xml.XmlTextWriter writer = new System.Xml.XmlTextWriter(stream, null);
// formating (optional?)
writer.Formatting = System.Xml.Formatting.Indented;
xDoc.WriteContentTo(writer);
// Write to underlying stream
writer.Flush();
}
System.Xml.XmlDocument GetXML()
{
System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument();
// simple declaration & Root - This is NOT a well formated document...
xDoc.LoadXml("<?xml version=\"1.0\"?><ASSETS></ASSETS>");
System.Xml.XmlElement root = xDoc.DocumentElement;
foreach (Asset item in this)
{
... Add nodes
}
return xDoc;
}
}
[Serializable()]
public class Asset : DataObjectBase, IDataAffecter
{
internal Asset(object[] values)
{
Load(values);
}
public Asset() { }
/// <summary>
/// Gets an update command based on the SerialNumber property.
/// </summary>
/// <returns>String to use as a CommandString</returns>
string IDataAffecter.GetUpdateStatement()
{
...
}
/// <summary>
/// Gets a delete command for the SerialNumber property.
/// </summary>
/// <returns>String to use as a CommandString</returns>
string IDataAffecter.GetDeleteStatement()
{
...
}
/// <summary>
/// Gets an insert command of the current properties.
/// </summary>
/// <returns>String to use as a CommandString</returns>
string IDataAffecter.GetInsertStatement()
{
...
}
/// <summary>
/// Gets a select command with additional filters of properties that have been set.
/// </summary>
/// <returns>String to use as a CommandString</returns>
/// <remarks>This function is only applicable for Asset objects that have not been created by an AssetCollection.</remarks>
public override string GetFilteredSelect()
{
...
}
#region fields
/* Fields have been given internal access to allow the AssetCollection to serialize the Asset object */
internal object _SerialNumber = null;
internal object _ModelNumber = null;
internal object _LessorAssetNumber = null;
internal object _EquipDescription = null;
internal object _AssetType = null;
internal object _CostMonthly = null;
internal object _ManufactureId = null;
internal object _ProductNumber = null;
internal object _AssetTypeDescription = null;
internal object _ManufactureDescription = null;
#endregion
public override void Load(object[] values)
{
int propertyCount = this.GetType().GetProperties().Length;
if (propertyCount != values.Length)
{
throw new ArgumentException("The values supplied do not match the expected array length of " +
propertyCount.ToString() + ".");
}
_SerialNumber = values[0];
_ModelNumber = values[1];
_LessorAssetNumber = values[2];
_EquipDescription = values[3];
_AssetType = values[4];
_CostMonthly = values[5];
_ManufactureId = values[6];
_ProductNumber = values[7];
_AssetTypeDescription = values[8];
_ManufactureDescription = values[9];
}
/// <summary>
/// TBL_ASSET.SERIAL_NUMBER
/// </summary>
[XmlElement("SERIAL_NUMBER")]
public string SerialNumber
{
get {
return (_SerialNumber == null) ? String.Empty : _SerialNumber.ToString();
}
set { _SerialNumber = value; }
}
/// <summary>
/// TBL_ASSET.MODEL_NUMBER
/// </summary>
[XmlElement("MODEL_NUMBER")]
public string ModelNumber
{
get {
return (_ModelNumber == null) ? String.Empty : _ModelNumber.ToString();
}
set { _ModelNumber = value; }
}
/// <summary>
/// TBL_ASSET.LESSOR_ASSET_NUMBER
/// </summary>
[XmlElement("LESSOR_ASSET_NUMBER")]
public string LessorAssetNumber
{
get {
return (_LessorAssetNumber == null) ? String.Empty : _LessorAssetNumber.ToString();
}
set { _LessorAssetNumber = value; }
}
/// <summary>
/// TBL_ASSET.EQUIP_DESCRIPTION
/// </summary>
[XmlElement("EQUIP_DESCRIPTION")]
public string EquipDescription
{
get {
return (_EquipDescription == null) ? String.Empty : _EquipDescription.ToString();
}
set { _EquipDescription = value; }
}
/// <summary>
/// TBL_ASSET.ASSET_TYPE
/// </summary>
[XmlElement("ASSET_TYPE")]
public int AssetType /* Double */
{
get {
return (_AssetType == null) ? new Int32() : Convert.ToInt32(_AssetType);
}
set { _AssetType = value; }
}
/// <summary>
/// TBL_ASSET.COST_MONTHLY
/// </summary>
[XmlElement("COST_MONTHLY")]
public Decimal CostMonthly
{
get {
return (_CostMonthly == null) ? new Decimal() : Convert.ToDecimal(_CostMonthly);
}
set { _CostMonthly = value; }
}
/// <summary>
/// TBL_ASSET.MANUFACTURE_ID
/// </summary>
[XmlElement("MANUFACTURE_ID")]
public int ManufactureId /* Double */
{
get {
return (_ManufactureId == null) ? new Int32() : Convert.ToInt32(_ManufactureId);
}
set { _ManufactureId = value; }
}
/// <summary>
/// TBL_ASSET.PRODUCT_NUMBER
/// </summary>
[XmlElement("PRODUCT_NUMBER")]
public string ProductNumber
{
get {
return (_ProductNumber == null) ? String.Empty : _ProductNumber.ToString();
}
set { _ProductNumber = value; }
}
/// <summary>
/// TBL_ASSET_TYPE.DESCRIPTION (ASSET_TYPE_DESCRIPTION)
/// </summary>
//[XmlElement("ASSET_TYPE_DESCRIPTION")]
public string AssetTypeDescription
{
get {
return (_AssetTypeDescription == null) ? String.Empty : _AssetTypeDescription.ToString();
}
//set { _AssetTypeDescription = value; }
}
/// <summary>
/// TBL_MANUFACTURE.DESCRIPTION (MANUFACTURE_DESCRIPTION)
/// </summary>
//[XmlElement("MANUFACTURE_DESCRIPTION")]
public string ManufactureDescription
{
get {
return (_ManufactureDescription == null) ? String.Empty : _ManufactureDescription.ToString();
}
//set { _ManufactureDescription = value; }
}
}
This is where the objects are used:
public class DataSource
{
IDbCommand cmd = null;
IDbConnection con = null;
IDataReader reader = null;
public DataSource(string connectionString)
{
// using Odbc
con = new OdbcConnection(connectionString);
}
public void Open()
{
con.Open();
}
public void Close()
{
con.Close();
}
#region Selection
public int Fill(DataObjectCollection objects)
{
cmd = con.CreateCommand();
cmd.CommandText = objects.SelectStatement();
reader = cmd.ExecuteReader();
objects.Add(reader);
reader.Close();
cmd.Dispose();
return objects.Count;
}
public int Fill(DataObjectCollection objects, string selectCommand)
{
...
}
public int Fill(DataObjectCollection objects, DataObjectBase selectFrom)
{
...
}
#endregion
#region Update
public int Update(IDataAffecter data)
{
...
}
public int BatchUpdate(DataObjectCollection objects)
{
...
}
#endregion
#region Insert
/// <summary>
/// Insert a IDataAffecter into a data source.
/// </summary>
/// <param name="data">IDataAffecter that contains the data to be inserted.</param>
/// <returns>Returns the number of records affected.</returns>
public int Insert(IDataAffecter data)
{
...
}
/// <summary>
/// Insert a collection of IDataAffecter items contained in a DataObjectCollection into a data source.
/// </summary>
/// <param name="objects">DataObjectCollection containing the data to be inserted.</param>
/// <returns>Returns the number of records affected.</returns>
public int BatchInsert(DataObjectCollection objects)
{
...
}
#endregion
}
Thank you for your time and assistance in this matter.
As you can see, I've moved to the manual serialization.
However; in this instance it worked well by allowing me to maintain some readonly properties for the Asset object- but there are several more instances where I could (and would prefer to) use the built in serialization - and these other instances are the same; a collection object and its contained objects.