From 0e070dcf9bf4a2b30c153b40febe09b84ecff46a Mon Sep 17 00:00:00 2001 From: Vincent He Date: Tue, 30 Aug 2016 15:10:42 +0800 Subject: [PATCH] Address comments --- src/Microsoft.Restier.Core/ApiBaseExtensions.cs | 5 +++-- src/Microsoft.Restier.Core/Model/IModelMapper.cs | 12 ++++++------ .../Model/ModelMapper.cs | 12 ++++++------ .../Model/ModelProducer.cs | 4 ++-- .../Submit/ChangeSetInitializer.cs | 4 ++-- .../Model/ModelMapper.cs | 16 ++++++++-------- .../Model/RestierModelExtender.cs | 8 ++++---- .../Operation/OperationExecutor.cs | 6 +++--- .../Query/RestierQueryExecutor.cs | 2 +- .../Routing/HttpConfigurationExtensions.cs | 6 +++--- test/Microsoft.Restier.Core.Tests/Api.Tests.cs | 4 ++-- .../FallbackTests.cs | 4 ++-- .../StoreApi.cs | 4 ++-- test/Microsoft.Restier.TestCommon/PublicApi.bsl | 4 ++-- 14 files changed, 46 insertions(+), 45 deletions(-) diff --git a/src/Microsoft.Restier.Core/ApiBaseExtensions.cs b/src/Microsoft.Restier.Core/ApiBaseExtensions.cs index 9dbef032..ecde1601 100644 --- a/src/Microsoft.Restier.Core/ApiBaseExtensions.cs +++ b/src/Microsoft.Restier.Core/ApiBaseExtensions.cs @@ -524,13 +524,14 @@ private static Type EnsureElementType( var mapper = api.GetApiService(); if (mapper != null) { + var modelContext = new ModelContext(api.ServiceProvider); if (namespaceName == null) { - mapper.TryGetRelevantType(api, name, out elementType); + mapper.TryGetRelevantType(modelContext, name, out elementType); } else { - mapper.TryGetRelevantType(api, namespaceName, name, out elementType); + mapper.TryGetRelevantType(modelContext, namespaceName, name, out elementType); } } diff --git a/src/Microsoft.Restier.Core/Model/IModelMapper.cs b/src/Microsoft.Restier.Core/Model/IModelMapper.cs index b9afa1df..9bfa2ab4 100644 --- a/src/Microsoft.Restier.Core/Model/IModelMapper.cs +++ b/src/Microsoft.Restier.Core/Model/IModelMapper.cs @@ -15,8 +15,8 @@ public interface IModelMapper /// Tries to get the relevant type of an entity /// set, singleton, or composable function import. /// - /// - /// An API. + /// + /// The context for model mapper. /// /// /// The name of an entity set, singleton or composable function import. @@ -48,15 +48,15 @@ public interface IModelMapper /// /// bool TryGetRelevantType( - ApiBase apiBase, + ModelContext context, string name, out Type relevantType); /// /// Tries to get the relevant type of a composable function. /// - /// - /// An API. + /// + /// The context for model mapper. /// /// /// The name of a namespace containing a composable function. @@ -85,7 +85,7 @@ bool TryGetRelevantType( /// /// bool TryGetRelevantType( - ApiBase apiBase, + ModelContext context, string namespaceName, string name, out Type relevantType); diff --git a/src/Microsoft.Restier.Providers.EntityFramework/Model/ModelMapper.cs b/src/Microsoft.Restier.Providers.EntityFramework/Model/ModelMapper.cs index 8efb5368..062f73c5 100644 --- a/src/Microsoft.Restier.Providers.EntityFramework/Model/ModelMapper.cs +++ b/src/Microsoft.Restier.Providers.EntityFramework/Model/ModelMapper.cs @@ -35,8 +35,8 @@ public ModelMapper(Type dbContextType) /// Tries to get the relevant type of an entity /// set, singleton, or composable function import. /// - /// - /// An API. + /// + /// The context for model mapper. /// /// /// The name of an entity set, singleton or composable function import. @@ -50,7 +50,7 @@ public ModelMapper(Type dbContextType) /// provided; otherwise, false. /// public bool TryGetRelevantType( - ApiBase apiBase, + ModelContext context, string name, out Type relevantType) { @@ -77,8 +77,8 @@ public bool TryGetRelevantType( /// /// Tries to get the relevant type of a composable function. /// - /// - /// An API. + /// + /// The context for model mapper. /// /// /// The name of a namespace containing a composable function. @@ -95,7 +95,7 @@ public bool TryGetRelevantType( /// provided; otherwise, false. /// public bool TryGetRelevantType( - ApiBase apiBase, + ModelContext context, string namespaceName, string name, out Type relevantType) diff --git a/src/Microsoft.Restier.Providers.EntityFramework/Model/ModelProducer.cs b/src/Microsoft.Restier.Providers.EntityFramework/Model/ModelProducer.cs index 0baf332b..b67228b9 100644 --- a/src/Microsoft.Restier.Providers.EntityFramework/Model/ModelProducer.cs +++ b/src/Microsoft.Restier.Providers.EntityFramework/Model/ModelProducer.cs @@ -48,7 +48,7 @@ public Task GetModelAsync(ModelContext context, CancellationToken can Ensure.NotNull(context, "context"); #if EF7 - var dbContext = context.ServiceProvider.GetService(); + var dbContext = context.GetApiService(); context.ResourceSetTypeMap = dbContext.GetType().GetProperties() .Where(e => e.PropertyType.FindGenericType(typeof(DbSet<>)) != null) .ToDictionary(e => e.Name, e => e.PropertyType.GetGenericArguments()[0]); @@ -59,7 +59,7 @@ public Task GetModelAsync(ModelContext context, CancellationToken can #else var resourceSetTypeMap = new Dictionary(); var resourceTypeKeyPropertiesMap = new Dictionary>(); - var dbContext = context.ServiceProvider.GetService(); + var dbContext = context.GetApiService(); var efModel = (dbContext as IObjectContextAdapter).ObjectContext.MetadataWorkspace; var efEntityContainer = efModel.GetItems(DataSpace.CSpace).Single(); diff --git a/src/Microsoft.Restier.Providers.EntityFramework/Submit/ChangeSetInitializer.cs b/src/Microsoft.Restier.Providers.EntityFramework/Submit/ChangeSetInitializer.cs index ad8f2985..3c6d5db1 100644 --- a/src/Microsoft.Restier.Providers.EntityFramework/Submit/ChangeSetInitializer.cs +++ b/src/Microsoft.Restier.Providers.EntityFramework/Submit/ChangeSetInitializer.cs @@ -36,7 +36,7 @@ public async Task InitializeAsync( SubmitContext context, CancellationToken cancellationToken) { - DbContext dbContext = context.ServiceProvider.GetService(); + DbContext dbContext = context.GetApiService(); foreach (var entry in context.ChangeSet.Entries.OfType()) { @@ -145,7 +145,7 @@ private static async Task FindResource( DataModificationItem item, CancellationToken cancellationToken) { - var apiBase = context.ServiceProvider.GetService(); + var apiBase = context.GetApiService(); IQueryable query = apiBase.GetQueryableSource(item.ResourceSetName); query = item.ApplyTo(query); diff --git a/src/Microsoft.Restier.Publishers.OData/Model/ModelMapper.cs b/src/Microsoft.Restier.Publishers.OData/Model/ModelMapper.cs index 92cf0b59..63efc756 100644 --- a/src/Microsoft.Restier.Publishers.OData/Model/ModelMapper.cs +++ b/src/Microsoft.Restier.Publishers.OData/Model/ModelMapper.cs @@ -21,8 +21,8 @@ internal class ModelMapper : IModelMapper /// Tries to get the relevant type of an entity /// set, singleton, or composable function import. /// - /// - /// An API. + /// + /// The context for model mapper. /// /// /// The name of an entity set, singleton or composable function import. @@ -36,12 +36,12 @@ internal class ModelMapper : IModelMapper /// provided; otherwise, false. /// public bool TryGetRelevantType( - ApiBase api, + ModelContext context, string name, out Type relevantType) { // Cannot await as cannot make method async - var model = api.GetModelAsync().Result; + var model = context.GetApiService(); var element = model.EntityContainer.Elements.Where(e => e.Name == name).FirstOrDefault(); if (element != null) @@ -73,14 +73,14 @@ public bool TryGetRelevantType( } } - return InnerMapper.TryGetRelevantType(api, name, out relevantType); + return InnerMapper.TryGetRelevantType(context, name, out relevantType); } /// /// Tries to get the relevant type of a composable function. /// - /// - /// An API. + /// + /// The context for model mapper. /// /// /// The name of a namespace containing a composable function. @@ -97,7 +97,7 @@ public bool TryGetRelevantType( /// provided; otherwise, false. /// public bool TryGetRelevantType( - ApiBase api, + ModelContext context, string namespaceName, string name, out Type relevantType) diff --git a/src/Microsoft.Restier.Publishers.OData/Model/RestierModelExtender.cs b/src/Microsoft.Restier.Publishers.OData/Model/RestierModelExtender.cs index 67e975cb..ee42aafd 100644 --- a/src/Microsoft.Restier.Publishers.OData/Model/RestierModelExtender.cs +++ b/src/Microsoft.Restier.Publishers.OData/Model/RestierModelExtender.cs @@ -372,12 +372,12 @@ public ModelMapper(RestierModelExtender modelCache) /// public bool TryGetRelevantType( - ApiBase api, + ModelContext context, string name, out Type relevantType) { if (this.InnerModelMapper != null && - this.InnerModelMapper.TryGetRelevantType(api, name, out relevantType)) + this.InnerModelMapper.TryGetRelevantType(context, name, out relevantType)) { return true; } @@ -403,13 +403,13 @@ public bool TryGetRelevantType( /// public bool TryGetRelevantType( - ApiBase api, + ModelContext context, string namespaceName, string name, out Type relevantType) { if (this.InnerModelMapper != null && - this.InnerModelMapper.TryGetRelevantType(api, namespaceName, name, out relevantType)) + this.InnerModelMapper.TryGetRelevantType(context, namespaceName, name, out relevantType)) { return true; } diff --git a/src/Microsoft.Restier.Publishers.OData/Operation/OperationExecutor.cs b/src/Microsoft.Restier.Publishers.OData/Operation/OperationExecutor.cs index 0be1a08a..fa1cbf7d 100644 --- a/src/Microsoft.Restier.Publishers.OData/Operation/OperationExecutor.cs +++ b/src/Microsoft.Restier.Publishers.OData/Operation/OperationExecutor.cs @@ -39,7 +39,7 @@ public async Task ExecuteOperationAsync( var parameterArray = method.GetParameters(); - var model = context.ServiceProvider.GetService(); + var model = context.GetApiService(); // Parameters of method and model is exactly mapped or there is parsing error var parameters = new object[parameterArray.Length]; @@ -195,7 +195,7 @@ private static async Task InvokeAuthorizers( private static void PerformPreEvent(OperationContext context, CancellationToken cancellationToken) { - var processor = context.ServiceProvider.GetService(); + var processor = context.GetApiService(); if (processor != null) { processor.OnOperationExecutingAsync(context, cancellationToken); @@ -204,7 +204,7 @@ private static void PerformPreEvent(OperationContext context, CancellationToken private static void PerformPostEvent(OperationContext context, CancellationToken cancellationToken) { - var processor = context.ServiceProvider.GetService(); + var processor = context.GetApiService(); if (processor != null) { processor.OnOperationExecutedAsync(context, cancellationToken); diff --git a/src/Microsoft.Restier.Publishers.OData/Query/RestierQueryExecutor.cs b/src/Microsoft.Restier.Publishers.OData/Query/RestierQueryExecutor.cs index a17f5624..15cc294a 100644 --- a/src/Microsoft.Restier.Publishers.OData/Query/RestierQueryExecutor.cs +++ b/src/Microsoft.Restier.Publishers.OData/Query/RestierQueryExecutor.cs @@ -20,7 +20,7 @@ public async Task ExecuteQueryAsync( IQueryable query, CancellationToken cancellationToken) { - var countOption = context.ServiceProvider.GetService(); + var countOption = context.GetApiService(); if (countOption.IncludeTotalCount) { var countQuery = ExpressionHelpers.GetCountableQuery(query); diff --git a/src/Microsoft.Restier.Publishers.OData/Routing/HttpConfigurationExtensions.cs b/src/Microsoft.Restier.Publishers.OData/Routing/HttpConfigurationExtensions.cs index bb315bb0..98507828 100644 --- a/src/Microsoft.Restier.Publishers.OData/Routing/HttpConfigurationExtensions.cs +++ b/src/Microsoft.Restier.Publishers.OData/Routing/HttpConfigurationExtensions.cs @@ -52,9 +52,9 @@ public static Task MapRestierRoute( ApiBase.AddPublisherServices( typeof(TApi), services => - { - services.AddODataServices(); - }); + { + services.AddODataServices(); + }); Func func = () => new RestierContainerBuilder(typeof(TApi)); config.UseCustomContainerBuilder(func); diff --git a/test/Microsoft.Restier.Core.Tests/Api.Tests.cs b/test/Microsoft.Restier.Core.Tests/Api.Tests.cs index bd3c3e6f..22bbc703 100644 --- a/test/Microsoft.Restier.Core.Tests/Api.Tests.cs +++ b/test/Microsoft.Restier.Core.Tests/Api.Tests.cs @@ -36,7 +36,7 @@ public Task GetModelAsync(ModelContext context, CancellationToken can private class TestModelMapper : IModelMapper { public bool TryGetRelevantType( - ApiBase context, + ModelContext context, string name, out Type relevantType) { relevantType = typeof(string); @@ -44,7 +44,7 @@ public bool TryGetRelevantType( } public bool TryGetRelevantType( - ApiBase context, + ModelContext context, string namespaceName, string name, out Type relevantType) { diff --git a/test/Microsoft.Restier.Publishers.OData.Test/FallbackTests.cs b/test/Microsoft.Restier.Publishers.OData.Test/FallbackTests.cs index 16fc19e0..4c72f9d6 100644 --- a/test/Microsoft.Restier.Publishers.OData.Test/FallbackTests.cs +++ b/test/Microsoft.Restier.Publishers.OData.Test/FallbackTests.cs @@ -174,14 +174,14 @@ public Expression ReplaceQueryableSource(QueryExpressionContext context, bool em class FallbackModelMapper : IModelMapper { - public bool TryGetRelevantType(ApiBase context, string name, out Type relevantType) + public bool TryGetRelevantType(ModelContext context, string name, out Type relevantType) { relevantType = name == "Person" ? typeof(Person) : typeof(Order); return true; } - public bool TryGetRelevantType(ApiBase context, string namespaceName, string name, out Type relevantType) + public bool TryGetRelevantType(ModelContext context, string namespaceName, string name, out Type relevantType) { return TryGetRelevantType(context, name, out relevantType); } diff --git a/test/Microsoft.Restier.Publishers.OData.Test/StoreApi.cs b/test/Microsoft.Restier.Publishers.OData.Test/StoreApi.cs index aaf1980e..a7d33b3e 100644 --- a/test/Microsoft.Restier.Publishers.OData.Test/StoreApi.cs +++ b/test/Microsoft.Restier.Publishers.OData.Test/StoreApi.cs @@ -83,7 +83,7 @@ class Address class TestModelMapper : IModelMapper { - public bool TryGetRelevantType(ApiBase context, string name, out Type relevantType) + public bool TryGetRelevantType(ModelContext context, string name, out Type relevantType) { if (name == "Products") { @@ -105,7 +105,7 @@ public bool TryGetRelevantType(ApiBase context, string name, out Type relevantTy return true; } - public bool TryGetRelevantType(ApiBase context, string namespaceName, string name, out Type relevantType) + public bool TryGetRelevantType(ModelContext context, string namespaceName, string name, out Type relevantType) { relevantType = typeof(Product); return true; diff --git a/test/Microsoft.Restier.TestCommon/PublicApi.bsl b/test/Microsoft.Restier.TestCommon/PublicApi.bsl index 3aeb1638..5fadb9b2 100644 --- a/test/Microsoft.Restier.TestCommon/PublicApi.bsl +++ b/test/Microsoft.Restier.TestCommon/PublicApi.bsl @@ -212,8 +212,8 @@ public interface Microsoft.Restier.Core.Model.IModelBuilder { } public interface Microsoft.Restier.Core.Model.IModelMapper { - bool TryGetRelevantType (Microsoft.Restier.Core.ApiBase apiBase, string name, out System.Type& relevantType) - bool TryGetRelevantType (Microsoft.Restier.Core.ApiBase apiBase, string namespaceName, string name, out System.Type& relevantType) + bool TryGetRelevantType (Microsoft.Restier.Core.Model.ModelContext context, string name, out System.Type& relevantType) + bool TryGetRelevantType (Microsoft.Restier.Core.Model.ModelContext context, string namespaceName, string name, out System.Type& relevantType) } public class Microsoft.Restier.Core.Model.ModelContext : Microsoft.Restier.Core.InvocationContext {