Ejemplo n.º 1
0
        private string createISvcMethods()
        {
            CSharp2TS csharp2TS = new CSharp2TS();
            var       sb        = new StringBuilder(512);

            sb.AppendLine("export interface ISvcMethods");
            sb.AppendLine("{");
            StringBuilder sbArgs = new StringBuilder(255);

            this._metadata.methods.ForEach((methodInfo) =>
            {
                if (!methodInfo.isQuery)
                {
                    sbArgs.Length = 0;
                    if (methodInfo.parameters.Count() > 0)
                    {
                        sbArgs.AppendLine("(args: {");
                        methodInfo.parameters.ForEach((paramInfo) =>
                        {
                            sbArgs.AppendFormat("\t\t{0}{1}: {2}{3};", paramInfo.name, paramInfo.isNullable ? "?" : "", CSharp2TS.GetTSType(paramInfo.dataType), paramInfo.isArray ? "[]" : "");
                            sbArgs.AppendLine();
                        });
                        if (methodInfo.methodResult)
                        {
                            sbArgs.Append("\t}) => IPromise<");
                            sbArgs.Append(csharp2TS.GetTSTypeName(methodInfo.methodInfo.ReturnType));
                            sbArgs.Append(">");
                        }
                        else
                        {
                            sbArgs.Append("\t}) => IPromise<any>");
                        }
                    }
                    else
                    {
                        if (methodInfo.methodResult)
                        {
                            sbArgs.Append("() => IPromise<");
                            sbArgs.Append(csharp2TS.GetTSTypeName(methodInfo.methodInfo.ReturnType));
                            sbArgs.Append(">");
                        }
                        else
                        {
                            sbArgs.Append("() => IPromise<any>");
                        }
                    }

                    sb.AppendFormat("\t{0}: {1};", methodInfo.methodName, sbArgs.ToString());
                    sb.AppendLine();
                }
            });
            sb.AppendLine("}");

            StringBuilder sbResult = csharp2TS.GetInterfaceDeclarations();

            sbResult.Append(sb.ToString());
            return(sbResult.ToString());
        }
Ejemplo n.º 2
0
        public string GetTSTypeName(Type t)
        {
            bool   isArray      = false;
            bool   isEnumerable = false;
            string res          = "any";

            try
            {
                if (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(IEnumerable <>))
                {
                    t            = t.GetGenericArguments().First();
                    isEnumerable = true;
                }
                else if (t.IsArray)
                {
                    isEnumerable = true;
                    t            = t.GetElementType();
                }
                else if (t is System.Collections.IEnumerable)
                {
                    isEnumerable = true;
                    return("any[]");
                }

                DataType dtype = this.valConverter.DataTypeFromType(t, out isArray);
                res = CSharp2TS.GetTSType(dtype);
                if (isArray || isEnumerable)
                {
                    res = string.Format("{0}[]", res);
                }
                return(res);
            }
            catch
            {
                //complex type
                try
                {
                    string typeName = string.Format("I{0}", t.Name);
                    res = this.GetTSInterface(t, typeName);
                    if (isArray || isEnumerable)
                    {
                        return(string.Format("{0}[]", typeName));
                    }
                    else
                    {
                        return(typeName);
                    }
                }
                catch
                {
                    return("any");
                }
            }
        }
Ejemplo n.º 3
0
        private string GetFieldDataType(FieldInfo fieldInfo)
        {
            string   fieldName = fieldInfo.fieldName;
            string   fieldType = "any";
            DataType dataType  = fieldInfo.dataType;

            if (fieldInfo.isNavigation)
            {
                fieldType = fieldInfo._TypeScriptDataType;
            }
            else
            {
                fieldType = CSharp2TS.GetTSType(dataType);
            }
            return(fieldType);
        }
Ejemplo n.º 4
0
        /*
         * private string createQueryNames()
         * {
         *  var sb = new StringBuilder(256);
         *  sb.AppendLine("export var QUERY_NAME =");
         *  sb.Append("{");
         *  bool isFirst = true;
         *  this._metadata.methods.ForEach((methodInfo) =>
         *  {
         *      if (methodInfo.isQuery)
         *      {
         *          if (!isFirst)
         *          {
         *              sb.Append(",");
         *              sb.AppendLine();
         *          }
         *          sb.AppendFormat("\t{0}: '{0}'", methodInfo.methodName);
         *
         *          isFirst = false;
         *      }
         *  });
         *  sb.AppendLine();
         *  sb.AppendLine("};");
         *  return sb.ToString();
         * }
         */

        private string createDbSetQueries(DbSetInfo dbSetInfo)
        {
            var sb       = new StringBuilder(256);
            var sbArgs   = new StringBuilder(256);
            var selected = this._metadata.methods.Where((m) => m.isQuery && m.methodInfo.ReturnType.GetGenericArguments().First() == dbSetInfo.EntityType).ToList();

            selected.ForEach((methodInfo) =>
            {
                sbArgs.Length = 0;
                sbArgs.AppendLine("args?: {");
                methodInfo.parameters.ForEach((paramInfo) =>
                {
                    sbArgs.AppendFormat("\t\t{0}{1}: {2}{3};", paramInfo.name, paramInfo.isNullable ? "?" : "", CSharp2TS.GetTSType(paramInfo.dataType), paramInfo.isArray ? "[]" : "");
                    sbArgs.AppendLine();
                });
                sbArgs.Append("\t}");
                if (methodInfo.parameters.Count() == 0)
                {
                    sbArgs.Length = 0;
                }
                sb.AppendFormat("\tcreate{0}Query({1})", methodInfo.methodName, sbArgs.ToString());
                sb.AppendLine();
                sb.Append("\t{");
                sb.AppendLine();
                if (sbArgs.Length > 0)
                {
                    sb.AppendFormat("\t\tvar query = this.createQuery('{0}');", methodInfo.methodName);
                    sb.AppendLine();
                    sb.AppendLine("\t\tquery.params = args;");
                    sb.AppendLine("\t\treturn query;");
                }
                else
                {
                    sb.AppendFormat("\t\treturn this.createQuery('{0}');", methodInfo.methodName);
                    sb.AppendLine();
                }
                sb.AppendLine("\t}");
            });
            return(sb.ToString());
        }