ObjectConvertor.cs :  » Database » NBearLite » NBear » Mapping » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Database » NBearLite 
NBearLite » NBear » Mapping » ObjectConvertor.cs
using System;
using System.Text;
using System.Data;
using System.Collections.Generic;
using System.Collections;
namespace NBear.Mapping{
    public static class ObjectConvertor
    {
        #region ToObject


        /// <summary>
        ///   Convert to outputType
        /// </summary>
        /// <typeparam name="OutputType">The type of the output type.</typeparam>
        /// <param name="inputObject">The input object.</param>
        /// <returns></returns>
        public static OutputType ToObject<OutputType>(object inputObject)
            where OutputType : class
        {
            return (OutputType)ToObject(inputObject.GetType(), typeof(OutputType), typeof(OutputType), inputObject);
        }

        /// <summary>
        ///   Convert to outputType
        /// </summary>
        /// <typeparam name="InputType">The type of the input type.</typeparam>
        /// <typeparam name="OutputType">The type of the output type.</typeparam>
        /// <param name="inputObject">The input object.</param>
        /// <returns></returns>
        public static OutputType ToObject<InputType, OutputType>(InputType inputObject)
            where InputType : class
            where OutputType : class
        {
            return (OutputType)ToObject(typeof(InputType), typeof(OutputType), typeof(OutputType), inputObject);
        }

        /// <summary>
        ///   Convert to outputType
        /// </summary>
        /// <typeparam name="InputType">The type of the nput type.</typeparam>
        /// <typeparam name="OutputType">The type of the utput type.</typeparam>
        /// <typeparam name="InitType">The type of the nit type.</typeparam>
        /// <param name="inputObject">The input object.</param>
        /// <returns></returns>
        public static OutputType ToObject<InputType, OutputType, InitType>(InputType inputObject)
            where OutputType : class
            where InitType : class
        {
            return ToObject<InputType, OutputType, InitType>(inputObject, string.Empty);
        }

        public static OutputType ToObject<InputType, OutputType, InitType>(InputType inputObject, string viewName)
        {
            return (OutputType)ToObject(typeof(InputType), typeof(OutputType), typeof(InitType), inputObject, viewName);
        }

        /// <summary>
        ///   Convert to outputType
        /// </summary>
        /// <typeparam name="OutputType">The type of the output type.</typeparam>
        /// <param name="inputObject">The input object.</param>
        /// <returns></returns>
        public static OutputType ToObject<OutputType>(object inputObject, string viewName)
            where OutputType : class
        {
            return (OutputType)ToObject(inputObject.GetType(), typeof(OutputType), typeof(OutputType), inputObject, viewName);
        }

        /// <summary>
        ///   Convert to outputType
        /// </summary>
        /// <typeparam name="InputType">The type of the nput type.</typeparam>
        /// <typeparam name="OutputType">The type of the utput type.</typeparam>
        /// <param name="inputObject">The input object.</param>
        /// <returns></returns>
        public static OutputType ToObject<InputType, OutputType>(InputType inputObject, string viewName)
            where InputType : class
            where OutputType : class
        {
            return (OutputType)ToObject(typeof(InputType), typeof(OutputType), typeof(OutputType), inputObject, viewName);
        }

        /// <summary>
        ///   convert to object
        /// </summary>
        /// <param name="inputType">Type of the input.</param>
        /// <param name="outputType">Type of the output.</param>
        /// <param name="initType">Type of the init.</param>
        /// <param name="inputObject">The input object.</param>
        /// <returns></returns>
        public static object ToObject(Type inputType, Type outputType, Type initType, object inputObject)
        {
            return ToObject(inputType, outputType, initType, inputObject, string.Empty);
        }

        /// <summary>
        ///   convert to object
        /// </summary>
        /// <param name="inputType">Type of the input.</param>
        /// <param name="outputType">Type of the output.</param>
        /// <param name="initType">Type of the init.</param>
        /// <param name="inputObject">The input object.</param>
        /// <param name="viewName">Name of the view.</param>
        /// <returns></returns>
        public static object ToObject(Type inputType, Type outputType, Type initType, object inputObject, string viewName)
        {
            return ToObject(inputType, outputType, initType, inputObject, null, viewName);

        }


        /// <summary>
        /// Toes the object.
        /// </summary>
        /// <typeparam name="OutputType">The type of the utput type.</typeparam>
        /// <param name="inputObject">The input object.</param>
        /// <param name="outputObject">The output object.</param>
        /// <returns></returns>
        public static OutputType ToObject<OutputType>(object inputObject, OutputType outputObject)
            where OutputType : class
        {
            return ToObject<OutputType>(inputObject, outputObject, string.Empty);
        }
        /// <summary>
        /// Toes the object.
        /// </summary>
        /// <typeparam name="OutputType">The type of the utput type.</typeparam>
        /// <param name="inputObject">The input object.</param>
        /// <param name="outputObject">The output object.</param>
        /// <param name="viewName">Name of the view.</param>
        /// <returns></returns>
        public static OutputType ToObject<OutputType>(object inputObject, OutputType outputObject, string viewName)
        {
            return (OutputType)ToObject(inputObject.GetType(), typeof(OutputType), inputObject, outputObject, viewName);
        }
        /// <summary>
        /// Toes the object.
        /// </summary>
        /// <typeparam name="InpuType">The type of the npu type.</typeparam>
        /// <typeparam name="OutputType">The type of the utput type.</typeparam>
        /// <param name="inputObject">The input object.</param>
        /// <param name="outputObject">The output object.</param>
        /// <returns></returns>
        public static OutputType ToObject<InpuType, OutputType>(InpuType inputObject, OutputType outputObject)
            where InpuType : class
            where OutputType : class
        {
            return ToObject<InpuType, OutputType>(inputObject, outputObject, string.Empty);
        }
        /// <summary>
        /// Toes the object.
        /// </summary>
        /// <typeparam name="InpuType">The type of the npu type.</typeparam>
        /// <typeparam name="OutputType">The type of the utput type.</typeparam>
        /// <param name="inputObject">The input object.</param>
        /// <param name="outputObject">The output object.</param>
        /// <param name="viewName">Name of the view.</param>
        /// <returns></returns>
        public static OutputType ToObject<InpuType, OutputType>(InpuType inputObject, OutputType outputObject, string viewName)
            where InpuType : class
            where OutputType : class
        {
            return (OutputType)ToObject(typeof(InpuType), typeof(OutputType), inputObject, outputObject, viewName);
        }
        public static object ToObject(Type inputType, Type outputType, object inputObject, object outputObject)
        {
            return ToObject(inputType, outputType, inputObject, outputObject, string.Empty);
        }
        public static object ToObject(Type inputType, Type outputType, object inputObject, object outputObject, string viewName)
        {
            return ToObject(inputType, outputType, null, inputObject, outputObject, viewName);
        }
        private static object ToObject(Type inputType, Type outputType, Type initType, object inputObject, object outputObject, string viewName)
        {
            ObjectMapper objectMapper = GetObjectMapper(inputType, outputType, initType, viewName);
            return objectMapper.ConvertObject(inputObject, outputObject);
        }
        #endregion

        /// <summary>
        ///   get object mapper
        /// </summary>
        /// <param name="inputType">Type of the input.</param>
        /// <param name="outputType">Type of the output.</param>
        /// <param name="initType">Type of the init.</param>
        /// <param name="viewName">Name of the view.</param>
        /// <returns></returns>
        internal static ObjectMapper GetObjectMapper(Type inputType, Type outputType, Type initType, string viewName)
        {
            ObjectMapper objectMapper = ConfigurationManager.GetObjectMapper(inputType, outputType, viewName);
            if (objectMapper == null)
            {
                objectMapper = new ObjectMapper(inputType, outputType, initType);
            }
            return objectMapper;
        }

        #region ToList


        /// <summary>
        /// convert to object list
        /// </summary>
        /// <typeparam name="InputType">The type of the input type.</typeparam>
        /// <typeparam name="OutputType">The type of the output type.</typeparam>
        /// <param name="inputList">The input list.(IEnumerable or DataTable or DataReader)</param>
        /// <returns></returns>
        public static OutputType[] ToList<InputType, OutputType>(object inputList)
            where InputType : class
            where OutputType : class
        {
            return ToList<InputType, OutputType>(inputList, string.Empty);
        }

        /// <summary>
        ///   convert to object list
        /// </summary>
        /// <typeparam name="InputType">The type of the nput type.</typeparam>
        /// <typeparam name="OutputType">The type of the utput type.</typeparam>
        /// <typeparam name="InitType">The type of the nit type.</typeparam>
        /// <param name="inputObject">The input object.(IEnumerable or DataTable or DataReader)</param>
        /// <returns></returns>
        public static OutputType[] ToList<InputType, OutputType, InitType>(object inputList)
            where InputType : class
            where OutputType : class
            where InitType : class
        {
            return ToList<InputType, OutputType, InitType>(inputList, string.Empty);
        }

        /// <summary>
        ///   convert to object list
        /// </summary>
        /// <typeparam name="InputType">The type of the nput type.</typeparam>
        /// <typeparam name="OutputType">The type of the utput type.</typeparam>
        /// <param name="inputObject">The input object.(IEnumerable or DataTable or DataReader)</param>
        /// <param name="viewName">Name of the view.</param>
        /// <returns></returns>
        public static OutputType[] ToList<InputType, OutputType>(object inputList, string viewName)
            where InputType : class
            where OutputType : class
        {
            return ToList<InputType, OutputType, OutputType>(inputList, viewName);
        }

        /// <summary>
        ///   convert to list
        /// </summary>
        /// <typeparam name="InputType">The type of the input type.</typeparam>
        /// <typeparam name="OutputType">The type of the output type.</typeparam>
        /// <typeparam name="InitType">The type of the init type.</typeparam>
        /// <param name="inputList">The input list.</param>
        /// <param name="viewName">Name of the view.</param>
        /// <returns></returns>
        public static OutputType[] ToList<InputType, OutputType, InitType>(object inputList, string viewName)
            where InputType : class
            where OutputType : class
            where InitType : class
        {
            return ToList<OutputType>(inputList, typeof(InputType), typeof(InitType), viewName);
        }

        /// <summary>
        ///   convert to list
        /// </summary>
        /// <typeparam name="OutputType">The type of the output type.</typeparam>
        /// <param name="inputList">The input list.</param>
        /// <param name="inputType">Type of the input.</param>
        /// <param name="initType">Type of the init.</param>
        /// <param name="viewName">Name of the view.</param>
        /// <returns></returns>
        public static OutputType[] ToList<OutputType>(object inputList, Type inputType, Type initType, string viewName)
            where OutputType : class
        {
            List<OutputType> list = new List<OutputType>();
            if (inputList is DataTable)
            {
                ObjectMapper objectMapper = GetObjectMapper(typeof(DataRow), typeof(OutputType), initType, viewName);
                foreach (DataRow dataRow in ((DataTable)inputList).Rows)
                {
                    list.Add((OutputType)objectMapper.ConvertObject(dataRow));
                }
            }
            else if (inputList is IDataReader)
            {
                ObjectMapper objectMapper = GetObjectMapper(typeof(IDataReader), typeof(OutputType), initType, viewName);
                IDataReader dataReader = (IDataReader)inputList;
                while (dataReader.Read())
                {
                    list.Add((OutputType)objectMapper.ConvertObject(dataReader));
                }
            }
            else
            {
                IEnumerable enumerable = (IEnumerable)inputList;
                IEnumerator enumerator = enumerable.GetEnumerator();
                ObjectMapper objectMapper = GetObjectMapper(inputType, typeof(OutputType), initType, viewName);
                while (enumerator.MoveNext())
                {
                    list.Add((OutputType)objectMapper.ConvertObject(enumerator.Current));
                }
            }
            return list.ToArray();
        }


        /// <summary>
        ///   ToList
        /// </summary>
        /// <param name="inputList">The input list.</param>
        /// <param name="inputType">Type of the input.</param>
        /// <param name="outputType">Type of the output.</param>
        /// <returns></returns>
        public static object[] ToList(object inputList, Type inputType, Type outputType)
        {
            return ToList(inputList, inputType, outputType, string.Empty);
        }
        /// <summary>
        ///   ToList
        /// </summary>
        /// <param name="inputList">The input list.</param>
        /// <param name="inputType">Type of the input.</param>
        /// <param name="outputType">Type of the output.</param>
        /// <param name="viewName">Name of the view.</param>
        /// <returns></returns>
        public static object[] ToList(object inputList, Type inputType, Type outputType, string viewName)
        {
            return ToList(inputList, inputType, outputType, viewName);
        }
        /// <summary>
        ///   ToList
        /// </summary>
        /// <param name="inputList">The input list.</param>
        /// <param name="inputType">Type of the input.</param>
        /// <param name="outputType">Type of the output.</param>
        /// <param name="initType">Type of the init.</param>
        /// <param name="viewName">Name of the view.</param>
        /// <returns></returns>
        public static object[] ToList(object inputList, Type inputType, Type outputType, Type initType, string viewName)
        {
            ArrayList list = new ArrayList();
            if (inputList is DataTable)
            {
                ObjectMapper objectMapper = GetObjectMapper(typeof(DataRow), outputType, initType, viewName);
                foreach (DataRow dataRow in ((DataTable)inputList).Rows)
                {
                    list.Add(objectMapper.ConvertObject(dataRow));
                }
            }
            else if (inputList is IDataReader)
            {
                ObjectMapper objectMapper = GetObjectMapper(typeof(IDataReader), outputType, initType, viewName);
                IDataReader dataReader = (IDataReader)inputList;
                while (dataReader.Read())
                {
                    list.Add(objectMapper.ConvertObject(dataReader));
                }
            }
            else
            {
                IEnumerable enumerable = (IEnumerable)inputList;
                IEnumerator enumerator = enumerable.GetEnumerator();
                ObjectMapper objectMapper = GetObjectMapper(inputType, outputType, initType, viewName);
                while (enumerator.MoveNext())
                {
                    list.Add(objectMapper.ConvertObject(enumerator.Current));
                }
            }
            return list.ToArray();
        }
        #endregion

        #region ToDataTable

        /// <summary>
        ///   convert to DataTable
        /// </summary>
        /// <param name="inputList">The input list.</param>
        /// <param name="inputType">Type of the input.</param>
        /// <returns></returns>
        public static DataTable ToDataTable(object inputList, Type inputType)
        {
            return ToDataTable(inputList, inputType, DataRowState.Unchanged);
        }
        public static DataTable ToDataTable(object inputList, Type inputType, DataRowState rowState)
        {
            return ToDataTable(inputList, inputType, string.Empty, rowState);
        }
        /// <summary>
        ///   convert to DataTable
        /// </summary>
        /// <param name="inputList">The input list.</param>
        /// <param name="inputType">Type of the input.</param>
        /// <param name="viewName">Name of the view.</param>
        /// <returns></returns>
        public static DataTable ToDataTable(object inputList, Type inputType, string viewName, DataRowState rowState)
        {
            DataTable dataTable = null;
            //if (inputList is DataTable)
            //{
            //    ObjectMapper objectMapper = GetObjectMapper(typeof(DataRow), typeof(DataTable), null, viewName);
            //    foreach (DataRow dataRow in ((DataTable)inputList).Rows)
            //    {
            //        dataTable = (DataTable)objectMapper.ConvertObject(dataRow, dataTable);
            //    }
            //}
            //else if (inputList is IDataReader)
            //{
            //    ObjectMapper objectMapper = GetObjectMapper(typeof(IDataReader), typeof(DataTable), null, viewName);
            //    IDataReader dataReader = (IDataReader)inputList;
            //    while (dataReader.Read())
            //    {
            //        dataTable = (DataTable)objectMapper.ConvertObject(dataReader, dataTable);
            //    }
            //}
            //else
            //{
            Check.Require(inputList is IEnumerable, "input list type must implement from IEnumerable");
            IEnumerable enumerable = (IEnumerable)inputList;
            IEnumerator enumerator = enumerable.GetEnumerator();
            ObjectMapper objectMapper = GetObjectMapper(inputType, typeof(DataRow), null, viewName);
            DataRow dataRow = null;
            while (enumerator.MoveNext())
            {
                dataRow = (DataRow)objectMapper.ConvertObject(enumerator.Current, dataRow);
                dataTable = dataRow.Table;
                dataTable.Rows.Add(dataRow);

                switch (rowState)
                {
                    case DataRowState.Added:
                        break;
                    case DataRowState.Deleted:
                        break;
                    case DataRowState.Detached:
                        break;
                    case DataRowState.Modified:
                        dataRow.AcceptChanges();
                        dataRow.SetModified();
                        break;
                    case DataRowState.Unchanged:
                        dataRow.AcceptChanges();
                        break;
                    default:
                        break;
                }

                dataRow = dataTable.NewRow();
            }
            //}

            //dataTable.AcceptChanges();
            return dataTable;
        }

        #endregion
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.