Click here to Skip to main content
11,496,146 members (589 online)
Click here to Skip to main content
Articles » Database » Database » Databases » Downloads
Add your own
alternative version

Single Object Property Database

, 30 Oct 2007 16.3K 76 14
Object Properties Snapshot Management
The site is currently in read-only mode for maintenance. Posting of new items will be available again shortly.
propertymappertest.zip
PropertyMapperTest
Objects
Properties
PropertyMapper
Attributes
Exceptions
Properties
PropertyManager.csproj.user
PropertyMapper
PropertyValuesTable
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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Tigran Martirosyan

United States United States
No Biography provided

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150520.1 | Last Updated 30 Oct 2007
Article Copyright 2007 by Tigran Martirosyan
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid