Esempio n. 1
0
        /// <summary>
        /// 简单映射方法
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="inputMapper"></param>
        internal static void EntityMappingMethod <X>(IClassMapper <X> inputValue)
            where X : BaseEntity
        {
            //开启二级缓存
            inputValue.Cache(k => { k.Usage(CacheUsage.ReadWrite); });

            //优先关闭延迟加载
            inputValue.Lazy(false);

            //绑定Id
            //设定为自增主键
            inputValue.Id(k => k.Id, map => map.Generator(Generators.Identity));
            inputValue.EntityName(typeof(X).Name);

            Type useType = typeof(X);

            //获取属性设置器类型
            Type useMapperMehtodType = typeof(EntityAutoMappingUtility);

            //设置用方法
            MethodInfo useBasePropertyInfo  = null;
            MethodInfo useBaseBagInfo       = null;
            MethodInfo useBaseManyToOneInfo = null;
            MethodInfo useBaseBagManyInfo   = null;

            useBasePropertyInfo  = useMapperMehtodType.GetMethod(m_strUsePropertyMethodName, BindingFlags.Static | BindingFlags.NonPublic);
            useBaseBagInfo       = useMapperMehtodType.GetMethod(m_strUseBagMethodName, BindingFlags.Static | BindingFlags.NonPublic);
            useBaseManyToOneInfo = useMapperMehtodType.GetMethod(m_strUseManyToOneMethodName, BindingFlags.Static | BindingFlags.NonPublic);
            useBaseBagManyInfo   = useMapperMehtodType.GetMethod(m_strUseBagManyMethodName, BindingFlags.Static | BindingFlags.NonPublic);

            var allProperties = useType.GetProperties();

            //表达式变量
            ParameterExpression target               = null;
            Expression          getPropertyValue     = null;
            MethodInfo          realUsePopertyMethod = null;

            //遍历属性
            foreach (var onePropertie in allProperties)
            {
                //跳过Id
                if (onePropertie.Name.Equals(m_strUseIdName))
                {
                    continue;
                }

                //选取合适的特性
                if (onePropertie.CanRead && onePropertie.CanWrite && onePropertie.GetMethod.IsVirtual && onePropertie.SetMethod.IsVirtual)
                {
                    //获取附加属性
                    PropertyAttribute useAttribute = onePropertie.GetCustomAttribute(typeof(PropertyAttribute)) as PropertyAttribute;

                    //制作表达式
                    target           = Expression.Parameter(useType);
                    getPropertyValue = Expression.Property(target, onePropertie);


                    //one - Many 行为
                    if (onePropertie.PropertyType.IsGenericCollection() && onePropertie.PropertyType.IsInterface &&
                        onePropertie.PropertyType.DetermineCollectionElementType().IsSubclassOf(m_baseEntityType))
                    {
                        //bag
                        if (onePropertie.PropertyType.GetGenericTypeDefinition() == m_baseIEnumerable)
                        {
                            //区分ManyToMany
                            if (null != useAttribute && useAttribute.IfIsManyToMany)
                            {
                                realUsePopertyMethod = useBaseBagManyInfo.MakeGenericMethod(new Type[] { useType, onePropertie.PropertyType.DetermineCollectionElementType() });

                                //不同的执行分支
                                realUsePopertyMethod.Invoke(null, new object[] { inputValue, Expression.Lambda(getPropertyValue, target), !useAttribute.IfIsManyToManyControl });

                                continue;
                            }
                            else
                            {
                                realUsePopertyMethod = useBaseBagInfo.MakeGenericMethod(new Type[] { useType, onePropertie.PropertyType.DetermineCollectionElementType() });
                            }
                        }
                        else
                        {
                            continue;
                        }
                    }
                    //Many - one 行为
                    else if (onePropertie.PropertyType.IsSubclassOf(m_baseEntityType))
                    {
                        realUsePopertyMethod = useBaseManyToOneInfo.MakeGenericMethod(new Type[] { useType, onePropertie.PropertyType });
                    }
                    //标准行为
                    else if (!((onePropertie.PropertyType.IsGenericCollection() && onePropertie.PropertyType.IsInterface)))
                    {
                        realUsePopertyMethod = useBasePropertyInfo.MakeGenericMethod(new Type[] { useType, onePropertie.PropertyType });
                    }
                    //不处理行为
                    else
                    {
                        continue;
                    }

                    realUsePopertyMethod.Invoke(null, new object[] { inputValue, Expression.Lambda(getPropertyValue, target) });
                }
                else
                {
                    continue;
                }
            }
        }