示例#1
0
        public void GetFromEntityMetadata()
        {
            var entItem = systemUnderTest.GetEntityMetadata("contact");
            var actual  = entItem.Attributes.Select(p => p.AttributeType).ToList();

            actual.Should().NotBeNull();
        }
示例#2
0
        private static TData GetEntityData <TData>(object entity, Type entityType, Action <TData, string, object> action)
            where TData : class, new()
        {
            if (entity == null)
            {
                return(null);
            }

            TData data = Activator.CreateInstance <TData>();//类型TData要求new()约束

            EntityMetadata entityMeta = EntityMetadataCache.GetEntityMetadata(entityType);

            foreach (PropertyMetadata field in entityMeta.Propertys)
            {
                MethodInfo mi = field.PropertyInfo.GetGetMethod();
                if (mi == null)
                {
                    continue;                                           // 无Getter的时候忽略此Property
                }
                string key   = field.FieldName;
                object value = mi.Invoke(entity, null);

                if (value == null)
                {
                    action(data, key, value);
                    continue;
                }

                Type propType = field.PropertyInfo.PropertyType;
                if (propType.IsGenericType)
                {
                    if (propType.IsValueType)
                    {
                        action(data, key, value);
                    }
                    else if (IsIList(propType))//TODO:未处理循环引用情况,此时会导致死循环
                    {
                        Type genericType = propType.GetGenericArguments()[0];
                        if (genericType.IsClass)
                        {
                            IList         entites = value as IList;
                            IList <TData> list    = GetEntityDatas <TData>(entites, genericType, action);
                            action(data, key, list);
                        }
                    }
                    continue;
                }

                if (IsPrimitiveType(propType) || propType.IsEnum)
                {
                    action(data, key, value);
                    continue;
                }

                action(data, key, GetEntityData(value, propType, action));
            }
            return(data);
        }
示例#3
0
        public void GetEntityMetadata()
        {
            var orgService   = ConnectionHelper.GetOrganizationalServiceTarget();
            var cache        = new EntityMetadataCache(orgService);
            var contactCache = cache.GetEntityMetadata("contact");

            // this time it shoudl get item from cache
            var cache2        = new EntityMetadataCache(orgService);
            var contactCache2 = cache2.GetEntityMetadata("contact");

            Assert.AreSame(contactCache, contactCache2);
            Assert.AreEqual(contactCache.Keys.Length, contactCache2.Keys.Length);
        }
        public void GetEntityMetadata()
        {
            string entityName = "contactemd";

            RetrieveEntityResponse response = InjectMetaDataToResponse(new RetrieveEntityResponse());

            MockOrganizationService.Setup(a => a.Execute(It.IsAny <OrganizationRequest>())).Returns(response);

            FluentActions.Invoking(() => systemUnderTest.GetEntityMetadata(entityName))
            .Should()
            .NotThrow();

            MockOrganizationService.VerifyAll();
        }
示例#5
0
        /// <summary>
        /// 实体列表转DataTable
        /// </summary>
        /// <typeparam name="TEntity">实体类型</typeparam>
        /// <param name="entitys">实体集合</param>
        /// <returns>DataTable</returns>
        public static DataTable GetDataTableOfEntitys <TEntity>(IList <TEntity> entitys) where TEntity : class
        {
            if (entitys == null || entitys.Count < 1)
            {
                return(new DataTable());
            }

            // 取实体缓存
            Type           type      = typeof(TEntity);
            EntityMetadata tableMeta = EntityMetadataCache.GetEntityMetadata(type);

            // 无需缓存表结构
            DataTable dt = new DataTable(tableMeta.TableName);

            tableMeta.Propertys.ForEach((pi) =>
            {
                Type columnType = pi.PropertyInfo.PropertyType;
                if (columnType.IsGenericType && columnType.GetGenericTypeDefinition() == typeof(Nullable <>))
                {
                    columnType = columnType.GetGenericArguments()[0];
                }
                dt.Columns.Add(new DataColumn(pi.FieldName, columnType));
            });

            // 填充数据
            foreach (TEntity entity in entitys)
            {
                DataRow row = dt.NewRow();
                tableMeta.Propertys.ForEach((p) =>
                {
                    object value = p.PropertyInfo.GetValue(entity, null);
                    if (value == null)
                    {
                        value = DBNull.Value;
                    }
                    row[p.FieldName] = value;
                });
                dt.Rows.Add(row);
            }
            return(dt);
        }
示例#6
0
        private static void AssignToPropertyOrField(object propertyValue, object o, string memberName, JsonSerializer serializer)
        {
            IDictionary dictionary = o as IDictionary;

            if (dictionary != null)
            {
                propertyValue          = ConvertObjectToType(propertyValue, null, serializer);
                dictionary[memberName] = propertyValue;
                return;
            }

            Type             serverType = o.GetType();
            EntityMetadata   typeData   = EntityMetadataCache.GetEntityMetadata(serverType); // 使用缓存性能更好 add by zq 2015-12-30
            PropertyMetadata pMeta      = typeData.Propertys.Find(delegate(PropertyMetadata meta) { return(meta.FieldName.ToLower() == memberName.ToLower()); });

            if (pMeta != null)
            {
                PropertyInfo propInfo = pMeta.PropertyInfo;
                MethodInfo   setter   = propInfo.GetSetMethod();
                if (setter != null)
                {
                    propertyValue = ConvertObjectToType(propertyValue, propInfo.PropertyType, serializer, memberName);
                    if (typeof(IParamPropertySetter).IsAssignableFrom(serverType))
                    {
                        ((IParamPropertySetter)o).PropertySetterBeforeInvoke(propInfo.Name, propertyValue);
                    }
                    setter.Invoke(o, new Object[] { propertyValue });
                    return;
                }
            }

            pMeta = typeData.Fields.Find(delegate(PropertyMetadata meta) { return(meta.FieldName.ToLower() == memberName.ToLower()); });
            if (pMeta != null)
            {
                FieldInfo fieldInfo = pMeta.FieldInfo;
                propertyValue = ConvertObjectToType(propertyValue, fieldInfo.FieldType, serializer, memberName);
                fieldInfo.SetValue(o, propertyValue);
                return;
            }
        }
示例#7
0
        private static object GetEntity <TData>(TData data, Type entityType, Func <TData, string, object> getValueAction)
            where TData : class
        {
            if (data == null)
            {
                return(null);
            }

            object         entity     = Activator.CreateInstance(entityType);                                   // 类型entityType要求new()约束
            EntityMetadata entityMeta = EntityMetadataCache.GetEntityMetadata(entityType);

            // 处理属性
            foreach (PropertyMetadata field in entityMeta.Propertys)
            {
                MethodInfo mInfo = field.PropertyInfo.GetSetMethod();;
                if (mInfo == null)
                {
                    continue;                                                                                   // 无Setter的时候忽略此Property
                }
                AssignToPropertyOrField <TData, MethodInfo>(data, entityType, getValueAction, setMethodAction, mInfo, field.FieldName, entity, field.PropertyInfo.PropertyType);
            }

            return(entity);
        }
示例#8
0
        private void SerializeCustomObject(object o, StringBuilder sb, int depth, Hashtable objectsInUse,
                                           SerializationFormat serializationFormat)
        {
            bool first = true;
            Type type  = o.GetType();

            sb.Append('{');

            if (TypeResolver != null)
            {
                string typeString = TypeResolver.ResolveTypeId(type);
                if (typeString != null)
                {
                    SerializeString(ServerTypeFieldName, sb);
                    sb.Append(':');
                    SerializeValue(typeString, sb, depth, objectsInUse, serializationFormat);
                    first = false;
                }
            }

            EntityMetadata typeData = EntityMetadataCache.GetEntityMetadata(type);// 使用统一缓存方式 add by zq 2015-12-30

            foreach (PropertyMetadata pMeta in typeData.Fields)
            {
                FieldInfo fieldInfo = pMeta.FieldInfo;
                if (fieldInfo.IsDefined(typeof(ScriptIgnoreAttribute), true))
                {
                    continue;
                }

                if (!first)
                {
                    sb.Append(',');
                }
                SerializeString(pMeta.FieldName, sb);
                sb.Append(':');
                SerializeValue(fieldInfo.GetValue(o), sb, depth, objectsInUse, serializationFormat);
                first = false;
            }

            foreach (PropertyMetadata pMeta in typeData.Propertys)
            {
                PropertyInfo propInfo = pMeta.PropertyInfo;
                if (propInfo.IsDefined(typeof(ScriptIgnoreAttribute), true))
                {
                    continue;
                }

                MethodInfo getMethodInfo = propInfo.GetGetMethod();
                if (getMethodInfo == null)
                {
                    continue;
                }

                if (getMethodInfo.GetParameters().Length > 0)
                {
                    continue;
                }

                if (!first)
                {
                    sb.Append(',');
                }
                SerializeString(pMeta.FieldName, sb);
                sb.Append(':');
                SerializeValue(getMethodInfo.Invoke(o, null), sb, depth, objectsInUse, serializationFormat);
                first = false;
            }

            sb.Append('}');
        }