Click here to Skip to main content
Click here to Skip to main content
Articles » Database » Database » ADO.NET » Downloads
 
Add your own
alternative version
Go to top

Light ORM Library for .NET

, 8 Oct 2010
This article is about the Light Object-Relational Mapping library.
using System;
using System.Data;
using System.Reflection;

namespace Light.Model
{
	/// <summary>
	/// Represents a property based column.
	/// </summary>
	class PropertyColumn : AbstractColumn
	{
		PropertyInfo property;
		MethodInfo getter;
		MethodInfo setter;
		
		/// <summary>
		/// Creates a new property column.
		/// </summary>
		/// <param name="property">underlying class property</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="identity">whether this column is an identity column</param>
		internal PropertyColumn(PropertyInfo property, string name, DbType dbtype,
		                       bool primaryKey, bool identity)
			: this(property, name, dbtype, 0, primaryKey, identity)
		{
		}
		
		/// <summary>
		/// Creates a new property column.
		/// </summary>
		/// <param name="property">underlying class property</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="identity">whether this column is an identity column</param>
		internal PropertyColumn(PropertyInfo property, string name, DbType dbtype,
		                        int size, bool primaryKey, bool identity)
			: this(property, name, dbtype, size, 0, 0, primaryKey, identity)
		{
		}
		
		/// <summary>
		/// Creates a new property column.
		/// </summary>
		/// <param name="property">underlying class property</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="identity">whether this column is an identity column</param>
		internal PropertyColumn(PropertyInfo property, string name, DbType dbtype, int size,
		                        byte precision, byte scale, bool primaryKey, bool identity)
			: base(name, dbtype, size, precision, scale, primaryKey, identity)
		{
			this.property = property;
			this.getter = property.GetGetMethod(true);
			this.setter = property.GetSetMethod(true);
		}
		
		/// <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 setter != null; }
		}
		
		/// <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 getter != null; }
		}
		
		/// <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)
		{
			if(!Writeable)
				return;
			setter.Invoke(target, new 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 override object Read(object source)
		{
			if(!Readable)
				return null;
			return getter.Invoke(source, null);
		}
	}
}

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)

Share

About the Author

Sergey Busel
Software Developer (Senior)
United States United States
No Biography provided

| Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 8 Oct 2010
Article Copyright 2007 by Sergey Busel
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid