From aac986bfec675e58044340228ad63d9caf7d2d29 Mon Sep 17 00:00:00 2001 From: Vincent He Date: Thu, 1 Sep 2016 09:17:21 +0800 Subject: [PATCH] Make some Api public --- README.md | 9 +- .../Extensions.cs | 49 +------- .../Model/EdmHelpers.cs | 112 +++++++++++++++--- .../Operation/OperationExecutor.cs | 1 + .../Query/RestierQueryBuilder.cs | 3 +- .../RestierController.cs | 11 +- .../PublicApi.bsl | 20 ++++ 7 files changed, 129 insertions(+), 76 deletions(-) diff --git a/README.md b/README.md index fc757037..c7077248 100644 --- a/README.md +++ b/README.md @@ -62,11 +62,14 @@ The release of the component binaries is carried out regularly through [Nuget](h ## 4. Documentation Please visit the [RESTier pages](http://odata.github.io/RESTier). It has detailed descriptions on each feature provided by RESTier. -## 5. Community -### 5.1 Contribution +## 5. Sample services +Refer to [sample service github](https://github.com/OData/ODataSamples/tree/master/RESTier) for end to end sample service. The source code also contains end to end service for end to end test purpose. All the sample service can be run with visual studio 2015. + +## 6. Community +### 6.1 Contribution There are many ways for you to contribute to RESTier. The easiest way is to participate in discussion of features and issues. You can also contribute by sending pull requests of features or bug fixes to us. Contribution to the documentations is also highly welcomed. Please refer to the [CONTRIBUTING.md](https://github.com/OData/RESTier/blob/master/.github/CONTRIBUTING.md) for more details. -### 5.2 Support +### 6.2 Support - Issues
Report issues on [Github issues](https://github.com/OData/RESTier/issues). - Questions
Ask questions on [Stack Overflow](http://stackoverflow.com/questions/ask?tags=odata). - Feedback
Please send mails to [odatafeedback@microsoft.com](mailto:odatafeedback@microsoft.com). diff --git a/src/Microsoft.Restier.Publishers.OData/Extensions.cs b/src/Microsoft.Restier.Publishers.OData/Extensions.cs index 3e20a099..efabca36 100644 --- a/src/Microsoft.Restier.Publishers.OData/Extensions.cs +++ b/src/Microsoft.Restier.Publishers.OData/Extensions.cs @@ -164,22 +164,6 @@ public static IDictionary RetrievePropertiesAttribut return propertiesAttributes; } - public static Type GetClrType(this IEdmType edmType, ApiBase api) - { - IEdmModel edmModel = api.GetModelAsync().Result; - - ClrTypeAnnotation annotation = edmModel.GetAnnotationValue(edmType); - if (annotation != null) - { - return annotation.ClrType; - } - - throw new NotSupportedException(string.Format( - CultureInfo.InvariantCulture, - Resources.ElementTypeNotFound, - edmType.FullTypeName())); - } - public static IEdmTypeReference GetReturnTypeReference(this Type type, IEdmModel model) { // In case it is a nullable type, get the underlying type @@ -196,38 +180,7 @@ public static IEdmTypeReference GetReturnTypeReference(this Type type, IEdmModel type = typeof(void); } - return GetTypeReference(type, model); - } - - public static IEdmTypeReference GetTypeReference(this Type type, IEdmModel model) - { - Type elementType; - if (type.TryGetElementType(out elementType)) - { - return EdmCoreModel.GetCollection(GetTypeReference(elementType, model)); - } - - var edmType = model.FindDeclaredType(type.FullName); - - var enumType = edmType as IEdmEnumType; - if (enumType != null) - { - return new EdmEnumTypeReference(enumType, true); - } - - var complexType = edmType as IEdmComplexType; - if (complexType != null) - { - return new EdmComplexTypeReference(complexType, true); - } - - var entityType = edmType as IEdmEntityType; - if (entityType != null) - { - return new EdmEntityTypeReference(entityType, true); - } - - return type.GetPrimitiveTypeReference(); + return EdmHelpers.GetTypeReference(type, model); } public static bool IsSameTerm(this IEdmTerm sourceTerm, IEdmTerm targetTerm) diff --git a/src/Microsoft.Restier.Publishers.OData/Model/EdmHelpers.cs b/src/Microsoft.Restier.Publishers.OData/Model/EdmHelpers.cs index 6810a33c..f5244df7 100644 --- a/src/Microsoft.Restier.Publishers.OData/Model/EdmHelpers.cs +++ b/src/Microsoft.Restier.Publishers.OData/Model/EdmHelpers.cs @@ -4,15 +4,105 @@ using System; using System.Globalization; using System.Linq; +using System.Web.OData; +using Microsoft.Extensions.DependencyInjection; using Microsoft.OData.Edm; namespace Microsoft.Restier.Publishers.OData.Model { - internal static class EdmHelpers + /// + /// This class contains some common extension methods for Edm + /// + public static class EdmHelpers { private const string DefaultEntityContainerName = "DefaultContainer"; - public static EdmEntityContainer EnsureEntityContainer(this EdmModel model, Type apiType) + /// + /// The type to get the primitive type reference + /// + /// The clr type to get edm type reference + /// The edm type reference for the clr type + public static EdmTypeReference GetPrimitiveTypeReference(this Type type) + { + // Only handle primitive type right now + bool isNullable; + EdmPrimitiveTypeKind? primitiveTypeKind = EdmHelpers.GetPrimitiveTypeKind(type, out isNullable); + + if (!primitiveTypeKind.HasValue) + { + return null; + } + + return new EdmPrimitiveTypeReference( + EdmCoreModel.Instance.GetPrimitiveType(primitiveTypeKind.Value), + isNullable); + } + + /// + /// Get the clr type for a specified edm type + /// + /// The edm type to get clr type + /// The provider to get service from DI container + /// The clr type + public static Type GetClrType(this IEdmType edmType, IServiceProvider serviceProvider) + { + IEdmModel edmModel = serviceProvider.GetService(); + + ClrTypeAnnotation annotation = edmModel.GetAnnotationValue(edmType); + if (annotation != null) + { + return annotation.ClrType; + } + + throw new NotSupportedException(string.Format( + CultureInfo.InvariantCulture, + Resources.ElementTypeNotFound, + edmType.FullTypeName())); + } + + /// + /// Get the edm type reference for a clr type + /// + /// The clr type + /// The Edm model + /// The Edm type reference + public static IEdmTypeReference GetTypeReference(this Type type, IEdmModel model) + { + if (type == null || model == null) + { + return null; + } + + Type elementType; + if (type.TryGetElementType(out elementType)) + { + return EdmCoreModel.GetCollection(GetTypeReference(elementType, model)); + } + + var edmType = model.FindDeclaredType(type.FullName); + + var enumType = edmType as IEdmEnumType; + if (enumType != null) + { + return new EdmEnumTypeReference(enumType, true); + } + + var complexType = edmType as IEdmComplexType; + if (complexType != null) + { + return new EdmComplexTypeReference(complexType, true); + } + + var entityType = edmType as IEdmEntityType; + if (entityType != null) + { + return new EdmEntityTypeReference(entityType, true); + } + + return type.GetPrimitiveTypeReference(); + } + + internal static EdmEntityContainer EnsureEntityContainer(this EdmModel model, Type apiType) { var container = (EdmEntityContainer)model.EntityContainer; if (container == null) @@ -24,7 +114,7 @@ public static EdmEntityContainer EnsureEntityContainer(this EdmModel model, Type return container; } - public static IEdmEntitySet FindDeclaredEntitySetByTypeReference( + internal static IEdmEntitySet FindDeclaredEntitySetByTypeReference( this IEdmModel model, IEdmTypeReference typeReference) { IEdmTypeReference elementTypeReference; @@ -42,22 +132,6 @@ public static IEdmEntitySet FindDeclaredEntitySetByTypeReference( .SingleOrDefault(e => e.EntityType().FullTypeName() == elementTypeReference.FullName()); } - public static EdmTypeReference GetPrimitiveTypeReference(this Type type) - { - // Only handle primitive type right now - bool isNullable; - EdmPrimitiveTypeKind? primitiveTypeKind = EdmHelpers.GetPrimitiveTypeKind(type, out isNullable); - - if (!primitiveTypeKind.HasValue) - { - return null; - } - - return new EdmPrimitiveTypeReference( - EdmCoreModel.Instance.GetPrimitiveType(primitiveTypeKind.Value), - isNullable); - } - private static bool TryGetElementTypeReference( this IEdmTypeReference typeReference, out IEdmTypeReference elementTypeReference) { diff --git a/src/Microsoft.Restier.Publishers.OData/Operation/OperationExecutor.cs b/src/Microsoft.Restier.Publishers.OData/Operation/OperationExecutor.cs index fa1cbf7d..d9c427e4 100644 --- a/src/Microsoft.Restier.Publishers.OData/Operation/OperationExecutor.cs +++ b/src/Microsoft.Restier.Publishers.OData/Operation/OperationExecutor.cs @@ -16,6 +16,7 @@ using Microsoft.Restier.Core; using Microsoft.Restier.Core.Operation; using Microsoft.Restier.Publishers.OData.Formatter; +using Microsoft.Restier.Publishers.OData.Model; namespace Microsoft.Restier.Publishers.OData.Operation { diff --git a/src/Microsoft.Restier.Publishers.OData/Query/RestierQueryBuilder.cs b/src/Microsoft.Restier.Publishers.OData/Query/RestierQueryBuilder.cs index 4d2f80a5..6a327f11 100644 --- a/src/Microsoft.Restier.Publishers.OData/Query/RestierQueryBuilder.cs +++ b/src/Microsoft.Restier.Publishers.OData/Query/RestierQueryBuilder.cs @@ -9,6 +9,7 @@ using Microsoft.OData.Edm; using Microsoft.OData.UriParser; using Microsoft.Restier.Core; +using Microsoft.Restier.Publishers.OData.Model; using ODataPath = System.Web.OData.Routing.ODataPath; namespace Microsoft.Restier.Publishers.OData.Query @@ -274,7 +275,7 @@ private void HandleEntityTypeSegment(ODataPathSegment segment) if (edmType.TypeKind == EdmTypeKind.Entity) { - this.currentType = edmType.GetClrType(api); + this.currentType = edmType.GetClrType(api.ServiceProvider); this.queryable = ExpressionHelpers.OfType(this.queryable, this.currentType); } } diff --git a/src/Microsoft.Restier.Publishers.OData/RestierController.cs b/src/Microsoft.Restier.Publishers.OData/RestierController.cs index 0a3c5cb7..8eb1a128 100644 --- a/src/Microsoft.Restier.Publishers.OData/RestierController.cs +++ b/src/Microsoft.Restier.Publishers.OData/RestierController.cs @@ -26,6 +26,7 @@ using Microsoft.Restier.Core.Query; using Microsoft.Restier.Core.Submit; using Microsoft.Restier.Publishers.OData.Batch; +using Microsoft.Restier.Publishers.OData.Model; using Microsoft.Restier.Publishers.OData.Query; // This is a must for creating response with correct extension method @@ -159,8 +160,8 @@ public async Task Post(EdmEntityObject edmEntityObject, Cance DataModificationItem postItem = new DataModificationItem( entitySet.Name, - expectedEntityType.GetClrType(Api), - actualEntityType.GetClrType(Api), + expectedEntityType.GetClrType(Api.ServiceProvider), + actualEntityType.GetClrType(Api.ServiceProvider), DataModificationItemAction.Insert, null, null, @@ -238,7 +239,7 @@ public async Task Delete(CancellationToken cancellationToken) DataModificationItem deleteItem = new DataModificationItem( entitySet.Name, - path.EdmType.GetClrType(Api), + path.EdmType.GetClrType(Api.ServiceProvider), null, DataModificationItemAction.Remove, RestierQueryBuilder.GetPathKeyValues(path), @@ -388,8 +389,8 @@ private async Task Update( DataModificationItem updateItem = new DataModificationItem( entitySet.Name, - expectedEntityType.GetClrType(Api), - actualEntityType.GetClrType(Api), + expectedEntityType.GetClrType(Api.ServiceProvider), + actualEntityType.GetClrType(Api.ServiceProvider), DataModificationItemAction.Update, RestierQueryBuilder.GetPathKeyValues(path), propertiesInEtag, diff --git a/test/Microsoft.Restier.TestCommon/PublicApi.bsl b/test/Microsoft.Restier.TestCommon/PublicApi.bsl index 5fadb9b2..00fbb3d3 100644 --- a/test/Microsoft.Restier.TestCommon/PublicApi.bsl +++ b/test/Microsoft.Restier.TestCommon/PublicApi.bsl @@ -626,6 +626,26 @@ public class Microsoft.Restier.Publishers.OData.Formatter.RestierResourceSetSeri public virtual void WriteObject (object graph, System.Type type, Microsoft.OData.ODataMessageWriter messageWriter, System.Web.OData.Formatter.Serialization.ODataSerializerContext writeContext) } +[ +ExtensionAttribute(), +] +public sealed class Microsoft.Restier.Publishers.OData.Model.EdmHelpers { + [ + ExtensionAttribute(), + ] + public static System.Type GetClrType (Microsoft.OData.Edm.IEdmType edmType, System.IServiceProvider serviceProvider) + + [ + ExtensionAttribute(), + ] + public static Microsoft.OData.Edm.EdmTypeReference GetPrimitiveTypeReference (System.Type type) + + [ + ExtensionAttribute(), + ] + public static Microsoft.OData.Edm.IEdmTypeReference GetTypeReference (System.Type type, Microsoft.OData.Edm.IEdmModel model) +} + [ AttributeUsageAttribute(), ]