private static object GetValue(TypeExtension typeExtension, MemberAccessor member, string elemName, out bool isSet)
		{
			var value = typeExtension[member.Name][elemName].Value;

			isSet = value != null;

			return value;
		}
		private static object GetValue(ObjectMapper mapper, MemberAccessor member, string elemName, out bool isSet)
		{
			object value = mapper.Extension[member.Name][elemName].Value;

			isSet = value != null;

			return value;
		}
		public virtual bool GetMapIgnore(TypeExtension typeExtension, MemberAccessor member, out bool isSet)
		{
			isSet = false;

			return
				TypeHelper.IsScalar(member.Type) == false;// ||
				//(member.MemberInfo is FieldInfo && ((FieldInfo)member.MemberInfo).IsLiteral);
		}
Beispiel #4
0
        public ToDefinition(MemberInfo memberInfo)
        {
            Require.NotNull(memberInfo, "memberInfo");

            // We need a getter as well for situations where we are mapping into an existing object and need to merge into
            // 'child' members, rather than just creating new instances of them.
            Accessor = new MemberAccessor(memberInfo);
        }
		public override string GetFieldStorage(TypeExtension typeExtension, MemberAccessor member, out bool isSet)
		{
			var value = GetValue(typeExtension, member, "FieldStorage", out isSet);

			if (value != null)
				return value.ToString();

			return base.GetFieldStorage(typeExtension, member, out isSet);
		}
		public override bool GetInheritanceDiscriminator(TypeExtension typeExtension, MemberAccessor member, out bool isSet)
		{
			var value = GetValue(typeExtension, member, "IsInheritanceDiscriminator", out isSet);

			if (value != null)
				return TypeExtension.ToBoolean(value);

			return base.GetInheritanceDiscriminator(typeExtension, member, out isSet);
		}
		public override bool GetMapIgnore(TypeExtension typeExtension, MemberAccessor member, out bool isSet)
		{
			var value = GetValue(typeExtension, member, "MapIgnore", out isSet);

			if (value != null)
				return TypeExtension.ToBoolean(value);

			return base.GetMapIgnore(typeExtension, member, out isSet) || GetAssociation(typeExtension, member) != null;
		}
		public override string GetFieldName(ObjectMapper mapper, MemberAccessor member, out bool isSet)
		{
			object value = GetValue(mapper, member, "MapField", out isSet);

			if (value != null)
				return value.ToString();

			return base.GetFieldName(mapper, member, out isSet);
		}
		public override bool GetIgnore(ObjectMapper mapper, MemberAccessor member, out bool isSet)
		{
			object value = GetValue(mapper, member, "MapIgnore", out isSet);

			if (value != null)
				return TypeExtension.ToBoolean(value);

			return base.GetIgnore(mapper, member, out isSet);
		}
		public override bool GetIgnore(ObjectMapper mapper, MemberAccessor member, out bool isSet)
		{
			if (IsLinqObject(mapper.TypeAccessor.Type))
			{
				isSet = true;
				return member.GetAttribute<ColumnAttribute>() == null;
			}

			return base.GetIgnore(mapper, member, out isSet);
		}
		public MemberAccessorTests() {
			person = new Person();
			person.NameField = "John Smith";
			person.Forename = "John";
			person.Address = new Address {Country = new Country {Name = "United States"}};

			nameFieldAccessor = MemberAccessor<Person>.From(x => x.NameField);
			forenameAccessor = MemberAccessor<Person>.From(x => x.Forename);
			countryNameAccessor = MemberAccessor<Person>.From(x => x.Address.Country.Name);
		}
Beispiel #12
0
 void SetEnumConverterInternal(MemberAccessor ma, MappingSchema ms)
 {
     if (_valueConverter == null)
     {
         _valueConverter = o => ms.MapEnumToValue(o, ma, true);
     }
     else
     {
         var converter = _valueConverter;
         _valueConverter = o => ms.MapEnumToValue(converter(o), ma, true);
     }
 }
		public override string GetFieldStorage(TypeExtension typeExtension, MemberAccessor member, out bool isSet)
		{
			foreach (var p in _list)
			{
				var name = p.GetFieldStorage(typeExtension, member, out isSet);

				if (isSet)
					return name;
			}

			return base.GetFieldStorage(typeExtension, member, out isSet);
		}
		public override bool GetInheritanceDiscriminator(TypeExtension typeExtension, MemberAccessor member, out bool isSet)
		{
			foreach (var p in _list)
			{
				var value = p.GetInheritanceDiscriminator(typeExtension, member, out isSet);

				if (isSet)
					return value;
			}

			return base.GetInheritanceDiscriminator(typeExtension, member, out isSet);
		}
		public override bool GetIgnore(ObjectMapper mapper, MemberAccessor member, out bool isSet)
		{
			foreach (MetadataProviderBase p in _list)
			{
				bool ignore = p.GetIgnore(mapper, member, out isSet);

				if (isSet)
					return ignore;
			}

			return base.GetIgnore(mapper, member, out isSet);
		}
		public override bool GetTrimmable(ObjectMapper mapper, MemberAccessor member, out bool isSet)
		{
			if (member.Type == typeof(string))
			{
				object value = GetValue(mapper, member, "Trimmable", out isSet);

				if (value != null)
					return TypeExtension.ToBoolean(value);
			}

			return base.GetTrimmable(mapper, member, out isSet);
		}
		public override string GetFieldName(ObjectMapper mapper, MemberAccessor member, out bool isSet)
		{
			foreach (MetadataProviderBase p in _list)
			{
				string name = p.GetFieldName(mapper, member, out isSet);

				if (isSet)
					return name;
			}

			return base.GetFieldName(mapper, member, out isSet);
		}
        public void When_Member_Is_WriteOnlyProperty_Then_Constructed_Correctly()
        {
            // Act
            var sut = new MemberAccessor(ReadWriteTestEntity.WriteOnlyPropertyMemberInfo);

            // Asert
            Assert.AreEqual(sut.Type, MemberAccessorType.Property);
            Assert.AreEqual(sut.MemberName, ReadWriteTestEntity.WriteOnlyPropertyName);
            Assert.AreEqual(typeof(ReadWriteTestEntity), sut.DeclaringType);
            Assert.AreEqual(typeof(int), sut.MemberType);
            Assert.IsNull(sut.Getter);
            Assert.NotNull(sut.Setter);
        }
Beispiel #19
0
		internal void SetEnumConverter(MemberAccessor ma, MappingSchema ms)
		{
			if (!_isEnumConverterSet)
			{
				_isEnumConverterSet = true;

				if (EnumTypes == null)
					EnumTypes = new List<Type>();

				EnumTypes.Add(ma.Type);

				SetEnumConverterInternal(ma, ms);
			}
		}
        public void QueryInstanceBindingFlag()
        {
            var initialQuery = new MemberAccessor<MemberDescriptor>();
              //var query = initialQuery.Where(x => x.AccessMode == MemberAccessMode.Instance).ToList();

              var query1 =
            initialQuery.Where(
              x =>
            x.MemberTypes == MemberTypes.Property
            && x.AccessMode == MemberAccessMode.Instance
            && (x.Visibility == MemberVisibility.NonPublic || x.Visibility == MemberVisibility.Public)
            && x.Name == "Test"
            ).ToList();
        }
Beispiel #21
0
		void SetEnumConverterInternal(MemberAccessor ma, MappingSchema ms)
		{
			if (_valueConverter == null)
			{
				_valueConverter = o => ms.MapEnumToValue(o, ma, true);
			}
			else
			{
				var converter = _valueConverter;
				_valueConverter = o => ms.MapEnumToValue(converter(o), ma, true);
			}
			// update system type in SqlValue :-/
			var tmp = Value;
		}
		public override bool GetNullable(ObjectMapper mapper, MemberAccessor member, out bool isSet)
		{
			if (IsLinqObject(mapper.TypeAccessor.Type))
			{
				var attr = member.GetAttribute<ColumnAttribute>();

				if (attr != null)
				{
					isSet = true;
					return attr.CanBeNull;
				}
			}

			return base.GetNullable(mapper, member, out isSet);
		}
		public override string GetFieldName(ObjectMapper mapper, MemberAccessor member, out bool isSet)
		{
			if (IsLinqObject(mapper.TypeAccessor.Type))
			{
				ColumnAttribute a = member.GetAttribute<ColumnAttribute>();

				if (a != null && !string.IsNullOrEmpty(a.Name))
				{
					isSet = true;
					return a.Name;
				}
			}

			return base.GetFieldName(mapper, member, out isSet);
		}
		public override string GetFieldStorage(TypeExtension typeExtension, MemberAccessor member, out bool isSet)
		{
			if (IsLinqObject(member.TypeAccessor.Type))
			{
				var a = member.GetAttribute<ColumnAttribute>();

				if (a != null && !string.IsNullOrEmpty(a.Name))
				{
					isSet = true;
					return a.Storage;
				}
			}

			return base.GetFieldStorage(typeExtension, member, out isSet);
		}
Beispiel #25
0
        public override Association GetAssociation(TypeExtension typeExtension, MemberAccessor member)
        {
            var aa = member.GetAttribute <AssociationAttribute>();

            if (aa == null)
            {
                return(base.GetAssociation(typeExtension, member));
            }

            return(new Association(
                       member,
                       aa.GetThisKeys(),
                       aa.GetOtherKeys(),
                       aa.Storage,
                       aa.CanBeNull));
        }
Beispiel #26
0
        /// <summary>
        /// Ariel Sigo
        /// Created 2016/11/22
        /// </summary>
        /// <param name="oldMember"></param>
        /// <param name="newMember"></param>
        /// <returns>true if update succeeded</returns>
        public bool UpdateMember(Member oldMember, Member newMember)
        {
            var result = false;


            if (result = (1 == MemberAccessor.UpdateMember(oldMember.MemberID, newMember.MemberID, oldMember.FirstName, newMember.FirstName, oldMember.LastName, newMember.LastName, oldMember.PhoneNumber, newMember.PhoneNumber, oldMember.Status, newMember.Status, oldMember.Birthday, newMember.Birthday, oldMember.StartDate, newMember.StartDate, oldMember.MembershipTypeID, newMember.MembershipTypeID, oldMember.MemberPoleLevel, newMember.MemberPoleLevel, oldMember.MemberAcroLevel, newMember.MemberAcroLevel, oldMember.MemberSilksLevel, newMember.MemberSilksLevel, oldMember.MemberLyraLevel, newMember.MemberLyraLevel)))
            {
                return(result);
            }

            else
            {
                result = false;
                return(result);
            }
        }
        public override bool GetMapIgnore(TypeExtension typeExtension, MemberAccessor member, out bool isSet)
        {
            if (member.GetAttribute <AssociationAttribute>() != null)
            {
                isSet = true;
                return(true);
            }

            if (IsLinqObject(member.TypeAccessor.Type))
            {
                isSet = true;
                return(member.GetAttribute <ColumnAttribute>() == null);
            }

            return(base.GetMapIgnore(typeExtension, member, out isSet));
        }
Beispiel #28
0
        internal void SetEnumConverter(MemberAccessor ma, MappingSchema ms)
        {
            if (!_isEnumConverterSet)
            {
                _isEnumConverterSet = true;

                if (EnumTypes == null)
                {
                    EnumTypes = new List <Type>();
                }

                EnumTypes.Add(ma.Type);

                SetEnumConverterInternal(ma, ms);
            }
        }
        public override bool GetTrimmable(TypeExtension typeExtension, MemberAccessor member, out bool isSet)
        {
            if (member.Type == typeof(string))
            {
                foreach (var p in _list)
                {
                    var trimmable = p.GetTrimmable(typeExtension, member, out isSet);

                    if (isSet)
                    {
                        return(trimmable);
                    }
                }
            }

            return(base.GetTrimmable(typeExtension, member, out isSet));
        }
Beispiel #30
0
        // Try to generate an accessor for a built-in primitive type.
        static bool TryGenerateAccessorPrimitive(ref MemberAccessor accessor, Type parentType, Type type, PropertyInfo property)
        {
            TypeCode typeCode = Type.GetTypeCode(type);

            // If it's not in the range of supported types (bool, DateTime, primitives) don't do the primitive accessor.
            if (typeCode < TypeCode.Boolean || typeCode > TypeCode.DateTime)
            {
                return(false);
            }

            Delegate?getter = property.GetGetMethod() !.CreateDelegate(GenericPrimitivePropertyGetter.MakeGenericType(parentType, type));
            Delegate?setter = property.GetSetMethod() !.CreateDelegate(GenericPropertySetter.MakeGenericType(parentType, type));

            accessor.Initialize(MemberAccessorType.PrimitiveProperty, getter, setter);
            accessor.PrimitiveTypeCode = typeCode;
            return(true);
        }
Beispiel #31
0
        Action <IDatabase, DbCommand, TEntity> CreateParameterBinder()
        {
            var members = _members.Select((_, i) => new MemberTuple <TEntity>(
                                              _.Name,
                                              _.DbType,
                                              BuildParameterName(i),
                                              MemberAccessor <TEntity, object> .GetGetter(_.Member)
                                              )).ToArray();

            return((db, command, entity) =>
            {
                foreach (var member in members)
                {
                    db.AddInParameter(command, member.ParameterName, member.DbType, member.Getter(entity));
                }
            });
        }
Beispiel #32
0
        public MemberBindable(MemberAccessor access, NotificationMode mode = null)
        {
            _accessor = access;
            var instance = _accessor.RootObject;
            var changed  = instance as INotifyPropertyChanged;

            if (changed != null)
            {
                changed.PropertyChanged += (sender, args) => {
                    if (args.PropertyName == access.MemberChain[0].Name)
                    {
                        NotifyChange();
                    }
                };
            }
            mode?.Subscribe(instance, this);
        }
        public override bool GetTrimmable(ObjectMapper mapper, MemberAccessor member, out bool isSet)
        {
            if (member.Type == typeof(string))
            {
                foreach (MetadataProviderBase p in _list)
                {
                    bool trimmable = p.GetTrimmable(mapper, member, out isSet);

                    if (isSet)
                    {
                        return(trimmable);
                    }
                }
            }

            return(base.GetTrimmable(mapper, member, out isSet));
        }
        public void Setup()
        {
            person           = new Person();
            person.NameField = "John Smith";
            person.Forename  = "John";
            person.Address   = new Address {
                Country = new Country {
                    Name = "United States"
                }
            };

            nameFieldAccessor = MemberAccessor <Person> .From(x => x.NameField);

            forenameAccessor = MemberAccessor <Person> .From(x => x.Forename);

            countryNameAccessor = MemberAccessor <Person> .From(x => x.Address.Country.Name);
        }
        public void Testインスタンスプロパティ()
        {
            Table1.StaticValue = "STATIC";
            Table1 obj = new Table1()
            {
                InstanceValue = "INSTANCE"
            };

            PropertyInfo prop    = obj.GetType().GetProperty("InstanceValue");
            object       actual1 = MemberAccessor.GetValue(obj, prop);

            Assert.AreEqual("INSTANCE", actual1);
            MemberAccessor.SetValue(obj, prop, "CHANGEDVALUE");
            object actual2 = MemberAccessor.GetValue(obj, prop);

            Assert.AreEqual("CHANGEDVALUE", actual2);
        }
Beispiel #36
0
        public override int GetPrimaryKeyOrder(Type type, TypeExtension typeExt, MemberAccessor member, out bool isSet)
        {
            var keyAttr = member.GetAttribute <KeyAttribute>();

            if (keyAttr != null)
            {
                isSet = true;
                var colAttr = member.GetAttribute <ColumnAttribute>();
                if (colAttr != null)
                {
                    return(colAttr.Order);
                }
                return(0);
            }

            return(base.GetPrimaryKeyOrder(type, typeExt, member, out isSet));
        }
        internal Action <IDatabase, DbCommand, TEntity> BuildCommandExecutor()
        {
            // 不带返回
            if (_identity == null && _outputs.Length == 0)
            {
                return((db, command, entity) => db.ExecuteNonQuery(command));
            }
            Action <IDatabase, TEntity> rerverseBindId = (db, entity) => { };
            Action <IDatabase, TEntity> reverseBind    = rerverseBindId;

            if (_identity != null)
            {
                var setId = MemberAccessor <TEntity, object> .GetSetter(_identity.Member);

                rerverseBindId = (db, entity) =>
                {
                    setId(entity, db.ExecuteScalar("SELECT LAST_INSERT_ROWID()"));
                };
            }

            if (_outputs.Length > 0)
            {
                var assigns = _outputs.Select(_ => MemberAccessor.GetAssign <TEntity>(_.Member)).ToArray();

                reverseBind = (db, entity) =>
                {
                    var reverseEntity = db.LoadEntity(entity);
                    foreach (var assign in assigns)
                    {
                        assign(reverseEntity, entity);
                    }
                };
            }

            return((db, command, entity) =>
            {
                using (var transactionManager = new TransactionManager())
                {
                    db.ExecuteNonQuery(command);
                    rerverseBindId(db, entity);
                    reverseBind(db, entity);

                    transactionManager.Commit();
                }
            });
        }
Beispiel #38
0
        public override bool GetMapIgnore(TypeExtension typeExtension, MemberAccessor member, out bool isSet)
        {
            if (member.IsDefined <NotMappedAttribute>() || member.IsDefined <ScaffoldColumnAttribute>() ||
                member.IsDefined <System.ComponentModel.DataAnnotations.AssociationAttribute>())
            {
                isSet = true;
                return(true);
            }

            if (member.IsDefined <ColumnAttribute>())
            {
                isSet = true;
                return(false);
            }

            return(base.GetMapIgnore(typeExtension, member, out isSet));
        }
Beispiel #39
0
        static Action <IDatabase, DbCommand, TEntity> CreateParameterBinder(MemberDescriptor[] descriptors, Func <int, string> parameterNameBuilder)
        {
            var members = descriptors.Select((_, i) => new MemberTuple <TEntity>(
                                                 _.Name,
                                                 _.DbType,
                                                 parameterNameBuilder(i),
                                                 MemberAccessor <TEntity, object> .GetGetter(_.Member)
                                                 )).ToArray();

            return((db, command, entity) =>
            {
                foreach (var member in members)
                {
                    db.AddInParameter(command, member.ParameterName, member.DbType, member.Getter(entity));
                }
            });
        }
        public Expression BuildSql(MemberAccessor ma, int idx, MethodInfo checkNullFunction, Expression context)
        {
            var expr = Expression.Call(DataReaderParam, ReflectionHelper.DataReader.GetValue, Expression.Constant(idx));

            if (checkNullFunction != null)
            {
                expr = Expression.Call(null, checkNullFunction, expr, context);
            }

            Expression mapper;

            if (TypeHelper.IsEnumOrNullableEnum(ma.Type))
            {
                var type = TypeHelper.ToNullable(ma.Type);
                mapper =
                    Expression.Convert(
                        Expression.Call(
                            Expression.Constant(MappingSchema),
                            ReflectionHelper.MapSchema.MapValueToEnumWithMemberAccessor,
                            expr,
                            Expression.Constant(ma)),
                        type);
            }
            else
            {
                MethodInfo mi;

                if (!ReflectionHelper.MapSchema.Converters.TryGetValue(ma.Type, out mi))
                {
                    mapper =
                        Expression.Convert(
                            Expression.Call(
                                Expression.Constant(MappingSchema),
                                ReflectionHelper.MapSchema.ChangeType,
                                expr,
                                Expression.Constant(ma.Type)),
                            ma.Type);
                }
                else
                {
                    mapper = Expression.Call(Expression.Constant(MappingSchema), mi, expr);
                }
            }

            return(mapper);
        }
        public override bool GetSqlIgnore(TypeExtension typeExtension, MemberAccessor member, out bool isSet)
        {
            var attr = member.GetAttribute <SqlIgnoreAttribute>();

            if (attr == null)
            {
                attr = (SqlIgnoreAttribute)TypeHelper.GetFirstAttribute(member.Type, typeof(SqlIgnoreAttribute));
            }

            if (attr != null)
            {
                isSet = true;
                return(attr.Ignore);
            }

            return(base.GetSqlIgnore(typeExtension, member, out isSet));
        }
    public void Same_Scoped_Expressions_With_Different_ToStrings_Are_Equal()
    {
        var bindable = ObservedPropertyModels.CreateFirst("");

        MemberAccessor <string> ae1, ae2;

        {
            var item1 = bindable.Second;
            ae1 = MemberAccessor.Create(() => item1.Third.Value);
        }

        {
            var item2 = bindable.Second;
            ae2 = MemberAccessor.Create(() => item2.Third.Value);
        }

        Assert.Equal(ae1, ae2);
    }
Beispiel #43
0
        protected internal virtual MemberMapper[] GetKeyFieldList(DbManager db, Type type)
        {
            string key = type.FullName + "$" + db.DataProvider.UniqueName;

            MemberMapper[] mmList = (MemberMapper[])_keyList[key];

            if (mmList == null)
            {
                TypeExtension      typeExt = TypeExtension.GetTypeExtension(type, Extensions);
                List <MemberOrder> list    = new List <MemberOrder>();

                foreach (MemberMapper mm in db.MappingSchema.GetObjectMapper(type))
                {
                    if (mm.MapMemberInfo.SqlIgnore)
                    {
                        continue;
                    }

                    MemberAccessor ma = mm.MapMemberInfo.MemberAccessor;

                    if (TypeHelper.IsScalar(ma.Type))
                    {
                        bool isSet;
                        int  order = MappingSchema.MetadataProvider.GetPrimaryKeyOrder(type, typeExt, ma, out isSet);

                        if (isSet)
                        {
                            list.Add(new MemberOrder(mm, order));
                        }
                    }
                }

                list.Sort(delegate(MemberOrder x, MemberOrder y) { return(x.Order - y.Order); });

                _keyList[key] = mmList = new MemberMapper[list.Count];

                for (int i = 0; i < list.Count; i++)
                {
                    mmList[i] = list[i].MemberMapper;
                }
            }

            return(mmList);
        }
Beispiel #44
0
        ConstantExpression EvaluateExternalMember(MemberExpression node)
        {
            if (node.Expression == null) // static member
            {
                return(Expression.Constant(MemberAccessor.GetGetter(node.Member)(null)));
            }
            switch (node.Expression.NodeType)
            {
            case ExpressionType.Constant:
                return(Expression.Constant(MemberAccessor.GetGetter(node.Member)(((ConstantExpression)node.Expression).Value)));

            case ExpressionType.MemberAccess:
                var value = EvaluateExternalMember((MemberExpression)node.Expression);      // 递归向内获取
                return(Expression.Constant(MemberAccessor.GetGetter(node.Member)(value.Value)));

            default:
                throw new NotSupportedException("不支持的外部值访问:" + node);
            }
        }
Beispiel #45
0
        internal Action <IDatabase, DbCommand, TEntity> BuildCommandExecutor()
        {
            // 不带返回
            if (_outputs.Length == 0)
            {
                return((db, command, entity) => db.ExecuteNonQuery(command));
            }

            var assigns = _outputs.Select(_ => MemberAccessor.GetAssign <TEntity>(_.Member)).ToArray();

            return((db, command, entity) =>
            {
                var result = db.ExecuteEntity <TEntity>(command);
                foreach (var assign in assigns)
                {
                    assign(result, entity);                             // 回写
                }
            });
        }
Beispiel #46
0
 public void AddHook <H, T, C>(
     H hookedItem,
     MemberAccessor <H, T> hookMember,
     C chainItem,
     MemberAccessor <C, T> chainMember,
     T defaultValue)
 {
     hooks.Add(
         new Hook <T, T>()
     {
         HookGetter    = () => hookMember.Getter(hookedItem),
         HookSetter    = (t) => hookMember.Setter(hookedItem, t),
         ChainGetter   = () => chainMember.Getter(chainItem),
         ChainSetter   = (t) => chainMember.Setter(chainItem, t),
         Default       = defaultValue,
         PullConverter = (t) => t,
         PushConverter = (t) => t
     });
 }
        public override void EnsureMapper(TypeAccessor typeAccessor, MappingSchema mappingSchema, EnsureMapperHandler handler)
        {
            foreach (MapFieldAttribute attr in GetMapFieldAttributes(typeAccessor))
            {
                if (attr.OrigName != null)
                {
                    handler(attr.MapName, attr.OrigName);
                }
                else
                {
                    MemberAccessor ma = typeAccessor[attr.MapName];

                    foreach (MemberMapper inner in mappingSchema.GetObjectMapper(ma.Type))
                    {
                        handler(string.Format(attr.Format, inner.Name), ma.Name + "." + inner.MemberName);
                    }
                }
            }
        }
Beispiel #48
0
        private static T GetPropertyValue <T>(Type objType, string propertyName, object obj)
        {
            var                key = objType.FullName + "_" + propertyName;
            object             accessor;
            MemberAccessor <T> typedAccessor;

            if (!accessors.TryGetValue(key, out accessor))
            {
                var property = objType.GetProperty(propertyName);
                typedAccessor = property != null ? MemberAccessor <T> .Get(property) : null;

                accessors.TryAdd(key, typedAccessor);
            }
            else
            {
                typedAccessor = (MemberAccessor <T>)accessor;
            }
            return(typedAccessor == null ? default(T) : typedAccessor.Get(obj));
        }
Beispiel #49
0
        public override Association GetAssociation(TypeExtension typeExtension, MemberAccessor member)
        {
            if (member.Type.IsClass && !member.Type.FullName.StartsWith("System"))
            {
                bool canBeNull = !member.IsDefined <RequiredAttribute>();
                Type otherType = member.Type;
                if (otherType.IsGenericType)
                {
                    otherType = otherType.GetGenericArguments()[0];
                }
                string[] otherKeys = null;
                string[] thisKeys  = null;
                if (member.IsDefined <System.ComponentModel.DataAnnotations.AssociationAttribute>())
                {
                    var asAttr = member.GetAttribute <System.ComponentModel.DataAnnotations.AssociationAttribute>();
                    thisKeys  = asAttr.ThisKeyMembers.ToArray();
                    otherKeys = asAttr.OtherKeyMembers.ToArray();
                }
                else if (member.IsDefined <ForeignKeyAttribute>())
                {
                    var fkAttr = member.GetAttribute <ForeignKeyAttribute>();
                    otherKeys = GetTypeKeyNames(otherType);
                    thisKeys  = fkAttr.Name.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                }
                else
                {
                    otherKeys = GetTypeKeyNames(otherType);
                    thisKeys  = otherKeys;
                }

                if (thisKeys != null && otherKeys != null && thisKeys.Length == otherKeys.Length && thisKeys.Length > 0)
                {
                    return(new Association(
                               member,
                               thisKeys,
                               otherKeys,
                               string.Format("FK_{0}_{1}", member.TypeAccessor.Type, otherType),
                               canBeNull));
                }
            }
            return(base.GetAssociation(typeExtension, member));
        }
        public override bool GetNullable(ObjectMapper mapper, MemberAccessor member, out bool isSet)
        {
            // Check extension <Member1 Nullable='true' />
            //
            object value = GetValue(mapper, member, "Nullable", out isSet);

            if (isSet)
            {
                return(TypeExtension.ToBoolean(value));
            }

            // Check extension <Member1 NullValue='-1' />
            //
            if (GetValue(mapper, member, "NullValue", out isSet) != null)
            {
                return(true);
            }

            return(base.GetNullable(mapper, member, out isSet));
        }
        public override bool GetMapIgnore(TypeExtension typeExtension, MemberAccessor member, out bool isSet)
        {
            var attr = member.GetAttribute <MapIgnoreAttribute>() ?? (MapIgnoreAttribute)TypeHelper.GetFirstAttribute(member.Type, typeof(MapIgnoreAttribute));

            if (attr != null)
            {
                isSet = true;
                return(attr.Ignore);
            }

            if (member.GetAttribute <MapFieldAttribute>() != null ||
                member.GetAttribute <MapImplicitAttribute>() != null ||
                TypeHelper.GetFirstAttribute(member.Type, typeof(MapImplicitAttribute)) != null)
            {
                isSet = true;
                return(false);
            }

            return(base.GetMapIgnore(typeExtension, member, out isSet) || member.GetAttribute <AssociationAttribute>() != null);
        }
Beispiel #52
0
        public override bool GetSqlIgnore(TypeExtension typeExtension, MemberAccessor member, out bool isSet)
        {
            var ignoreAttr = member.GetAttribute <NotMappedAttribute>();

            if (ignoreAttr != null)
            {
                isSet = true;
                return(true);
            }

            var scAttr = member.GetAttribute <ScaffoldColumnAttribute>();

            if (scAttr != null)
            {
                isSet = true;
                return(true);
            }

            return(base.GetSqlIgnore(typeExtension, member, out isSet));
        }
Beispiel #53
0
        public ColumnPropertyInfo(PropertyInfo property, ColumnAttribute column)
        {
            Property             = property;
            ColumnName           = column.ColumnName ?? property.Name;
            IsCustomColumnName   = column.ColumnName != null;
            HasDefaultConstraint = column.HasDefaultConstraint;

            var type = property.PropertyType;

            _accessor = new MemberAccessor(property);
            if (type.IsValueType)
            {
                _defaultValue = ValueTypeDefaultCache.GetDefaultValue(type);
            }
            else
            {
                _defaultValue = null;
            }
            HasSetter = _accessor.HasSetter;
        }
Beispiel #54
0
        protected MemberMapper[] GetNonKeyFieldList(ObjectMapper om)
        {
            TypeExtension       typeExt = TypeExtension.GetTypeExtension(om.TypeAccessor.OriginalType, Extensions);
            List <MemberMapper> list    = new List <MemberMapper>();

            foreach (MemberMapper mm in om)
            {
                MemberAccessor ma = mm.MemberAccessor;

                bool isSet;
                MappingSchema.MetadataProvider.GetPrimaryKeyOrder(om.TypeAccessor.OriginalType, typeExt, ma, out isSet);

                if (!isSet)
                {
                    list.Add(mm);
                }
            }

            return(list.ToArray());
        }
		public override string GetFieldStorage(TypeExtension typeExtension, MemberAccessor member, out bool isSet)
		{
			var a = member.GetAttribute<MapFieldAttribute>();

			if (a != null)
			{
				isSet = true;
				return a.Storage;
			}

			foreach (MapFieldAttribute attr in GetMapFieldAttributes(member.TypeAccessor))
			{
				if (string.Equals(attr.OrigName, member.Name, StringComparison.InvariantCultureIgnoreCase))
				{
					isSet = true;
					return attr.Storage;
				}
			}

			return base.GetFieldStorage(typeExtension, member, out isSet);
		}
        public override Association GetAssociation(TypeExtension typeExtension, MemberAccessor member)
        {
            if (member.Type.IsClass && !member.Type.FullName.StartsWith("System"))
            {
                bool canBeNull = !member.IsDefined<RequiredAttribute>();
                Type otherType = member.Type;
                if(otherType.IsGenericType)
                    otherType = otherType.GetGenericArguments()[0];
                string[] otherKeys = null;
                string[] thisKeys = null;
                if (member.IsDefined<System.ComponentModel.DataAnnotations.AssociationAttribute>())
                {
                    var asAttr = member.GetAttribute<System.ComponentModel.DataAnnotations.AssociationAttribute>();
                    thisKeys = asAttr.ThisKeyMembers.ToArray();
                    otherKeys = asAttr.OtherKeyMembers.ToArray();
                }
                else if (member.IsDefined<ForeignKeyAttribute>())
                {
                    var fkAttr = member.GetAttribute<ForeignKeyAttribute>();
                    otherKeys = GetTypeKeyNames(otherType);
                    thisKeys = fkAttr.Name.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                }
                else
                {
                    otherKeys = GetTypeKeyNames(otherType);
                    thisKeys = otherKeys;
                }

                if (thisKeys != null && otherKeys != null && thisKeys.Length == otherKeys.Length && thisKeys.Length > 0)
                {
                    return new Association(
                        member,
                        thisKeys,
                        otherKeys,
                        string.Format("FK_{0}_{1}", member.TypeAccessor.Type, otherType),
                        canBeNull);
                }
            }
            return base.GetAssociation(typeExtension, member);
        }
		public override bool GetIgnore(ObjectMapper mapper, MemberAccessor member, out bool isSet)
		{
			MapIgnoreAttribute attr = member.GetAttribute<MapIgnoreAttribute>();

			if (attr == null)
				attr = (MapIgnoreAttribute)TypeHelper.GetFirstAttribute(member.Type, typeof(MapIgnoreAttribute));

			if (attr != null)
			{
				isSet = true;
				return attr.Ignore;
			}

			if (member.GetAttribute<MapImplicitAttribute>() != null ||
				TypeHelper.GetFirstAttribute(member.Type, typeof(MapImplicitAttribute)) != null)
			{
				isSet = true;
				return false;
			}

			return base.GetIgnore(mapper, member, out isSet);
		}
			public override string GetFieldName(TypeExtension typeExtension, MemberAccessor member, out bool isSet)
			{
				string name = string.Empty;

				foreach (char c in member.Name)
				{
					if (char.IsUpper(c))
					{
						if (name.Length > 0)
							name += '_';
						name += c;
					}
					else
					{
						name += char.ToUpper(c);
					}
				}

				isSet = true;

				return name;
			}
		public override string GetFieldName(ObjectMapper mapper, MemberAccessor member, out bool isSet)
		{
			MapFieldAttribute a = member.GetAttribute<MapFieldAttribute>();

			if (a != null)
			{
				isSet = true;
				return a.MapName;
			}

			string name = member.Name.ToLower();

			foreach (MapFieldAttribute attr in GetMapFieldAttributes(mapper))
			{
				if (attr.OrigName.ToLower() == name)
				{
					isSet = true;
					return attr.MapName;
				}
			}

			return base.GetFieldName(mapper, member, out isSet);
		}
		public override MapValue[] GetMapValues(ObjectMapper mapper, MemberAccessor member, out bool isSet)
		{
			AttributeExtensionCollection extList = mapper.Extension[member.Name]["MapValue"];

			if (extList == AttributeExtensionCollection.Null)
				return GetMapValues(mapper.Extension, member.Type, out isSet);

			List<MapValue> list = new List<MapValue>(extList.Count);

			foreach (AttributeExtension ext in extList)
			{
				object origValue = ext["OrigValue"];

				if (origValue != null)
				{
					origValue = TypeExtension.ChangeType(origValue, member.Type);
					list.Add(new MapValue(origValue, ext.Value));
				}
			}

			isSet = true;

			return list.ToArray();
		}