Skip to content

Commit

Permalink
Make some Api public
Browse files Browse the repository at this point in the history
  • Loading branch information
chinadragon0515 committed Sep 1, 2016
1 parent 0e070dc commit aac986b
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 76 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<br />Report issues on [Github issues](https://github.com/OData/RESTier/issues).
- Questions<br />Ask questions on [Stack Overflow](http://stackoverflow.com/questions/ask?tags=odata).
- Feedback<br />Please send mails to [[email protected]](mailto:[email protected]).
Expand Down
49 changes: 1 addition & 48 deletions src/Microsoft.Restier.Publishers.OData/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,22 +164,6 @@ public static IDictionary<string, PropertyAttributes> RetrievePropertiesAttribut
return propertiesAttributes;
}

public static Type GetClrType(this IEdmType edmType, ApiBase api)
{
IEdmModel edmModel = api.GetModelAsync().Result;

ClrTypeAnnotation annotation = edmModel.GetAnnotationValue<ClrTypeAnnotation>(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
Expand All @@ -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)
Expand Down
112 changes: 93 additions & 19 deletions src/Microsoft.Restier.Publishers.OData/Model/EdmHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
/// <summary>
/// This class contains some common extension methods for Edm
/// </summary>
public static class EdmHelpers
{
private const string DefaultEntityContainerName = "DefaultContainer";

public static EdmEntityContainer EnsureEntityContainer(this EdmModel model, Type apiType)
/// <summary>
/// The type to get the primitive type reference
/// </summary>
/// <param name="type">The clr type to get edm type reference</param>
/// <returns>The edm type reference for the clr type</returns>
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);
}

/// <summary>
/// Get the clr type for a specified edm type
/// </summary>
/// <param name="edmType">The edm type to get clr type</param>
/// <param name="serviceProvider">The provider to get service from DI container</param>
/// <returns>The clr type</returns>
public static Type GetClrType(this IEdmType edmType, IServiceProvider serviceProvider)
{
IEdmModel edmModel = serviceProvider.GetService<IEdmModel>();

ClrTypeAnnotation annotation = edmModel.GetAnnotationValue<ClrTypeAnnotation>(edmType);
if (annotation != null)
{
return annotation.ClrType;
}

throw new NotSupportedException(string.Format(
CultureInfo.InvariantCulture,
Resources.ElementTypeNotFound,
edmType.FullTypeName()));
}

/// <summary>
/// Get the edm type reference for a clr type
/// </summary>
/// <param name="type">The clr type</param>
/// <param name="model">The Edm model</param>
/// <returns>The Edm type reference</returns>
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)
Expand All @@ -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;
Expand All @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
Expand Down
11 changes: 6 additions & 5 deletions src/Microsoft.Restier.Publishers.OData/RestierController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -159,8 +160,8 @@ public async Task<IHttpActionResult> 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,
Expand Down Expand Up @@ -238,7 +239,7 @@ public async Task<IHttpActionResult> Delete(CancellationToken cancellationToken)

DataModificationItem deleteItem = new DataModificationItem(
entitySet.Name,
path.EdmType.GetClrType(Api),
path.EdmType.GetClrType(Api.ServiceProvider),
null,
DataModificationItemAction.Remove,
RestierQueryBuilder.GetPathKeyValues(path),
Expand Down Expand Up @@ -388,8 +389,8 @@ private async Task<IHttpActionResult> 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,
Expand Down
20 changes: 20 additions & 0 deletions test/Microsoft.Restier.TestCommon/PublicApi.bsl
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
]
Expand Down

0 comments on commit aac986b

Please sign in to comment.