Click here to Skip to main content
15,896,154 members
Articles / Desktop Programming / Windows Forms

Do You Really Want To Be Agile?

Rate me:
Please Sign up or sign in to vote.
4.91/5 (50 votes)
29 Dec 2011CPOL44 min read 99K   735   112  
A walk on the wild side using Relationship Oriented Programming.
using System;
using System.Collections.Generic;
using System.Data;

namespace Clifton.Tools.Data
{
	public static class RawDataTable
	{
		public static void Serialize(RawSerializer rs, DataTable dt)
		{
			int goodRowCount = dt.Rows.Count;

			foreach (DataRow testrow in dt.Rows)
			{
				if ((testrow.RowState == DataRowState.Deleted) || (testrow.RowState == DataRowState.Detached))
				{
					--goodRowCount;
				}
			}

			rs.Serialize(dt.TableName);
			rs.Serialize(dt.Columns.Count);
			rs.Serialize(goodRowCount);
			rs.Serialize(dt.PrimaryKey.Length);

			foreach (DataColumn dc in dt.PrimaryKey)
			{
				rs.Serialize(dc.ColumnName);
			}

			foreach (DataColumn dc in dt.Columns)
			{
				rs.Serialize(dc.ColumnName);
				rs.Serialize(dc.AllowDBNull);
				rs.Serialize(dc.ReadOnly);
				rs.Serialize(dc.Expression);
				rs.Serialize(dc.DataType.AssemblyQualifiedName);
				rs.SerializeNullable(dc.DefaultValue);
			}

			foreach (DataRow dr in dt.Rows)
			{
				if ((dr.RowState != DataRowState.Deleted) && (dr.RowState != DataRowState.Detached))
				{
					foreach (DataColumn dc in dt.Columns)
					{
						if (dc.AllowDBNull)
						{
							rs.SerializeNullable(dr[dc]);
						}
						else
						{
							rs.Serialize(dr[dc]);
						}
					}
				}
			}
		}

		public static DataTable Deserialize(RawDeserializer rd)
		{
			string tableName = rd.DeserializeString();
			int columns = rd.DeserializeInt();
			int rows = rd.DeserializeInt();
			int pkCount = rd.DeserializeInt();
			string[] pkColNames=null;

			// Read the PK column names.
			if (pkCount > 0)
			{
				pkColNames = new string[pkCount];

				for (int i = 0; i < pkCount; i++)
				{
					pkColNames[i] = rd.DeserializeString();
				}
			}

			DataTable dtIn = new DataTable(tableName);

			// Initialize the columns.
			for (int x = 0; x < columns; x++)
			{
				string columnName = rd.DeserializeString();
				bool allowNulls = rd.DeserializeBool();
				bool readOnly = rd.DeserializeBool();
				string expression = rd.DeserializeString();
				string type = rd.DeserializeString();
				Type colType=Type.GetType(type);
				object defaultValue = rd.DeserializeNullable(colType);

				DataColumn dc = new DataColumn(columnName, colType);
				dc.DefaultValue = defaultValue;
				dc.AllowDBNull = allowNulls;
				dc.ReadOnly = readOnly;
				dc.Expression = expression;
				dtIn.Columns.Add(dc);
			}

			if (pkCount > 0)
			{
				List<DataColumn> pkCols = new List<DataColumn>();
				// DataColumn[] pkCols = new DataColumn[pkCount];

				for (int i = 0; i < pkCount; i++)
				{
                    // Allowable PK columns are only non-nullable pk fields.
                    // Otherwise, we have synchronization problems between the server and the client,
                    // and the client attempts to populate PK fields.
                    // A PK field will be nullable if the table in the view is designated as ReadOnly.
                    if (!dtIn.Columns[pkColNames[i]].AllowDBNull)
                    {
						pkCols.Add(dtIn.Columns[pkColNames[i]]);
                        // pkCols[i] = dtIn.Columns[pkColNames[i]];
                    }
				}

				dtIn.PrimaryKey = pkCols.ToArray();
			}

			// Initialize the rows.
			for (int y = 0; y < rows; y++)
			{
				DataRow dr = dtIn.NewRow();

				for (int x = 0; x < columns; x++)
				{
					DataColumn dc = dtIn.Columns[x];
					object obj;

					if (dc.AllowDBNull)
					{
						obj = rd.DeserializeNullable(dc.DataType);
					}
					else
					{
						obj = rd.Deserialize(dc.DataType);
					}

					dr[dc] = obj;
				}

				dtIn.Rows.Add(dr);
			}

			return dtIn;
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Interacx
United States United States
Blog: https://marcclifton.wordpress.com/
Home Page: http://www.marcclifton.com
Research: http://www.higherorderprogramming.com/
GitHub: https://github.com/cliftonm

All my life I have been passionate about architecture / software design, as this is the cornerstone to a maintainable and extensible application. As such, I have enjoyed exploring some crazy ideas and discovering that they are not so crazy after all. I also love writing about my ideas and seeing the community response. As a consultant, I've enjoyed working in a wide range of industries such as aerospace, boatyard management, remote sensing, emergency services / data management, and casino operations. I've done a variety of pro-bono work non-profit organizations related to nature conservancy, drug recovery and women's health.

Comments and Discussions