Ejemplo n.º 1
0
        /// <summary>Return the index of the named field.</summary>
        /// <returns>The index of the named field.</returns>
        /// <param name="name">The name of the field to find. </param>
        public int GetOrdinal(string name)
        {
            var index     = 0;
            var localName = _classTypeCache.SchemaMappingDatabaseToLocal(name);

            foreach (var key in _classTypeCache.Propertys.Keys)
            {
                if (key == localName)
                {
                    return(index);
                }
                index++;
            }
            return(-1);
        }
Ejemplo n.º 2
0
        internal static string GetDbToLocalSchemaMapping(this DbClassInfoCache type, string name)
        {
            if (IsAnonymousType(type))
            {
                return(name);
            }

            return(type.SchemaMappingDatabaseToLocal(name));
        }
Ejemplo n.º 3
0
        public static object ReflectionPropertySet(
            DbConfig config,
            object instance,
            DbClassInfoCache info,
            IDataRecord reader,
            Dictionary <int, DbPropertyInfoCache> cache,
            DbAccessType?dbAccessType)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }

            if (info == null)
            {
                throw new ArgumentNullException("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>();

            if (cache == null)
            {
                cache = new Dictionary <int, DbPropertyInfoCache>();
                for (var i = 0; i < reader.FieldCount; i++)
                {
                    DbPropertyInfoCache val = null;
                    info.Propertys.TryGetValue(info.SchemaMappingDatabaseToLocal(reader.GetName(i)), out val);
                    cache.Add(i, val);
                }
            }

            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(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(xmlDataRecord, dbAccessType,
                                                                                           config);
                            property.Setter.Invoke(instance, xmlSerilizedProperty);
                        }
                    }
                    else if (value is DBNull || value == null)
                    {
                        property.Setter.Invoke(instance, new object[] { null });
                    }
                    else
                    {
                        object changedType;
                        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);
        }
Ejemplo n.º 4
0
		public static object ReflectionPropertySet(
			DbConfig config,
			object instance,
			DbClassInfoCache info,
			IDataRecord reader,
			Dictionary<int, DbPropertyInfoCache> cache,
			DbAccessType? dbAccessType)
		{
			if (instance == null) throw new ArgumentNullException("instance");
			if (info == null) throw new ArgumentNullException("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>();

			if (cache == null)
			{
				cache = new Dictionary<int, DbPropertyInfoCache>();
				for (var i = 0; i < reader.FieldCount; i++)
				{
					DbPropertyInfoCache val = null;
					info.Propertys.TryGetValue(info.SchemaMappingDatabaseToLocal(reader.GetName(i)), out val);
					cache.Add(i, val);
				}
			}

			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 => DbAccessLayerHelper.SetPropertysViaReflection(genericArguments, 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)});

								Debug.Assert(caster != null, "caster != null");

								castedList = caster.Invoke(new object[] {enumerableOfItems});
							}
							else
							{
								var caster =
									typeof(NonObservableDbCollection<>).MakeGenericType(genericArguments.Type)
										.GetConstructor(new[] {typeof(IEnumerable)});

								Debug.Assert(caster != null, "caster != null");

								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 = DbAccessLayerHelper.SetPropertysViaReflection(classInfo, xmlDataRecord, dbAccessType,
								config);
							property.Setter.Invoke(instance, xmlSerilizedProperty);
						}
					}
					else if (value is DBNull || value == null)
					{
						property.Setter.Invoke(instance, new object[] {null});
					}
					else
					{
						object changedType;
						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);

			//}

			if (reader is EgarDataRecord)
			{
				(reader as IDisposable).Dispose();
			}

			return instance;
		}