Ejemplo n.º 1
0
        public static T Read <T>(IDictionary <string, object> data, Type instanceType)
        {
            if (null != data)
            {
                FastType reflection = FastType.Get(instanceType);

                object instance = Activator.CreateInstance(instanceType);

                foreach (String key in data.Keys)
                {
                    FastProperty prop =
                        reflection.Setters.SingleOrDefault(p => MatchColumnName(p.Name, key));

                    if (null != prop)
                    {
                        prop.SetValue(instance, data[key].ConvertToType(prop.Type));
                    }
                }
                return((T)instance);
            }
            else
            {
                return(default(T));
            }
        }
Ejemplo n.º 2
0
        internal static T DoRead <T>(IDataReader reader, TableMapping mapping)
        {
            object instance = Activator.CreateInstance(typeof(T));

            foreach (Column column in mapping.Table.Columns)
            {
                FastProperty prop = column.Property;

                if (null != prop)
                {
                    //TODO : 更好的处理可能出现列名不存在的情况
                    bool   exists = false;
                    object value  = null;
                    try
                    {
                        value  = reader[column.Name];
                        exists = true;
                    }
                    catch (IndexOutOfRangeException e)
                    {
                        //找不到列名抛出此错误,忽略掉
                    }

                    if (exists)
                    {
                        prop.SetValue(instance, value.ConvertToType(prop.Type));
                    }
                }
            }
            return((T)instance);
        }
Ejemplo n.º 3
0
        private static void WriteToProperty(object item, FastProperty prop, object value, ICollection <ConvertProblem> problems)
        {
            var pi = prop.Property;

            if (!pi.CanWrite)
            {
                return;
            }

            try
            {
                if (value != null && !Equals(value, ""))
                {
                    Type destType = pi.PropertyType;

                    if (destType == typeof(bool) && Equals(value, pi.Name))
                    {
                        value = true;
                    }

                    if (pi.PropertyType.IsAssignableFrom(value.GetType()))
                    {
                        prop.SetValue(item, value);
                        return;
                    }

                    if (pi.PropertyType.IsNullable())
                    {
                        destType = pi.PropertyType.GetGenericArguments()[0];
                    }

                    prop.SetValue(item, value.Convert(destType));
                }
            }
            catch (Exception ex)
            {
                problems.Add(new ConvertProblem {
                    Item = item, Property = pi, AttemptedValue = value, Exception = ex
                });
            }
        }
Ejemplo n.º 4
0
        private static T Read <T>(IDataReader reader,
                                  Type type,
                                  PropertyMapping[] mappings)
        {
            object instance = Activator.CreateInstance(type);

            foreach (PropertyMapping mapping in mappings)
            {
                FastProperty prop = mapping.Prop;

                prop.SetValue(instance, reader.GetValue(mapping.Index).ConvertToType(prop.Type));
            }
            return((T)instance);
        }
Ejemplo n.º 5
0
        protected override void AttachToComponentRegistration(IComponentRegistry componentRegistry, IComponentRegistration registration)
        {
            var userProperty = FindUserProperty(registration.Activator.LimitType);

            if (userProperty == null)
            {
                return;
            }

            var fastProperty = new FastProperty(userProperty);

            registration.Activated += (sender, e) =>
            {
                if (DataSettings.DatabaseIsInstalled())
                {
                    Localizer localizer = e.Context.Resolve <IText>().Get;
                    fastProperty.SetValue(e.Instance, localizer);
                }
            };
        }
Ejemplo n.º 6
0
        protected override void AttachToComponentRegistration(IComponentRegistry componentRegistry, IComponentRegistration registration)
        {
            var querySettingsProperty = FindQuerySettingsProperty(registration.Activator.LimitType);

            if (querySettingsProperty == null)
            {
                return;
            }

            var fastProperty = new FastProperty(querySettingsProperty);

            registration.Activated += (sender, e) =>
            {
                if (DataSettings.DatabaseIsInstalled())
                {
                    var querySettings = e.Context.Resolve <DbQuerySettings>();
                    fastProperty.SetValue(e.Instance, querySettings);
                }
            };
        }
Ejemplo n.º 7
0
        protected override void AttachToComponentRegistration(IComponentRegistry componentRegistry, IComponentRegistration registration)
        {
            // Look for first settable property of type "ICommonServices" and inject
            var servicesProperty = FindCommonServicesProperty(registration.Activator.LimitType);

            if (servicesProperty == null)
            {
                return;
            }

            var fastProperty = new FastProperty(servicesProperty);

            registration.Activated += (sender, e) =>
            {
                if (DataSettings.DatabaseIsInstalled())
                {
                    var services = e.Context.Resolve <ICommonServices>();
                    fastProperty.SetValue(e.Instance, services);
                }
            };
        }