void Init()
        {
            Type t       = this.Type;
            var  members = t.GetMembers(BindingFlags.Public | BindingFlags.Instance);

            Dictionary <MemberInfo, IMRM> mappingMemberMappers = new Dictionary <MemberInfo, IMRM>();
            Dictionary <MemberInfo, Action <object, object> > complexMemberSetters = new Dictionary <MemberInfo, Action <object, object> >();

            foreach (var member in members)
            {
                if (!member.HasPublicSetter())
                {
                    continue;
                }

                //只支持公共属性和字段
                Type memberType = member.GetMemberType();

                MappingTypeInfo mappingTypeInfo;
                if (MappingTypeSystem.IsMappingType(memberType, out mappingTypeInfo))
                {
                    IMRM mrm = MRMHelper.CreateMRM(member, mappingTypeInfo);
                    mappingMemberMappers.Add(member, mrm);
                }
                else
                {
                    Action <object, object> valueSetter = DelegateGenerator.CreateValueSetter(member);
                    complexMemberSetters.Add(member, valueSetter);
                }
            }

            this._mappingMemberMappers = PublicHelper.Clone(mappingMemberMappers);
            this._complexMemberSetters = PublicHelper.Clone(complexMemberSetters);
        }
        public IMRM GetMemberMapper(MemberInfo memberInfo)
        {
            IMRM mapper = null;

            this._mappingMemberMRMContainer.TryGetValue(memberInfo, out mapper);
            return(mapper);
        }
Beispiel #3
0
        void Init()
        {
            Type t       = this.Type;
            var  members = t.GetMembers(BindingFlags.Public | BindingFlags.Instance);

            Dictionary <MemberInfo, IMRM> mappingMemberMappers = new Dictionary <MemberInfo, IMRM>();
            Dictionary <MemberInfo, Action <object, object> > complexMemberSetters = new Dictionary <MemberInfo, Action <object, object> >();

            foreach (var member in members)
            {
                Type         memberType = null;
                PropertyInfo prop       = null;
                FieldInfo    field      = null;

                if ((prop = member as PropertyInfo) != null)
                {
                    if (prop.GetSetMethod() == null)
                    {
                        continue;//对于没有公共的 setter 直接跳过
                    }
                    memberType = prop.PropertyType;
                }
                else if ((field = member as FieldInfo) != null)
                {
                    memberType = field.FieldType;
                }
                else
                {
                    continue;//只支持公共属性和字段
                }
                MappingTypeInfo mappingTypeInfo;
                if (MappingTypeSystem.IsMappingType(memberType, out mappingTypeInfo))
                {
                    IMRM mrm = MRMHelper.CreateMRM(member, mappingTypeInfo);
                    mappingMemberMappers.Add(member, mrm);
                }
                else
                {
                    if (prop != null)
                    {
                        Action <object, object> valueSetter = DelegateGenerator.CreateValueSetter(prop);
                        complexMemberSetters.Add(member, valueSetter);
                    }
                    else if (field != null)
                    {
                        Action <object, object> valueSetter = DelegateGenerator.CreateValueSetter(field);
                        complexMemberSetters.Add(member, valueSetter);
                    }
                    else
                    {
                        continue;
                    }

                    continue;
                }
            }

            this._mappingMemberMappers = Utils.Clone(mappingMemberMappers);
            this._complexMemberSetters = Utils.Clone(complexMemberSetters);
        }
Beispiel #4
0
        public static IMRM CreateMRM(MemberInfo member)
        {
            Type type = CodeGenerator.CreateMRMType(member);
            IMRM obj  = (IMRM)type.GetConstructor(Type.EmptyTypes).Invoke(null);

            return(obj);
        }
Beispiel #5
0
            static List <IValueSetter> PrepareValueSetters(Type type, IDataReader reader, EntityMemberMapper mapper)
            {
                List <IValueSetter> memberSetters = new List <IValueSetter>(reader.FieldCount);

                MemberInfo[]      properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.SetProperty);
                MemberInfo[]      fields     = type.GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.SetField);
                List <MemberInfo> members    = new List <MemberInfo>(properties.Length + fields.Length);

                members.AddRange(properties);
                members.AddRange(fields);

                TypeDescriptor typeDescriptor = EntityTypeContainer.TryGetDescriptor(type);

                for (int i = 0; i < reader.FieldCount; i++)
                {
                    string     name      = reader.GetName(i);
                    MemberInfo mapMember = TryGetMapMember(members, name, typeDescriptor);

                    if (mapMember == null)
                    {
                        continue;
                    }

                    IMRM mMapper = mapper.TryGetMappingMemberMapper(mapMember);
                    if (mMapper == null)
                    {
                        continue;
                    }

                    MappingMemberBinder memberBinder = new MappingMemberBinder(mMapper, i);
                    memberSetters.Add(memberBinder);
                }

                return(memberSetters);
            }
            static List <IValueSetter> PrepareValueSetters(Type type, IDataReader reader, EntityMemberMapper mapper)
            {
                List <IValueSetter> memberSetters = new List <IValueSetter>(reader.FieldCount);

                MemberInfo[]      properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.SetProperty);
                MemberInfo[]      fields     = type.GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.SetField);
                List <MemberInfo> members    = new List <MemberInfo>(properties.Length + fields.Length);

                members.AddRange(properties);
                members.AddRange(fields);

                for (int i = 0; i < reader.FieldCount; i++)
                {
                    string name   = reader.GetName(i);
                    var    member = members.Where(a => a.Name == name).FirstOrDefault();
                    if (member == null)
                    {
                        member = members.Where(a => string.Equals(a.Name, name, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
                        if (member == null)
                        {
                            continue;
                        }
                    }
                    IMRM mMapper = mapper.TryGetMappingMemberMapper(member);
                    if (mMapper == null)
                    {
                        continue;
                    }

                    MappingMemberBinder memberBinder = new MappingMemberBinder(mMapper, i);
                    memberSetters.Add(memberBinder);
                }

                return(memberSetters);
            }
Beispiel #7
0
        public static MRMTuple CreateMRMTuple(MemberInfo member, MappingType mappingType)
        {
            MRMTuple mrmTuple = new MRMTuple();

            mrmTuple.StrongMRM = new Lazy <IMRM>(() =>
            {
                Type type      = ClassGenerator.CreateMRMType(member);
                IMRM strongMrm = (IMRM)type.GetDefaultConstructor().Invoke(null);
                return(strongMrm);
            }, LazyThreadSafetyMode.ExecutionAndPublication);

            if (member.GetMemberType().GetUnderlyingType().IsEnum /* 枚举比较特殊 */)
            {
                mrmTuple.SafeMRM = mrmTuple.StrongMRM;
            }
            else
            {
                mrmTuple.SafeMRM = new Lazy <IMRM>(() =>
                {
                    return(new MRM2(member, mappingType));
                }, LazyThreadSafetyMode.ExecutionAndPublication);
            }

            return(mrmTuple);
        }
        public IMRM TryGetMemberMapper(MemberInfo memberInfo)
        {
            memberInfo = memberInfo.AsReflectedMemberOf(this.Type);
            IMRM mapper = null;

            this._mappingMemberMRMContainer.TryGetValue(memberInfo, out mapper);
            return(mapper);
        }
        public void Prepare(IDataReader reader)
        {
            Type fieldType = reader.GetFieldType(this._ordinal);

            if (fieldType == this._member.GetMemberType().GetUnderlyingType())
            {
                this._mMapper = this._mrmTuple.StrongMRM.Value;
                return;
            }

            this._mMapper = this._mrmTuple.SafeMRM.Value;
        }
Beispiel #10
0
 public static IMRM CreateMRM(MemberInfo member, MappingTypeInfo mappingTypeInfo)
 {
     if (mappingTypeInfo.DbValueConverter == null || member.GetMemberType().GetUnderlyingType().IsEnum /* 枚举比较特殊 */)
     {
         Type type = ClassGenerator.CreateMRMType(member);
         IMRM obj  = (IMRM)type.GetConstructor(Type.EmptyTypes).Invoke(null);
         return(obj);
     }
     else
     {
         return(new MRM2(member, mappingTypeInfo.DbValueConverter));
     }
 }
Beispiel #11
0
        public IObjectActivator CreateObjectActivator(IDbContext dbContext)
        {
            EntityMemberMapper  mapper        = this.ConstructorDescriptor.GetEntityMemberMapper();
            List <IValueSetter> memberSetters = new List <IValueSetter>(this.Members.Count + this.EntityMembers.Count);

            foreach (var kv in this.Members)
            {
                IMRM mMapper = mapper.TryGetMappingMemberMapper(kv.Key);
                MappingMemberBinder binder = new MappingMemberBinder(mMapper, kv.Value);
                memberSetters.Add(binder);
            }

            foreach (var kv in this.EntityMembers)
            {
                Action <object, object> del            = mapper.TryGetNavigationMemberSetter(kv.Key);
                IObjectActivator        memberActivtor = kv.Value.CreateObjectActivator();
                NavigationMemberBinder  binder         = new NavigationMemberBinder(del, memberActivtor);
                memberSetters.Add(binder);
            }

            Func <IDataReader, ReaderOrdinalEnumerator, ObjectActivatorEnumerator, object> instanceCreator = this.ConstructorDescriptor.GetInstanceCreator();

            List <int> readerOrdinals = this.ConstructorParameters.Select(a => a.Value).ToList();
            List <IObjectActivator> objectActivators = this.ConstructorEntityParameters.Select(a => a.Value.CreateObjectActivator()).ToList();

            ObjectActivator ret;

            if (dbContext != null)
            {
                ret = new ObjectActivatorWithTracking(instanceCreator, readerOrdinals, objectActivators, memberSetters, this.CheckNullOrdinal, dbContext);
            }
            else
            {
                ret = new ObjectActivator(instanceCreator, readerOrdinals, objectActivators, memberSetters, this.CheckNullOrdinal);
            }

            return(ret);
        }
Beispiel #12
0
 public MappingMemberBinder(IMRM mMapper, int ordinal)
 {
     this._mMapper = mMapper;
     this._ordinal = ordinal;
 }
Beispiel #13
0
 public MappingMemberBinder(IMRM mMapper, int ordinal)
 {
     this._mMapper = mMapper;
     this._ordinal = ordinal;
 }