コード例 #1
0
ファイル: DbAccessLayer.cs プロジェクト: radtek/DataAccess
        internal async Task <IEnumerable <object> > EnumerateDataRecordsAsync(IDbCommand query,
                                                                              bool egarLoading,
                                                                              DbClassInfoCache type,
                                                                              CommandBehavior executionHint = CommandBehavior.Default)
        {
            var resultList = new List <object>();

            if (!egarLoading)
            {
                await EnumerateAsync(query,
                                     record =>
                {
                    resultList.Add(SetPropertysViaReflection(type, EagarDataRecord.WithExcludedFields(record)));
                },
                                     executionHint);
            }
            else
            {
                var recordCache = new List <EagarDataRecord>();
                await EnumerateAsync(query,
                                     record => { recordCache.Add(EagarDataRecord.WithExcludedFields(record)); }, executionHint);

                resultList.AddRange(recordCache
                                    .Select(f => SetPropertysViaReflection(type, f))
                                    .ToArray());
            }

            return(resultList);
        }
コード例 #2
0
 public static NonObservableDbCollection <T> FromXml(string xml)
 {
     return(new NonObservableDbCollection <T>(
                XmlDataRecord.TryParse(xml,
                                       typeof(T), false)
                .CreateListOfItems()
                .Select(item => typeof(T)
                        .GetClassInfo()
                        .SetPropertysViaReflection(EagarDataRecord.WithExcludedFields(item)))));
 }
コード例 #3
0
ファイル: DbAccessLayer.cs プロジェクト: radtek/DataAccess
        internal List <List <EagarDataRecord> > EnumerateMarsDataRecords(
            IDbCommand query)
        {
            Database.PrepaireRemoteExecution(query);
            try
            {
                if (ThreadSave)
                {
                    Monitor.Enter(_lockRoot);
                }

                return(Database.Run(
                           s =>
                {
                    var records = new List <List <EagarDataRecord> >();
                    using (query)
                    {
                        query.Connection = query.Connection ?? s.GetConnection();
                        query.Transaction = query.Transaction ?? s.ConnectionController.Transaction;
                        using (var dr = query.ExecuteReader())
                        {
                            try
                            {
                                do
                                {
                                    var resultSet = new List <EagarDataRecord>();
                                    while (dr.Read())
                                    {
                                        resultSet.Add(EagarDataRecord.WithExcludedFields(dr));
                                    }

                                    records.Add(resultSet);
                                } while (dr.NextResult());
                            }
                            catch (Exception ex)
                            {
                                RaiseFailedQuery(this, query, ex);
                                throw;
                            }
                        }
                    }

                    return records;
                }));
            }
            finally
            {
                if (ThreadSave)
                {
                    Monitor.Exit(_lockRoot);
                }
            }
        }
コード例 #4
0
        public EagarDataRecord[][] ExecuteMARSCommand(DbAccessLayer db, IDbCommand query, out int recordsAffected)
        {
            var recordsAffectedA = 0;
            var result           = db.Database.Run(
                s =>
            {
                var records = new List <EagarDataRecord[]>();
                using (query)
                {
                    query.Connection  = query.Connection ?? s.GetConnection();
                    query.Transaction = query.Transaction ?? s.ConnectionController.Transaction;
                    using (var dr = query.ExecuteReader())
                    {
                        recordsAffectedA = dr.RecordsAffected;
                        try
                        {
                            do
                            {
                                var resultSet = new List <EagarDataRecord>();
                                while (dr.Read())
                                {
                                    resultSet.Add(EagarDataRecord.WithExcludedFields(dr));
                                }

                                records.Add(resultSet.ToArray());
                            } while (dr.NextResult());
                        }
                        catch (Exception ex)
                        {
                            db.RaiseFailedQuery(this, query, ex);
                            throw;
                        }
                    }
                }

                return(records.ToArray());
            });

            recordsAffected = recordsAffectedA;
            return(result);
        }
コード例 #5
0
ファイル: DbAccessLayer.cs プロジェクト: radtek/DataAccess
        public static object ReflectionPropertySet(
            DbConfig config,
            object instance,
            DbClassInfoCache info,
            EagarDataRecord reader,
            ReflectionSetCacheModel cacheModel,
            DbAccessType?dbAccessType)
        {
            if (instance == null)
            {
                throw new ArgumentNullException(nameof(instance));
            }

            if (info == null)
            {
                throw new ArgumentNullException(nameof(info));
            }

            if (reader == null)
            {
                return(instance);
            }

            //Left c# property name and right the object to read from the reader
            //var listofpropertys = new Dictionary<string, object>();

            var propertys = info.Propertys.ToArray();
            var instanceOfFallbackList = new Dictionary <string, object>();
            IDictionary <int, DbPropertyInfoCache> cache = new Dictionary <int, DbPropertyInfoCache>();

            for (var i = 0; i < reader.FieldCount; i++)
            {
                info.Propertys.TryGetValue(info.SchemaMappingDatabaseToLocal(reader.GetName(i)), out var val);
                cache.Add(i, val);
            }

            //var containsKey = cacheModel?.Cache.ContainsKey(info.Type);
            //if (containsKey != true)
            //{
            //	for (var i = 0; i < reader.FieldCount; i++)
            //	{
            //		info.Propertys.TryGetValue(info.SchemaMappingDatabaseToLocal(reader.GetName(i)), out var val);
            //		cache.Add(i, val);
            //	}

            //	if (containsKey == false)
            //	{
            //		cacheModel.Cache[info.Type] = cache;
            //	}
            //}
            //if (containsKey == true)
            //{
            //	cache = cacheModel.Cache[info.Type];
            //}

            for (var i = 0; i < reader.FieldCount; i++)
            {
                var property = cache[i];
                var value    = reader.GetValue(i);

                if (property != null)
                {
                    var attributes = property.Attributes;
                    var valueConverterAttributeModel =
                        attributes.FirstOrDefault(s => s.Attribute is ValueConverterAttribute);

                    //Should the SQL value be converted
                    if (valueConverterAttributeModel != null)
                    {
                        var converter = valueConverterAttributeModel.Attribute as ValueConverterAttribute;
                        //Create the converter and then convert the value before everything else
                        var valueConverter = converter.CreateConverter();
                        value = valueConverter.Convert(value, property.PropertyInfo.PropertyType, converter.Parameter,
                                                       CultureInfo.CurrentCulture);
                    }

                    var xmlAttributeModel =
                        attributes.FirstOrDefault(s => s.Attribute is FromXmlAttribute);

                    //should the Content be considerd as XML text?
                    if (xmlAttributeModel != null)
                    {
                        //Get the XML text and check if its null or empty
                        var xmlStream = value.ToString();
                        if (string.IsNullOrEmpty(xmlStream))
                        {
                            continue;
                        }

                        //Check for List
                        //if this is a list we are expecting other entrys inside
                        if (property.CheckForListInterface())
                        {
                            //target Property is of type list
                            //so expect a xml valid list Take the first element and expect the propertys inside this first element
                            var record = XmlDataRecord.TryParse(xmlStream,
                                                                property.PropertyInfo.PropertyType.GetGenericArguments().FirstOrDefault(), false,
                                                                config);
                            var xmlDataRecords = record.CreateListOfItems();

                            var genericArguments =
                                config.GetOrCreateClassInfoCache(
                                    property.PropertyInfo.PropertyType.GetGenericArguments().FirstOrDefault());
                            var enumerableOfItems =
                                xmlDataRecords.Select(
                                    s => genericArguments
                                    .SetPropertysViaReflection(EagarDataRecord.WithExcludedFields(s),
                                                               dbAccessType, config)).ToList();
                            object castedList;

                            if (genericArguments.Type.IsClass &&
                                genericArguments.Type.GetInterface("INotifyPropertyChanged") != null)
                            {
                                var caster =
                                    typeof(DbCollection <>).MakeGenericType(genericArguments.Type)
                                    .GetConstructor(new[] { typeof(IEnumerable) });
                                castedList = caster.Invoke(new object[] { enumerableOfItems });
                            }
                            else
                            {
                                var caster =
                                    typeof(NonObservableDbCollection <>).MakeGenericType(genericArguments.Type)
                                    .GetConstructor(new[] { typeof(IEnumerable) });
                                castedList = caster.Invoke(new object[] { enumerableOfItems });
                            }

                            property.Setter.Invoke(instance, castedList);
                        }
                        else
                        {
                            var classInfo = config.GetOrCreateClassInfoCache(property
                                                                             .PropertyInfo
                                                                             .PropertyType);

                            var xmlDataRecord = XmlDataRecord.TryParse(xmlStream, property.PropertyInfo.PropertyType,
                                                                       true, config);

                            //the t
                            var xmlSerilizedProperty = classInfo.SetPropertysViaReflection(
                                EagarDataRecord.WithExcludedFields(xmlDataRecord), dbAccessType,
                                config);
                            property.Setter.Invoke(instance, xmlSerilizedProperty);
                        }
                    }
                    else if (value is DBNull || value == null)
                    {
                        //property.Setter.Invoke(instance, new object[] {null});
                    }
                    else if (value is IEnumerable <EagarDataRecord> navigationValue)
                    {
                        Type targetType;
                        if (property.CheckForListInterface())
                        {
                            targetType = property.PropertyType.GetElementType();
                            if (targetType == null)
                            {
                                targetType = property.PropertyType.GetGenericArguments().FirstOrDefault();
                            }
                        }
                        else
                        {
                            targetType = property.PropertyType;
                        }

                        var classInfo   = config.GetOrCreateClassInfoCache(targetType);
                        var enumeration = navigationValue.Select(subReader =>
                        {
                            bool created;
                            var source = CreateInstance(classInfo, subReader, out created);
                            if (created)
                            {
                                return(source);
                            }
                            return(ReflectionPropertySet(config, source, classInfo, subReader, cacheModel, dbAccessType));
                        }).ToArray();

                        if (property.CheckForListInterface())
                        {
                            var caster =
                                typeof(DbCollection <>).MakeGenericType(targetType)
                                .GetConstructor(new[] { typeof(IEnumerable) });
                            var castedList = caster.Invoke(new object[] { enumeration });
                            property.Setter.Invoke(instance, castedList);
                        }
                        else
                        {
                            property.Setter.Invoke(instance, enumeration.FirstOrDefault());
                        }
                    }
                    else
                    {
                        object changedType = value;
                        if (property.PropertyType.IsInstanceOfType(value))
                        {
                            changedType = value;
                        }
                        else
                        {
                            if (!DataConverterExtensions.ChangeType(ref changedType, property.PropertyInfo.PropertyType))
                            {
                                continue;
                            }
                        }

                        //if (value.GetType() != property.PropertyInfo.PropertyType)
                        //{
                        //	changedType = DataConverterExtensions.ChangeType(value, property.PropertyInfo.PropertyType);
                        //}
                        //else
                        //{
                        //	changedType = value;
                        //}

                        property.Setter.Invoke(instance, changedType);
                    }
                }
                //This variable is null if we tried to find a property with the LoadNotImplimentedDynamicAttribute but did not found it
                else if (instanceOfFallbackList != null)
                {
                    //no property found Look for LoadNotImplimentedDynamicAttribute property to include it

                    if (instanceOfFallbackList.Any())
                    {
                        instanceOfFallbackList.Add(reader.GetName(i), value);
                    }
                    else
                    {
                        var maybeFallbackProperty =
                            propertys.FirstOrDefault(
                                s => s.Value.Attributes.Any(e => e.Attribute is LoadNotImplimentedDynamicAttribute));
                        if (maybeFallbackProperty.Value != null)
                        {
                            instanceOfFallbackList =
                                (Dictionary <string, object>)maybeFallbackProperty.Value.Getter.Invoke(instance);
                            if (instanceOfFallbackList == null)
                            {
                                instanceOfFallbackList = new Dictionary <string, object>();
                                maybeFallbackProperty.Value.Setter.Invoke(instance, instanceOfFallbackList);
                            }

                            instanceOfFallbackList.Add(reader.GetName(i), value);
                        }
                        else
                        {
                            instanceOfFallbackList = null;
                        }
                    }
                }
            }

            //foreach (var item in listofpropertys)
            //{
            //	var property = propertys.FirstOrDefault(s => s.PropertyName == item.Key);

            //}
            return(instance);
        }