Example #1
0
        private bool IsOneToMany(MemberInfo member, bool declared)
        {
            if (declared) return true;
            if (member.GetPropertyOrFieldType().IsGenericCollection() == false) return false;

            var collectionElementType = member.GetPropertyOrFieldType().GetGenericArguments().First();

            var isOneToMany = ManyToMany.IsManyToMany(member) == false && Mapper.ModelInspector.IsEntity(collectionElementType);

            return  isOneToMany;
        }
		public void Component(MemberInfo property, Action<IComponentElementMapper> mapping)
		{
			System.Type nestedComponentType = property.GetPropertyOrFieldType();
			var hbm = new HbmNestedCompositeElement { name = property.Name, @class = nestedComponentType.GetShortClassName(_mapDoc) };
			mapping(new ComponentNestedElementMapper(nestedComponentType, _mapDoc, hbm, property));
			AddProperty(hbm);
		}
		public virtual void OneToOne(MemberInfo property, Action<IOneToOneMapper> mapping)
		{
			var hbm = new HbmOneToOne {name = property.Name};
			var type = typeof(OneToOneMapper<>).MakeGenericType(property.GetPropertyOrFieldType());
			var mapper = (IOneToOneMapper)Activator.CreateInstance(type, property, hbm);
			mapping(mapper);
			AddProperty(hbm);
		}
Example #4
0
 public void should_get_value(CachedMember cachedMember, MemberInfo memberInfo)
 {
     var @object = new Members();
     @object.SetPropertyOrFieldValue(memberInfo.Name,
         memberInfo.GetPropertyOrFieldType() == typeof(string) ?
             "hai" : (object)(Optional<string>)"hai");
     cachedMember.GetValue(@object).As<string>().ShouldEqual("hai");
 }
		public virtual void IdBag(MemberInfo property, Action<IIdBagPropertiesMapper> collectionMapping, Action<ICollectionElementRelation> mapping)
		{
			var hbm = new HbmIdbag { name = property.Name };
			System.Type propertyType = property.GetPropertyOrFieldType();
			System.Type collectionElementType = propertyType.DetermineCollectionElementType();
			collectionMapping(new IdBagMapper(container, collectionElementType, hbm));
			mapping(new CollectionElementRelation(collectionElementType, MapDoc, rel => hbm.Item = rel));
			AddProperty(hbm);
		}
 public virtual void Set(MemberInfo property, Action<ISetPropertiesMapper> collectionMapping, Action<ICollectionElementRelation> mapping)
 {
     var hbm = new HbmSet { name = property.Name };
     var propertyType = property.GetPropertyOrFieldType();
     var collectionElementType = propertyType.DetermineCollectionElementType();
     collectionMapping(new SetMapper(container, collectionElementType, hbm));
     mapping(new CollectionElementRelation(collectionElementType, MapDoc, rel => hbm.Item = rel));
     AddProperty(hbm);
 }
		public virtual void Component(MemberInfo property, Action<IComponentMapper> mapping)
		{
			if (!IsMemberSupportedByMappedContainer(property))
			{
				throw new ArgumentOutOfRangeException("property", "Can't add a property of another graph");
			}
			var hbm = new HbmComponent { name = property.Name };
			mapping(new ComponentMapper(hbm, property.GetPropertyOrFieldType(), property, MapDoc));
			AddProperty(hbm);
		}
		public void Property(MemberInfo property, Action<IPropertyMapper> mapping)
		{
			var hbmProperty = new HbmProperty
			                  {
			                  	name = property.Name, 
													type1 = property.GetPropertyOrFieldType().GetNhTypeName()
			                  };

			mapping(new PropertyMapper(property, hbmProperty, new NoMemberPropertyMapper()));
			AddProperty(hbmProperty);
		}
 public override bool Match(MemberInfo subject)
 {
     bool isBidirectionalUnaryAssociation = base.Match(subject);
     if (isBidirectionalUnaryAssociation)
     {
         Type from = subject.DeclaringType;
         Type to = subject.GetPropertyOrFieldType();
         return DomainInspector.IsOneToOne(from, to) && DomainInspector.IsMasterOneToOne(from, to)
                && DomainInspector.IsOneToOne(to, from);
     }
     return false;
 }
		public virtual void Map(MemberInfo property, Action<IMapPropertiesMapper> collectionMapping,
								Action<IMapKeyRelation> keyMapping, Action<ICollectionElementRelation> mapping)
		{
			var hbm = new HbmMap {name = property.Name};
			System.Type propertyType = property.GetPropertyOrFieldType();
			System.Type dictionaryKeyType = propertyType.DetermineDictionaryKeyType();
			System.Type dictionaryValueType = propertyType.DetermineDictionaryValueType();

			collectionMapping(new MapMapper(container, dictionaryKeyType, dictionaryValueType, hbm, mapDoc));
			keyMapping(new MapKeyRelation(dictionaryKeyType, hbm, mapDoc));
			mapping(new CollectionElementRelation(dictionaryValueType, MapDoc, rel => hbm.Item1 = rel));
			AddProperty(hbm);
		}
 protected override bool MemberMatch(MemberInfo subject)
 {
     Type memberType = subject.GetPropertyOrFieldType();
     if (typeof(IDictionary).IsAssignableFrom(memberType))
     {
         return true;
     }
     if (memberType.IsGenericType)
     {
         return memberType.GetGenericIntercafesTypeDefinitions().Contains(typeof(IDictionary<,>));
     }
     return false;
 }
Example #12
0
        private static bool IsSetFieldType(MemberInfo mi, bool declared)
        {
            var propertyTypeIsSet = mi.GetPropertyOrFieldType()
            .GetGenericIntercafesTypeDefinitions()
            .Contains(typeof (ISet<>));

              if (propertyTypeIsSet) return true;

              var backFieldInfo = PropertyToField.GetBackFieldInfo((PropertyInfo) mi);

              return backFieldInfo != null
             && backFieldInfo
                  .FieldType.GetGenericIntercafesTypeDefinitions().Contains(typeof (ISet<>));
        }
Example #13
0
		private bool MatchOneToMany(MemberInfo memberInfo)
		{
			var modelInspector = (IModelInspector) this;
			System.Type from = memberInfo.ReflectedType;
			System.Type to = memberInfo.GetPropertyOrFieldType().DetermineCollectionElementOrDictionaryValueType();
			if(to == null)
			{
				// no generic collection or simple property
				return false;
			}
			bool areEntities = modelInspector.IsEntity(from) && modelInspector.IsEntity(to);
			bool isFromComponentToEntity = modelInspector.IsComponent(from) && modelInspector.IsEntity(to);
			return !declaredModel.IsManyToMany(memberInfo) && (areEntities || isFromComponentToEntity);
		}
 public IdMapper(MemberInfo member, HbmId hbmId)
 {
     this.hbmId = hbmId;
     if (member != null)
     {
         var idType = member.GetPropertyOrFieldType();
         hbmId.name = member.Name;
         hbmId.type1 = idType.GetNhTypeName();
         accessorMapper = new AccessorPropertyMapper(member.DeclaringType, member.Name, x => hbmId.access = x);
     }
     else
     {
         accessorMapper = new NoMemberPropertyMapper();
     }
 }
Example #15
0
        protected override System.Reflection.PropertyInfo GetInverseProperty(System.Reflection.MemberInfo member)
        {
            var type = member.GetPropertyOrFieldType();
            var to   = type.DetermineCollectionElementOrDictionaryValueType();

            if (to == null)
            {
                // no generic collection or simple property
                return(null);
            }

            var argument = type.GetGenericArguments()[0];

            return(argument.GetProperties().FirstOrDefault(x => x.PropertyType == member.DeclaringType));
        }
 protected override bool MemberMatch(MemberInfo subject)
 {
     Type memberType = subject.GetPropertyOrFieldType();
     if (IsNotSupportedAsList(memberType))
     {
         return false;
     }
     if (typeof (IList).IsAssignableFrom(memberType))
     {
         return true;
     }
     if (memberType.IsGenericType)
     {
         var isList = memberType.GetGenericIntercafesTypeDefinitions().Contains(typeof(IList<>));
         // a bidirectional one-to-many should use Bag or Set
         return isList && !(new BidirectionalOneToManyPattern(domainInspector).Match(new Relation(subject.DeclaringType, memberType.DetermineCollectionElementType())));
     }
     return false;
 }
Example #17
0
        public static Action<object, IDataReader, int> CreateSetValueFromReaderDelegate(MemberInfo member)
        {
            Action<object, IDataReader, int> del = null;

            DynamicMethod dm = new DynamicMethod("SetValueFromReader_" + Guid.NewGuid().ToString("N"), null, new Type[] { typeof(object), typeof(IDataReader), typeof(int) }, true);
            ILGenerator il = dm.GetILGenerator();

            il.Emit(OpCodes.Ldarg_S, 0);//将第一个参数 object 对象加载到栈顶
            il.Emit(OpCodes.Castclass, member.DeclaringType);//将 object 对象转换为强类型对象 此时栈顶为强类型的对象

            var readerMethod = DataReaderConstant.GetReaderMethod(member.GetPropertyOrFieldType());

            //ordinal
            il.Emit(OpCodes.Ldarg_S, 1);    //加载参数DataReader
            il.Emit(OpCodes.Ldarg_S, 2);    //加载 read ordinal
            il.EmitCall(OpCodes.Call, readerMethod, null);     //调用对应的 readerMethod 得到 value  reader.Getxx(ordinal);  此时栈顶为 value

            EmitHelper.SetValueIL(il, member); // object.XX = value; 此时栈顶为空

            il.Emit(OpCodes.Ret);   // 即可 return

            del = (Action<object, IDataReader, int>)dm.CreateDelegate(typeof(Action<object, IDataReader, int>));
            return del;
        }
 protected override bool MemberMatch(MemberInfo subject)
 {
     var memberType = subject.GetPropertyOrFieldType();
     return memberType.IsGenericType && memberType.GetGenericTypeDefinition() == typeof(IList<>);
 }
Example #19
0
        public static BidirectionAssociation AnalizeManyToMany(MemberInfo memberInfo)
        {
            if (memberInfo.IsMap())
            {
                return new BidirectionAssociation((PropertyInfo)memberInfo, null);
            }

            var itemType = memberInfo.GetPropertyOrFieldType().GetGenericArguments().Single();

                var otherSideInterface = typeof (IEnumerable<>).MakeGenericType(memberInfo.ReflectedType);

                //has collection on the other end of association
                var otherSide =
                    itemType.GetProperties()
                        //Is a collection on the other side of association
                        .Where(property => property.PropertyType.GetInterfaces().Any(otherSideInterface.IsAssignableFrom))
                        //But no the same collection!
                        .Where(property => property.Equals(memberInfo) == false)
                        .SingleOrDefault();

                return new BidirectionAssociation((PropertyInfo)memberInfo, otherSide);
        }
 protected override bool MemberMatch(MemberInfo subject)
 {
     Type memberType = subject.GetPropertyOrFieldType();
     return memberType.IsArray && memberType.GetElementType() != typeof(byte);
 }
 public virtual void Component(MemberInfo property, Action<IComponentMapper> mapping)
 {
     var hbm = new HbmComponent { name = property.Name };
     mapping(new ComponentMapper(hbm, property.GetPropertyOrFieldType(), property, MapDoc));
     AddProperty(hbm);
 }
		protected bool MatchBagMember(MemberInfo subject)
		{
			System.Type memberType = subject.GetPropertyOrFieldType();
			return typeof(System.Collections.IEnumerable).IsAssignableFrom(memberType) && !(memberType == typeof(string) || memberType == typeof(byte[]));
		}
		protected bool MatchArrayMember(MemberInfo subject)
		{
			System.Type memberType = subject.GetPropertyOrFieldType();
			return memberType.IsArray && memberType.GetElementType() != typeof(byte);
		}
		protected bool MatchDictionaryMember(MemberInfo subject)
		{
			System.Type memberType = subject.GetPropertyOrFieldType();
			if (typeof(System.Collections.IDictionary).IsAssignableFrom(memberType))
			{
				return true;
			}
			if (memberType.IsGenericType)
			{
				return memberType.GetGenericInterfaceTypeDefinitions().Contains(typeof(IDictionary<,>));
			}
			return false;
		}
		private bool MatchManyToOne(MemberInfo memberInfo)
		{
			var modelInspector = (IModelInspector)this;
			System.Type from = memberInfo.ReflectedType;
			System.Type to = memberInfo.GetPropertyOrFieldType();

			bool areEntities = modelInspector.IsEntity(from) && modelInspector.IsEntity(to);
			bool isFromComponentToEntity = modelInspector.IsComponent(from) && modelInspector.IsEntity(to);
			return isFromComponentToEntity || (areEntities && !modelInspector.IsOneToOne(memberInfo));
		}
Example #26
0
 private bool IsVersion(MemberInfo member, bool declared)
 {
     return declared || member.Name.Equals("Version", StringComparison.OrdinalIgnoreCase) && _versionTypes.Contains(member.GetPropertyOrFieldType());
 }
		public void List(MemberInfo property, Action<IListPropertiesMapper> collectionMapping, Action<ICollectionElementRelation> mapping)
		{
			var hbm = new HbmList { name = property.Name };
			System.Type propertyType = property.GetPropertyOrFieldType();
			System.Type collectionElementType = propertyType.DetermineCollectionElementType();
			collectionMapping(new ListMapper(Container, collectionElementType, new NoMemberPropertyMapper(), hbm));
			mapping(new CollectionElementRelation(collectionElementType, MapDoc, rel => hbm.Item1 = rel));
			AddProperty(hbm);
		}
Example #28
0
 public void should_set_value(CachedMember cachedMember, MemberInfo memberInfo)
 {
     var @object = new Members();
     cachedMember.SetValue(@object, "hai");
     var value = @object.GetPropertyOrFieldValue(memberInfo.Name);
     (memberInfo.GetPropertyOrFieldType() == typeof(string) ? "hai" :
         (string)(Optional<string>)value).ShouldEqual("hai");
 }
		protected bool MatchSetMember(MemberInfo subject)
		{
			var memberType = subject.GetPropertyOrFieldType();
			if (typeof(ISet).IsAssignableFrom(memberType))
			{
				return true;
			}
			if (memberType.IsGenericType)
			{
				return memberType.GetGenericInterfaceTypeDefinitions().Contains(typeof(Iesi.Collections.Generic.ISet<>));
			}
			return false;
		}
		protected bool MatchSetMember(MemberInfo subject)
		{
			var memberType = subject.GetPropertyOrFieldType();

			if (memberType.IsGenericType)
			{
				return memberType.GetGenericInterfaceTypeDefinitions().Contains(typeof(ISet<>));
			}
			return false;
		}
Example #31
0
 public static bool IsManyToMany(MemberInfo memberInfo)
 {
     return
         memberInfo.GetPropertyOrFieldType().IsGenericCollection() &&
         BidirectionAssociation.AnalizeManyToMany(memberInfo).IsManyToMany;
 }