Click here to Skip to main content
15,886,075 members
Articles / Database Development / SQL Server

Light ORM Library for .NET

Rate me:
Please Sign up or sign in to vote.
4.83/5 (39 votes)
8 Oct 2010CPOL17 min read 221.1K   3.1K   184  
This article is about the Light Object-Relational Mapping library.
using System;
using System.Data;
using System.Reflection;

namespace Light.Model
{
	/// <summary>
	/// Abstract representation of a table column.
	/// </summary>
	public abstract class AbstractColumn : IEquatable<AbstractColumn>
	{
		private string name;
		private DbType dbType;
		private int size;
		private byte precision;
		private byte scale;
		private bool primaryKey;
		private bool autoIncrement;
		
		/// <summary>
		/// Creates a new column.
		/// </summary>
		/// <param name="name">column name</param>
		/// <param name="dbtype">database data type</param>
		/// <param name="primaryKey">whether this column is a primary key</param>
		/// <param name="autoIncr">whether this column is an auto generated column</param>
		internal AbstractColumn(string name, DbType dbtype, bool primaryKey,
		                        bool autoIncr)
			: this(name, dbtype, 0, primaryKey, autoIncr)
		{
		}
		
		/// <summary>
		/// Creates a new column.
		/// </summary>
		/// <param name="name">column name</param>
		/// <param name="dbtype">database data type</param>
		/// <param name="size">size of the database data type</param>
		/// <param name="primaryKey">whether this column is a primary key</param>
		/// <param name="autoIncr">whether this column is an auto generated column</param>
		internal AbstractColumn(string name, DbType dbtype, int size,
		                        bool primaryKey, bool autoIncr)
			: this(name, dbtype, size, 0, 0, primaryKey, autoIncr)
		{
		}
		
		/// <summary>
		/// Creates a new column.
		/// </summary>
		/// <param name="name">column name</param>
		/// <param name="dbtype">database data type</param>
		/// <param name="size">size of the database data type</param>
		/// <param name="precision">precision of the database data type</param>
		/// <param name="scale">scale of the database data type</param>
		/// <param name="primaryKey">whether this column is a primary key</param>
		/// <param name="autoIncr">whether this column is an auto generated column</param>
		internal AbstractColumn(string name, DbType dbtype, int size, byte precision, byte scale,
		                        bool primaryKey, bool autoIncr)
		{
			this.name = name;
			this.dbType = dbtype;
			this.size = size;
			this.precision = precision;
			this.scale = scale;
			this.primaryKey = primaryKey;
			this.autoIncrement = autoIncr;
		}
		
		/// <summary>
		/// Checks for equality with another object.
		/// </summary>
		/// <param name="obj">another object</param>
		/// <returns>true if equal, false otherwise</returns>
		public override bool Equals(object obj)
		{
			if(obj is AbstractColumn)
				return Equals((AbstractColumn)obj);
			else
				return false;
		}
		
		/// <summary>
		/// Checks for equality with another column.
		/// </summary>
		/// <param name="other">another column</param>
		/// <returns>true if equal, false otherwise</returns>
		public bool Equals(AbstractColumn other)
		{
			if(other == null)
				return false;
			return name.Equals(other.name) &&
				dbType.Equals(other.dbType) && size.Equals(other.size) &&
				primaryKey.Equals(other.primaryKey) && autoIncrement.Equals(other.autoIncrement);
		}
		
		/// <summary>
		/// Returns the hash code for this instance.
		/// </summary>
		/// <returns>hash code</returns>
		public override int GetHashCode()
		{
			return name.GetHashCode() ^ dbType.GetHashCode() ^ size.GetHashCode() ^
				primaryKey.GetHashCode() ^ autoIncrement.GetHashCode();
		}
		
		/// <summary>
		/// Gets the column name.
		/// </summary>
		public string Name
		{
			get { return name; }
		}
		
		/// <summary>
		/// Gets the database data type.
		/// </summary>
		public DbType DBType
		{
			get { return dbType; }
		}
		
		/// <summary>
		/// Gets the size of the database data type.
		/// Usually relevant only for string columns.
		/// </summary>
		public int Size
		{
			get { return size; }
		}
		
		/// <summary>
		/// Gets the precision of the database data type.
		/// Usually relevant to decimal and numeric types.
		/// </summary>
		public byte Precision
		{
			get { return precision; }
		}
		
		/// <summary>
		/// Gets the scale of the database data type.
		/// Usually relevant to decimal and numeric types.
		/// </summary>
		public byte Scale
		{
			get { return scale; }
		}
		
		/// <summary>
		/// Gets whether this column is a primary key column.
		/// </summary>
		public bool PrimaryKey
		{
			get { return primaryKey; }
		}
		
		/// <summary>
		/// Gets whether this column is an auto generated column.
		/// </summary>
		public bool AutoIncrement
		{
			get { return autoIncrement; }
		}
		
		/// <summary>
		/// True if this column can be written to. That is,
		/// whether calling the Write method can actually
		/// attempt to write a value into a target object.
		/// </summary>
		public abstract bool Writeable { get; }
		
		/// <summary>
		/// True if this column can be read from. That is,
		/// whether calling the Read method will return a valid value.
		/// </summary>
		public abstract bool Readable { get; }
		
		/// <summary>
		/// Attempts to write a given value info a given target object.
		/// </summary>
		/// <param name="target">target object</param>
		/// <param name="value">value to write</param>
		public abstract void Write(object target, object value);
		
		/// <summary>
		/// Attempts to read a value from a given source object.
		/// </summary>
		/// <param name="source">source object</param>
		/// <returns>value of this column from the source object</returns>
		public abstract object Read(object source);
	}
}

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
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions