|
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
namespace PropertyMapping
{
/// <summary>
/// The manager of the object properties
/// </summary>
///
[CLSCompliant(true)]
public sealed class PropertyMaper
{
MapDictionary mapTable = new MapDictionary();
/// <summary>
/// Register object in the PropertyManager
/// </summary>
/// <param name="objectType">The tyep of the object to register in</param>
public void RegisterObject(Type objectType)
{
if (objectType == null)
throw new ArgumentNullException("Object type is null");
string objectUniqueKey = MapDictionary.UniqueKeyFromObjectType(objectType);
if (string.IsNullOrEmpty(objectUniqueKey))
throw new ArgumentNullException();
if(mapTable.ContainsKey(objectUniqueKey))
throw new ObjectAlreadyDefinedException(objectType);
InitObject(objectType);
}
/// <summary>
/// Register manualy single property of the object
/// </summary>
/// <param name="objectType">The type of the object</param>
/// <param name="propertyName">Property name to register</param>
public void RegisterProperty(Type objectType, string propertyName)
{
if (objectType == null ||
string.IsNullOrEmpty(propertyName))
return;
mapTable.AddProperty(MapDictionary.UniqueKeyFromObjectType(objectType), propertyName);
}
/// <summary>
/// Save object properties in the internal database
/// </summary>
/// <param name="objToSave">Object need properties of which we need to save</param>
/// <param name="uniqueKey">The unique key to associate with the saved snapshot</param>
public void SaveObjectProperties(object objToSave, string uniqueKey)
{
mapTable.SaveObjectMappedProperties(objToSave, uniqueKey);
}
/// <summary>
/// Save object properties in the internal database
/// </summary>
/// <param name="objToSave">Object need properties of which we need to save</param>
/// <param name="uniqueKey">The unique key to associate with the saved snapshot</param>
/// <param name="ignorePropertyStandardValue">If false, only the property with the value different from
/// the StandardValue of the MapPropertyAttribute will be saved </param>
public void SaveObjectProperties(object objToSave, string uniqueKey,bool ignoreStandardValue)
{
mapTable.SaveObjectMappedProperties(objToSave, uniqueKey,ignoreStandardValue );
}
/// <summary>
/// Save object properties in the internal database
/// </summary>
/// <param name="objToSave">Object need properties of which we need to save</param>
/// <param name="uniqueKey">The unique key to associate with the saved snapshot</param>
/// <param name="propertyNames">The list of the properties to save in</param>
public void SaveObjectProperties(object objToSave, string uniqueKey,List<string> propertyNames)
{
mapTable.SaveObjectMappedProperties(objToSave, uniqueKey,propertyNames);
}
/// <summary>
/// Save object properties in the internal database
/// </summary>
/// <param name="objToSave">Object need properties of which we need to save</param>
/// <param name="uniqueKey">The unique key to associate with the saved snapshot</param>
/// <param name="propertyNames">The property to save in</param>
public void SaveObjectProperties(object objToSave, string uniqueKey, string propertyName)
{
List<string> propertyNames = new List<string>();
propertyNames.Add(propertyName);
mapTable.SaveObjectMappedProperties(objToSave, uniqueKey, propertyNames);
}
/// <summary>
/// Filling object with the requested snapshot property values
/// </summary>
/// <param name="objToFill">Object to modify</param>
/// <param name="uniqueKey">the unique key of the snapshot to get</param>
public void FillObjectProperties(object objToFill, string uniqueKey)
{
mapTable.FillObjectProperties(objToFill, uniqueKey);
}
/// <summary>
/// Property validator
/// </summary>
/// <param name="propertyToExamin"></param>
/// <returns></returns>
bool isPropertyValid(PropertyInfo propertyToExamin)
{
object[] attrCol = propertyToExamin.GetCustomAttributes(typeof(MapPropertyAttribute), false);
if (attrCol == null || attrCol.Length == 0)
return false;
if (!(propertyToExamin.CanRead & propertyToExamin.CanWrite))
return false;
if(!propertyToExamin.PropertyType.IsAnsiClass &&
!propertyToExamin.PropertyType.IsAssignableFrom(typeof(ICloneable)))
return false;
if (propertyToExamin.PropertyType.IsAbstract ||
propertyToExamin.PropertyType.IsGenericParameter ||
propertyToExamin.PropertyType.IsGenericType ||
propertyToExamin.PropertyType.IsGenericTypeDefinition)
return false;
return true;
}
public List<string> UniqueKeys
{
get
{
return mapTable.UniqueKeys;
}
}
/// <summary>
/// Internal initilizer
/// </summary>
/// <param name="objectType"></param>
void InitObject(Type objectType)
{
Type objType = objectType;
PropertyInfo []props = objType.GetProperties();
if (props == null || props.Length == 0)
throw new ObjectHasNotValidProperties(objectType);
foreach (PropertyInfo pi in props)
{
if (!isPropertyValid(pi))
continue;
mapTable.AddProperty(MapDictionary.UniqueKeyFromObjectType(objectType), pi.Name);
}
}
}
}
|
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.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.