Exemplo n.º 1
0
 public void SetTypeMappers(MappingEngineCollection coll)
 {
     foreach (var binder in coll.Binders)
     {
         TypeMappers.Add(binder);
     }
 }
Exemplo n.º 2
0
        protected Tuple <List <Expression>, ParameterExpression, ParameterExpression> GetMapExpressions(Type src, Type dest)
        {
            var cacheKey = MappingServiceProvider.CalculateCacheKey(src, dest);

            if (TypeMappers.ContainsKey(cacheKey))
            {
                return(TypeMappers[cacheKey].GetMapExpressions());
            }
            throw new MapNotImplementedException(string.Format("There is no mapping has bee found. Source Type: {0}, Destination Type: {1}", src.FullName, dest.FullName));
        }
Exemplo n.º 3
0
        public StringFormatContext()
        {
            typeStringBuilder = new StringBuilder();

            var mappers = TypeMappers.ToArray();

            TypeMappers.Clear();

            RegisterTypeMapper(new ToStringMapper <object>());
            mappers.Reverse().ForEach(RegisterTypeMapper);
        }
Exemplo n.º 4
0
        /// <summary>处理异常类型映射</summary>
        /// <param name="ex">异常实例</param>
        /// <param name="service">服务名称,MVC控制器或API控制器默认为控制器名称(不包括后缀Controller)</param>
        /// <param name="exceptionModel">异常数据</param>
        public WebExceptionModel Handle(Exception ex, string service, ExceptionModel exceptionModel)
        {
            var webExceptionModel = ex.ToWebModel();

            if (webExceptionModel != null)
            {
                return(webExceptionModel);
            }
            if (exceptionModel.Code.IsNullOrEmpty())
            {
                ExceptionModel exceptionModel2 = null;
                var            exType          = ex.GetType();
                do
                {
                    exceptionModel2 = TypeMappers.GetValueBy(exType);
                    exType          = exType.BaseType;
                } while (exceptionModel2 == null);
                exceptionModel.Code = exceptionModel2.Code;
                if (exceptionModel.Message.IsNullOrEmpty())
                {
                    exceptionModel.Message = exceptionModel2.Message;
                }
                if (exceptionModel.Details.IsNullOrEmpty())
                {
                    exceptionModel.Details = exceptionModel2.Details;
                }
            }
            else
            {
                if (service == null)
                {
                    service = "";
                }
                var codeMappers = WebCodeMappers.GetValueBy(service);
                if (codeMappers == null && service.Length > 0)
                {
                    service     = "";
                    codeMappers = WebCodeMappers.GetValueBy(service);
                }
                webExceptionModel = codeMappers.GetValueBy(exceptionModel.Code);
            }
            if (webExceptionModel == null)
            {
                var exType = ex.GetType();
                do
                {
                    webExceptionModel = WebTypeMappers.GetValueBy(exType);
                    exType            = exType.BaseType;
                } while (webExceptionModel == null);
            }
            return(webExceptionModel);
        }
Exemplo n.º 5
0
        public Expression GetMemberQueryableExpression(Type srcType, Type dstType)
        {
            var cacheKey = MappingServiceProvider.CalculateCacheKey(srcType, dstType);

            if (!TypeMappers.ContainsKey(cacheKey))
            {
                return(null);
            }

            var typeMapper = TypeMappers[cacheKey];

            if (typeMapper.QueryableGeneralExpression == null)
            {
                typeMapper.Compile(CompilationTypes.Source);
            }
            return(typeMapper.QueryableGeneralExpression);
        }
        public void SetVersions(AbstractTypeMapper mainTypeMapper, AbstractTypeMapper[] secondaryTypeMappers = null)
        {
            TypeMappers.Clear();
            MainVersion = mainTypeMapper?.SqlVersion;

            if (mainTypeMapper != null && (secondaryTypeMappers?.Contains(mainTypeMapper) != true))
            {
                TypeMappers.Add(mainTypeMapper.SqlVersion, mainTypeMapper);
            }

            if (secondaryTypeMappers != null)
            {
                foreach (var mapper in secondaryTypeMappers)
                {
                    TypeMappers.Add(mapper.SqlVersion, mapper);
                }
            }
        }
Exemplo n.º 7
0
        protected Tuple <List <Expression>, ParameterExpression, ParameterExpression> GetMapExpressions(Type src, Type dest)
        {
            var cacheKey = MappingServiceProvider.CalculateCacheKey(src, dest);

            if (TypeMappers.ContainsKey(cacheKey))
            {
                return(TypeMappers[cacheKey].GetMapExpressions());
            }

            dynamic srcInst  = Activator.CreateInstance(src);
            dynamic destInst = Activator.CreateInstance(dest);

            RegisterDynamic(srcInst, destInst);
            if (TypeMappers.ContainsKey(cacheKey))
            {
                return(TypeMappers[cacheKey].GetMapExpressions());
            }

            throw new MapNotImplementedException(
                      $"There is no mapping has been found. Source Type: {src.FullName}, Destination Type: {dest.FullName}");
        }
Exemplo n.º 8
0
 public void Reset()
 {
     CollectionMappers.Clear();
     TypeMappers.Clear();
 }
Exemplo n.º 9
0
        /// <summary>
        ///     Maps a .NET type to a TypeScript type spec. Returns null if the specified type can't be mapped. The meaning of
        ///     non-mappable types varies depending on the caller; it might be treated as an error or as a signal to ignore a
        ///     certain property or skip a certain base type.</summary>
        protected virtual ApiTypeDesc MapType(Type type)
        {
            if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Task <>))
            {
                return(MapType(type.GetGenericArguments()[0]));
            }
            if (type == typeof(Task))
            {
                return(MapType(typeof(void)));
            }

            bool nullable = Api.StrictNulls ? !type.IsValueType : false;

            if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable <>))
            {
                nullable = true;
                type     = type.GetGenericArguments()[0];
            }

            var mapped = TypeMappers.Select(m => m.MapType(type)).FirstOrDefault(t => t != null);

            if (mapped != null)
            {
                mapped.Nullable = nullable;
                if (mapped.BasicType == null)
                {
                    throw new NotSupportedException("Type converter returning a non-basic-type is not fully supported."); // missing support in converter function body generator
                }
                foreach (var import in mapped.TypeMapper.GetImports())
                {
                    Api.Imports.Add(import);
                }
                return(mapped);
            }
            else if (type == typeof(void))
            {
                return new ApiTypeDesc {
                           BasicType = "void"
                }
            }
            ;
            else if (type == typeof(object))
            {
                return new ApiTypeDesc {
                           BasicType = "any", Nullable = nullable
                }
            }
            ;
            else if (type == typeof(string))
            {
                return new ApiTypeDesc {
                           BasicType = "string", Nullable = nullable
                }
            }
            ;
            else if (type == typeof(int) || type == typeof(double) || type == typeof(decimal))
            {
                return new ApiTypeDesc {
                           BasicType = "number", Nullable = nullable
                }
            }
            ;
            else if (type == typeof(bool))
            {
                return new ApiTypeDesc {
                           BasicType = "boolean", Nullable = nullable
                }
            }
            ;
            else if (type == typeof(DateTime))
            {
                return new ApiTypeDesc {
                           BasicType = "string", Nullable = nullable
                }
            }
            ;
            else if (type.IsArray)
            {
                var elType = MapType(type.GetElementType());

                if (elType == null)
                {
                    return(null);
                }
                return(new ApiTypeDesc {
                    ArrayElementType = elType, Nullable = nullable
                });
            }
            else if (getIEnumerable(type, out var elT))
            {
                var elType = MapType(elT);
                if (elType == null)
                {
                    return(null);
                }
                return(new ApiTypeDesc {
                    ArrayElementType = elType, Nullable = nullable
                });
            }
            else if (type.IsEnum)
            {
                if (!AddEnum(type))
                {
                    return(null);
                }
                return(new ApiTypeDesc {
                    EnumType = Api.Enums[type], Nullable = nullable
                });
            }
            else
            {
                if (!AddInterface(type))
                {
                    return(null);
                }
                return(new ApiTypeDesc {
                    InterfaceType = Api.Interfaces[type], Nullable = nullable
                });
            }
        }