示例#1
0
        public string GenerateCode()
        {
            var context = new CodeGenerationContext(new Uri(MetadataDocumentUri, UriKind.Absolute), NamespacePrefix, _configuration.Credentials, _configuration.WebProxy, _configuration.AcceptInvalidCertificate)
            {
                UseDataServiceCollection = _configuration.UseDataServiceCollection,
                EnableNamingAlias        = _configuration.EnableNamingAlias,
                IgnoreUnexpectedElementsAndAttributes = _configuration.IgnoreUnexpectedElementsAndAttributes
            };

            var template = new ODataClientCSharpTemplate(context);

            return(template.TransformText());
        }
示例#2
0
        /// <summary>
        /// Gets the clr type name from the give type reference.
        /// </summary>
        /// <param name="edmTypeReference">The type reference in question.</param>
        /// <param name="useDataServiceCollection">true to use the DataServicCollection type for entity collections and the ObservableCollection type for non-entity collections,
        /// false to use Collection for collections.</param>
        /// <param name="clientTemplate">ODataClientTemplate instance that call this method.</param>
        /// <param name="context">CodeGenerationContext instance in the clientTemplate.</param>
        /// <param name="addNullableTemplate">This flag indicates whether to return the type name in nullable format</param>
        /// <param name="needGlobalPrefix">The flag indicates whether the namespace need to be added by global prefix</param>
        /// <param name="isOperationParameter">This flag indicates whether the edmTypeReference is for an operation parameter</param>
        /// <returns>The clr type name of the type reference.</returns>
        internal static string GetClrTypeName(IEdmTypeReference edmTypeReference, bool useDataServiceCollection, ODataClientTemplate clientTemplate, CodeGenerationContext context, bool addNullableTemplate = true, bool needGlobalPrefix = true, bool isOperationParameter = false)
        {
            string clrTypeName;
            var    edmType          = edmTypeReference.Definition;
            var    edmPrimitiveType = edmType as IEdmPrimitiveType;

            if (edmPrimitiveType != null)
            {
                clrTypeName = GetClrTypeName(edmPrimitiveType, clientTemplate);
                if (edmTypeReference.IsNullable && !clientTemplate.ClrReferenceTypes.Contains(edmPrimitiveType.PrimitiveKind) && addNullableTemplate)
                {
                    clrTypeName = string.Format(clientTemplate.SystemNullableStructureTemplate, clrTypeName);
                }
            }
            else
            {
                var edmComplexType = edmType as IEdmComplexType;
                if (edmComplexType != null)
                {
                    clrTypeName = context.GetPrefixedFullName(edmComplexType,
                                                              context.EnableNamingAlias ? clientTemplate.GetFixedName(Customization.CustomizeNaming(edmComplexType.Name)) : clientTemplate.GetFixedName(edmComplexType.Name), clientTemplate);
                }
                else
                {
                    var edmEnumType = edmType as IEdmEnumType;
                    if (edmEnumType != null)
                    {
                        clrTypeName = context.GetPrefixedFullName(edmEnumType,
                                                                  context.EnableNamingAlias ? clientTemplate.GetFixedName(Customization.CustomizeNaming(edmEnumType.Name)) : clientTemplate.GetFixedName(edmEnumType.Name), clientTemplate, needGlobalPrefix);
                        if (edmTypeReference.IsNullable && addNullableTemplate)
                        {
                            clrTypeName = string.Format(clientTemplate.SystemNullableStructureTemplate, clrTypeName);
                        }
                    }
                    else
                    {
                        var edmEntityType = edmType as IEdmEntityType;
                        if (edmEntityType != null)
                        {
                            clrTypeName = context.GetPrefixedFullName(edmEntityType,
                                                                      context.EnableNamingAlias ? clientTemplate.GetFixedName(Customization.CustomizeNaming(edmEntityType.Name)) : clientTemplate.GetFixedName(edmEntityType.Name), clientTemplate);
                        }
                        else
                        {
                            var edmCollectionType    = (IEdmCollectionType)edmType;
                            var elementTypeReference = edmCollectionType.ElementType;
                            var primitiveElementType = elementTypeReference.Definition as IEdmPrimitiveType;
                            if (primitiveElementType != null)
                            {
                                clrTypeName = GetClrTypeName(primitiveElementType, clientTemplate);
                            }
                            else
                            {
                                var schemaElement = (IEdmSchemaElement)elementTypeReference.Definition;
                                clrTypeName = context.GetPrefixedFullName(schemaElement,
                                                                          context.EnableNamingAlias ? clientTemplate.GetFixedName(Customization.CustomizeNaming(schemaElement.Name)) : clientTemplate.GetFixedName(schemaElement.Name), clientTemplate);
                            }

                            var collectionTypeName = isOperationParameter
                                ? clientTemplate.CollectionOfTStructureTemplate
                                : (useDataServiceCollection
                                    ? (elementTypeReference.TypeKind() == EdmTypeKind.Entity
                                        ? clientTemplate.DataServiceCollectionStructureTemplate
                                        : clientTemplate.ObservableCollectionStructureTemplate)
                                    : clientTemplate.ObjectModelCollectionStructureTemplate);

                            clrTypeName = string.Format(collectionTypeName, clrTypeName);
                        }
                    }
                }
            }

            return(clrTypeName);
        }
示例#3
0
        /// <summary>
        /// Gets the value expression to initualize the property with.
        /// </summary>
        /// <param name="property">The property in question.</param>
        /// <param name="useDataServiceCollection">true to use the DataServicCollection type for entity collections and the ObservableCollection type for non-entity collections,
        /// false to use Collection for collections.</param>
        /// <param name="clientTemplate">ODataClientTemplate instance that call this method.</param>
        /// <param name="context">CodeGenerationContext instance in the clientTemplate.</param>
        /// <returns>The value expression to initualize the property with.</returns>
        internal static string GetPropertyInitializationValue(IEdmProperty property, bool useDataServiceCollection, ODataClientTemplate clientTemplate, CodeGenerationContext context)
        {
            var edmTypeReference           = property.Type;
            var edmCollectionTypeReference = edmTypeReference as IEdmCollectionTypeReference;

            if (edmCollectionTypeReference == null)
            {
                var structuredProperty = property as IEdmStructuralProperty;
                if (structuredProperty != null)
                {
                    if (!string.IsNullOrEmpty(structuredProperty.DefaultValueString))
                    {
                        var valueClrType     = GetClrTypeName(edmTypeReference, useDataServiceCollection, clientTemplate, context);
                        var defaultValue     = structuredProperty.DefaultValueString;
                        var isCSharpTemplate = clientTemplate is ODataClientCSharpTemplate;
                        if (edmTypeReference.Definition.TypeKind == EdmTypeKind.Enum)
                        {
                            var enumValues           = defaultValue.Split(',');
                            var fullenumTypeName     = GetClrTypeName(edmTypeReference, useDataServiceCollection, clientTemplate, context);
                            var enumTypeName         = GetClrTypeName(edmTypeReference, useDataServiceCollection, clientTemplate, context, false, false);
                            var customizedEnumValues = new List <string>();
                            foreach (var enumValue in enumValues)
                            {
                                var currentEnumValue = enumValue.Trim();
                                var indexFirst       = currentEnumValue.IndexOf('\'') + 1;
                                var indexLast        = currentEnumValue.LastIndexOf('\'');
                                if (indexFirst > 0 && indexLast > indexFirst)
                                {
                                    currentEnumValue = currentEnumValue.Substring(indexFirst, indexLast - indexFirst);
                                }

                                var customizedEnumValue = context.EnableNamingAlias ? Customization.CustomizeNaming(currentEnumValue) : currentEnumValue;
                                if (isCSharpTemplate)
                                {
                                    currentEnumValue = "(" + fullenumTypeName + ")" + clientTemplate.EnumTypeName + ".Parse(" + clientTemplate.SystemTypeTypeName + ".GetType(\"" + enumTypeName + "\"), \"" + customizedEnumValue + "\")";
                                }
                                else
                                {
                                    currentEnumValue = clientTemplate.EnumTypeName + ".Parse(" + clientTemplate.SystemTypeTypeName + ".GetType(\"" + enumTypeName + "\"), \"" + currentEnumValue + "\")";
                                }
                                customizedEnumValues.Add(currentEnumValue);
                            }
                            if (isCSharpTemplate)
                            {
                                return(string.Join(" | ", customizedEnumValues));
                            }
                            return(string.Join(" Or ", customizedEnumValues));
                        }

                        if (valueClrType.Equals(clientTemplate.StringTypeName))
                        {
                            defaultValue = "\"" + defaultValue + "\"";
                        }
                        else if (valueClrType.Equals(clientTemplate.BinaryTypeName))
                        {
                            defaultValue = "System.Text.Encoding.UTF8.GetBytes(\"" + defaultValue + "\")";
                        }
                        else if (valueClrType.Equals(clientTemplate.SingleTypeName))
                        {
                            if (isCSharpTemplate)
                            {
                                defaultValue = defaultValue.EndsWith("f", StringComparison.OrdinalIgnoreCase) ? defaultValue : defaultValue + "f";
                            }
                            else
                            {
                                defaultValue = defaultValue.EndsWith("f", StringComparison.OrdinalIgnoreCase) ? defaultValue : defaultValue + "F";
                            }
                        }
                        else if (valueClrType.Equals(clientTemplate.DecimalTypeName))
                        {
                            if (isCSharpTemplate)
                            {
                                // decimal in C# must be initialized with 'm' at the end, like Decimal dec = 3.00m
                                defaultValue = defaultValue.EndsWith("m", StringComparison.OrdinalIgnoreCase) ? defaultValue : defaultValue + "m";
                            }
                            else
                            {
                                // decimal in VB must be initialized with 'D' at the end, like Decimal dec = 3.00D
                                defaultValue = defaultValue.ToLower().Replace("m", "D");
                                defaultValue = defaultValue.EndsWith("D", StringComparison.OrdinalIgnoreCase) ? defaultValue : defaultValue + "D";
                            }
                        }
                        else if (valueClrType.Equals(clientTemplate.GuidTypeName)
                                 | valueClrType.Equals(clientTemplate.DateTimeOffsetTypeName)
                                 | valueClrType.Equals(clientTemplate.DateTypeName)
                                 | valueClrType.Equals(clientTemplate.TimeOfDayTypeName))
                        {
                            defaultValue = valueClrType + ".Parse(\"" + defaultValue + "\")";
                        }
                        else if (valueClrType.Equals(clientTemplate.DurationTypeName))
                        {
                            defaultValue = clientTemplate.XmlConvertClassName + ".ToTimeSpan(\"" + defaultValue + "\")";
                        }
                        else if (valueClrType.Contains("Microsoft.Spatial"))
                        {
                            defaultValue = string.Format(clientTemplate.GeoTypeInitializePattern, valueClrType, defaultValue);
                        }

                        return(defaultValue);
                    }
                    // doesn't have a default value
                    return(null);
                }
                // only structured property has default value
                return(null);
            }
            string constructorParameters;

            if (edmCollectionTypeReference.ElementType().IsEntity() && useDataServiceCollection)
            {
                constructorParameters = clientTemplate.DataServiceCollectionConstructorParameters;
            }
            else
            {
                constructorParameters = "()";
            }

            var clrTypeName = GetClrTypeName(edmTypeReference, useDataServiceCollection, clientTemplate, context);

            return(clientTemplate.NewModifier + clrTypeName + constructorParameters);
        }