Click here to Skip to main content
15,880,725 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 220.3K   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>
	/// Represents a column based on a field.
	/// </summary>
	internal class FieldColumn : AbstractColumn
	{
		private FieldInfo field;
		
		/// <summary>
		/// Creates a new field column.
		/// </summary>
		/// <param name="field">underlying class field</param>
		/// <param name="name">column name</param>
		/// <param name="dbtype">database data type</param>
		/// <param name="primaryKey">whether this column is a primary key column</param>
		/// <param name="autoIncr">whether this column is an auto-incremented column</param>
		internal FieldColumn(FieldInfo field, string name, DbType dbtype,
		                     bool primaryKey, bool autoIncr)
			: this(field, name, dbtype, 0, primaryKey, autoIncr)
		{
		}
		
		/// <summary>
		/// Creates a new field column.
		/// </summary>
		/// <param name="field">underlying class field</param>
		/// <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 column</param>
		/// <param name="autoIncr">whether this column is an auto-incremented column</param>
		internal FieldColumn(FieldInfo field, string name, DbType dbtype,
		                     int size, bool primaryKey, bool autoIncr)
			: this(field, name, dbtype, size, 0, 0, primaryKey, autoIncr)
		{
		}
		
		/// <summary>
		/// Creates a new field column.
		/// </summary>
		/// <param name="field">underlying class field</param>
		/// <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 column</param>
		/// <param name="autoIncr">whether this column is an auto-incremented column</param>
		internal FieldColumn(FieldInfo field, string name, DbType dbtype, int size,
		                     byte precision, byte scale, bool primaryKey, bool autoIncr)
			: base(name, dbtype, size, precision, scale, primaryKey, autoIncr)
		{
			this.field = field;
		}
		
		/// <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 override bool Writeable
		{
			get { return true; }
		}
		
		/// <summary>
		/// True if this column can be read from. That is,
		/// whether calling the Read method will return a valid value.
		/// </summary>
		public override bool Readable
		{
			get { return true; }
		}
		
		/// <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 override void Write(object target, object value)
		{
			field.SetValue(target, 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 override object Read(object source)
		{
			return field.GetValue(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