Exemplo n.º 1
0
        internal void SerializeSingle(object instanceEntity, Type type, ComplexType complextType,
                                      EntityAccessor entityAccessor, Dictionary <string, int> columns, DbDataReader dbReader)
        {
            var trackable = instanceEntity as ITrackable;

            if (trackable != null)
            {
                //logger.Log(LogLevel.Debug, string.Format("Stopping and clearing Entity {0}  change tracker.", type));
                trackable.ChangeTracker.StopAndClear();
                //logger.Log(LogLevel.Debug, "Initializing trackable entity change tracker.");
                trackable.ChangeTracker.Init();
            }

            foreach (var keyVal in entityAccessor.Properties)
            {
                /* NOTE: Intentionally going only 1 level up the inheritance. something like :
                 *  SuperSuperClass
                 *      SuperClass
                 *          Class
                 *
                 *  SuperSuperClass if is a mapped entity its properties will be ignored. May be implement this later on.
                 *  For now too ugly don't want to touch.
                 */
                var prop = complextType.GetProperty(keyVal.Key);
                if (prop != null)
                {
                    int ordinal;
                    if (columns.TryGetValue(prop.ColumnName, out ordinal))
                    {
                        var val       = dbReader[ordinal];
                        var fieldType = dbReader.GetFieldType(ordinal);
                        if ((fieldType == keyVal.Value.PropertyType) && (val != DBNull.Value) && (val != null))
                        {
                            keyVal.Value.SetMethod(instanceEntity, val);
                            LoadInitialValueForInTracker(trackable, prop.PropertyName, val);
                        }
                        else if (keyVal.Value.PropertyType.IsEnum)
                        {
                            var enumVal = TypeConverterStore.ConvertToEnum(keyVal.Value.PropertyType, val);
                            keyVal.Value.SetMethod(instanceEntity, enumVal);
                            LoadInitialValueForInTracker(trackable, prop.PropertyName, enumVal);
                        }
                        else
                        {
                            var converter      = TypeConverterStore.GetConverterFactoryMethod(keyVal.Value.PropertyType);
                            var convertedValue = converter.Invoke(val);
                            keyVal.Value.SetMethod(instanceEntity, convertedValue);
                            LoadInitialValueForInTracker(trackable, prop.PropertyName, convertedValue);
                        }
                    }
                }
            }

            if (trackable != null)
            {
                trackable.ChangeTracker.Start();
                trackable.Version = trackable.ChangeTracker.Version;
                //logger.Log(LogLevel.Debug, string.Format("Restarted tracker -- version {0}.", trackable.Version));
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="EntitySerializer"/> class.
        /// </summary>
        /// <param name="settings">The settings.</param>
        /// <param name="typeConverterStore">The type converter.</param>
        public EntitySerializer(IDatabaseSettings settings, ITypeConverterStore typeConverterStore)
        {
            if (settings == null)
            {
                throw new ArgumentNullException("settings");
            }

            if (typeConverterStore == null)
            {
                typeConverterStore = new TypeConverterStore();
            }

            this.settings      = settings;
            TypeConverterStore = typeConverterStore;
        }
Exemplo n.º 3
0
        public object ReadFieldData(Type expectedType, object value)
        {
            if (value == null)
            {
                return(null);
            }

            Type actualType = value.GetType();

            if (object.Equals(actualType, expectedType)) //if (actualType.Equals(expectedType))
            {
                return(value);
            }
            else
            {
                var converter = TypeConverterStore.GetConverterFactoryMethod(expectedType);
                return(converter.Invoke(value));
            }
        }
Exemplo n.º 4
0
        object ReadField(object instanceEntity, ITrackable trackable, EntityAccessor entityAccessor, Property prop, Type type, int ordinal, DbDataReader dbReader)
        {
            var accessor = entityAccessor.Properties[prop.PropertyName];

            if (accessor == null)
            {
                throw new GoliathDataException("Could not find accessor for " + instanceEntity.ToString() + "." + prop.PropertyName);
            }

            var val       = dbReader[ordinal];
            var fieldType = dbReader.GetFieldType(ordinal);

            if (val == DBNull.Value || val == null)
            {
                return(null);
            }

            if ((fieldType == accessor.PropertyType) && (val != DBNull.Value))
            {
                accessor.SetMethod(instanceEntity, val);
                LoadInitialValueForInTracker(trackable, prop.PropertyName, val);
                return(val);
            }
            else if (accessor.PropertyType.IsEnum)
            {
                var enumVal = TypeConverterStore.ConvertToEnum(accessor.PropertyType, val);
                accessor.SetMethod(instanceEntity, enumVal);
                LoadInitialValueForInTracker(trackable, prop.PropertyName, enumVal);
                return(enumVal);
            }
            else
            {
                var converter      = TypeConverterStore.GetConverterFactoryMethod(accessor.PropertyType);
                var convertedValue = converter.Invoke(val);
                accessor.SetMethod(instanceEntity, convertedValue);
                LoadInitialValueForInTracker(trackable, prop.PropertyName, convertedValue);
                return(convertedValue);
            }
        }