Exemplo n.º 1
0
        private DataTable ObjectToDataTable(string tblName, IEnumerable <object> instance)
        {
            RefectorUtility utility              = new RefectorUtility();
            IFileReader     file                 = new XmlFileReader();
            var             assembly             = Assembly.GetExecutingAssembly();
            var             manifestResourceName = assembly.GetManifestResourceNames().First();
            var             parsedContent        = file.Parse <TableClassMapping>(assembly.GetManifestResourceStream(manifestResourceName));
            var             mappedType           = parsedContent.First(x => x.TableName == tblName);
            var             tableType            = utility.GetTypeFromString(mappedType.ClassName, mappedType.AssemblyName);
            DataTable       dataTable            = new DataTable(tblName);

            foreach (PropertyInfo propertyInfo in tableType.GetProperties())
            {
                dataTable.Columns.Add(propertyInfo.Name, propertyInfo.PropertyType);
            }
            foreach (object item in instance)
            {
                var     objeInstance = JsonConvert.DeserializeObject(item.ToString(), tableType);
                DataRow dr           = dataTable.NewRow();
                foreach (PropertyInfo propertyInfo in tableType.GetProperties())
                {
                    dr[propertyInfo.Name] = propertyInfo.GetValue(objeInstance);
                }
                dataTable.Rows.Add(dr);
            }
            return(dataTable);
        }
        public IEnumerable <T> Parse <T>(Stream stream)
        {
            var data = _memoryCache.Get(XmlFileKey);

            if (data != null)
            {
                return((IEnumerable <T>)data);
            }

            RefectorUtility          utility  = new RefectorUtility();
            XDocument                xDoc     = XDocument.Parse(ReadFile(stream));
            XElement                 xElement = xDoc.Element("Mappings");
            List <TableClassMapping> mappings = new List <TableClassMapping>();

            if (xElement != null)
            {
                var mapElements = xElement.Descendants("Map");
                Dictionary <string, string> attributeValueMappingDictionary = new Dictionary <string, string>();
                foreach (XElement mapElement in mapElements)
                {
                    foreach (XAttribute xAttribute in mapElement.Attributes())
                    {
                        if (attributeValueMappingDictionary.ContainsKey(xAttribute.Name.LocalName))
                        {
                            attributeValueMappingDictionary[xAttribute.Name.LocalName] = xAttribute.Value;
                        }
                        else
                        {
                            attributeValueMappingDictionary.Add(xAttribute.Name.LocalName, xAttribute.Value);
                        }
                    }
                    var mappedClass = utility.ParseColumn(typeof(TableClassMapping), attributeValueMappingDictionary) as TableClassMapping;
                    mappings.Add(mappedClass);
                }
            }
            var cacheItemPolicy = new CacheItemPolicy();

            cacheItemPolicy.AbsoluteExpiration = DateTimeOffset.MaxValue;
            cacheItemPolicy.SlidingExpiration  = TimeSpan.FromDays(365);
            cacheItemPolicy.Priority           = CacheItemPriority.Default;

            var cacheItem = new CacheItem(XmlFileKey, mappings);

            _memoryCache.Add(cacheItem, cacheItemPolicy);
            return((IEnumerable <T>)mappings);
        }
Exemplo n.º 3
0
        /// <summary>
        ///This method used for get array list.
        /// </summary>
        /// <param name="spName">pass store processer name</param>
        /// <param name="tableName">pass table name</param>
        /// <param name="lastCheckedDateTime">pass last checked datetime</param>
        /// <param name="className">pass class name</param>
        /// <param name="assemblyName">pass assembly name</param>
        /// <returns></returns>
        private async Task <ArrayList> GetArrayList(string spName, string tableName, DateTime lastCheckedDateTime, string className, string assemblyName)
        {
            RefectorUtility     utility    = new RefectorUtility();
            List <SqlParameter> parameters = new List <SqlParameter>
            {
                new SqlParameter("@TableName", SqlDbType.VarChar)
                {
                    Value = tableName
                },
                new SqlParameter("@LastCheckedDateTime", SqlDbType.DateTime)
                {
                    Value = lastCheckedDateTime
                }
            };
            ArrayList arrayList = new ArrayList();

            using (var dataReader = await ExecuteStoreProcedure(spName, parameters))
            {
                Dictionary <string, string> columnRow = new Dictionary <string, string>();
                //This will initialize dictionary with all the column names
                while (dataReader.Read())
                {
                    for (int i = 0; i < dataReader.FieldCount; i++)
                    {
                        if (columnRow.ContainsKey(dataReader.GetName(i)))
                        {
                            columnRow[dataReader.GetName(i)] = Convert.ToString(dataReader[i]);
                        }
                        else
                        {
                            columnRow.Add(dataReader.GetName(i), Convert.ToString(dataReader[i]));
                        }
                    }
                    var row = utility.ParseColumn(utility.GetTypeFromString(className, assemblyName), columnRow);
                    arrayList.Add(row);
                }
            }
            return(arrayList);
        }
Exemplo n.º 4
0
        private IEnumerable <string> GetUpdateStatement(string tblName, IEnumerable <object> data)
        {
            RefectorUtility utility              = new RefectorUtility();
            IFileReader     file                 = new XmlFileReader();
            var             assembly             = Assembly.GetExecutingAssembly();
            var             manifestResourceName = assembly.GetManifestResourceNames().First();
            var             parsedContent        = file.Parse <TableClassMapping>(assembly.GetManifestResourceStream(manifestResourceName));
            var             mappedType           = parsedContent.First(x => x.TableName == tblName);
            var             tableType            = utility.GetTypeFromString(mappedType.ClassName, mappedType.AssemblyName);
            string          updateFormat         = "UPDATE [" + tblName + "] SET {0} WHERE Id = {1}";
            List <string>   updateQuery          = new List <string>();
            List <string>   finalstring          = new List <string>();

            foreach (dynamic o in data)
            {
                dynamic objeInstance = JsonConvert.DeserializeObject(o.ToString(), tableType);
                foreach (PropertyInfo propertyInfo in objeInstance.GetType().GetProperties())
                {
                    if (propertyInfo.Name == "Id")
                    {
                        continue;
                    }
                    if (propertyInfo.PropertyType == typeof(DateTime))
                    {
                        dynamic dateTime = propertyInfo.GetValue(objeInstance);
                        updateQuery.Add(string.Format("[{0}] = N'{1}'", propertyInfo.Name, dateTime.ToString("yyyy-MM-dd HH:mm:ss")));
                    }
                    else
                    {
                        updateQuery.Add(string.Format("[{0}] = N'{1}'", propertyInfo.Name, propertyInfo.GetValue(objeInstance)));
                    }
                }
                finalstring.Add(string.Format(updateFormat, string.Join(",", updateQuery), objeInstance.Id));
                updateQuery.Clear();
            }
            return(finalstring);
        }