Skip to content

Commit

Permalink
Address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
chinadragon0515 committed Aug 30, 2016
1 parent 0751716 commit 0e070dc
Show file tree
Hide file tree
Showing 14 changed files with 46 additions and 45 deletions.
5 changes: 3 additions & 2 deletions src/Microsoft.Restier.Core/ApiBaseExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -524,13 +524,14 @@ private static Type EnsureElementType(
var mapper = api.GetApiService<IModelMapper>();
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);
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/Microsoft.Restier.Core/Model/IModelMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public interface IModelMapper
/// Tries to get the relevant type of an entity
/// set, singleton, or composable function import.
/// </summary>
/// <param name="apiBase">
/// An API.
/// <param name="context">
/// The context for model mapper.
/// </param>
/// <param name="name">
/// The name of an entity set, singleton or composable function import.
Expand Down Expand Up @@ -48,15 +48,15 @@ public interface IModelMapper
/// </para>
/// </remarks>
bool TryGetRelevantType(
ApiBase apiBase,
ModelContext context,
string name,
out Type relevantType);

/// <summary>
/// Tries to get the relevant type of a composable function.
/// </summary>
/// <param name="apiBase">
/// An API.
/// <param name="context">
/// The context for model mapper.
/// </param>
/// <param name="namespaceName">
/// The name of a namespace containing a composable function.
Expand Down Expand Up @@ -85,7 +85,7 @@ bool TryGetRelevantType(
/// </para>
/// </remarks>
bool TryGetRelevantType(
ApiBase apiBase,
ModelContext context,
string namespaceName,
string name,
out Type relevantType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public ModelMapper(Type dbContextType)
/// Tries to get the relevant type of an entity
/// set, singleton, or composable function import.
/// </summary>
/// <param name="apiBase">
/// An API.
/// <param name="context">
/// The context for model mapper.
/// </param>
/// <param name="name">
/// The name of an entity set, singleton or composable function import.
Expand All @@ -50,7 +50,7 @@ public ModelMapper(Type dbContextType)
/// provided; otherwise, <c>false</c>.
/// </returns>
public bool TryGetRelevantType(
ApiBase apiBase,
ModelContext context,
string name,
out Type relevantType)
{
Expand All @@ -77,8 +77,8 @@ public bool TryGetRelevantType(
/// <summary>
/// Tries to get the relevant type of a composable function.
/// </summary>
/// <param name="apiBase">
/// An API.
/// <param name="context">
/// The context for model mapper.
/// </param>
/// <param name="namespaceName">
/// The name of a namespace containing a composable function.
Expand All @@ -95,7 +95,7 @@ public bool TryGetRelevantType(
/// provided; otherwise, <c>false</c>.
/// </returns>
public bool TryGetRelevantType(
ApiBase apiBase,
ModelContext context,
string namespaceName,
string name,
out Type relevantType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public Task<IEdmModel> GetModelAsync(ModelContext context, CancellationToken can
Ensure.NotNull(context, "context");

#if EF7
var dbContext = context.ServiceProvider.GetService<DbContext>();
var dbContext = context.GetApiService<DbContext>();
context.ResourceSetTypeMap = dbContext.GetType().GetProperties()
.Where(e => e.PropertyType.FindGenericType(typeof(DbSet<>)) != null)
.ToDictionary(e => e.Name, e => e.PropertyType.GetGenericArguments()[0]);
Expand All @@ -59,7 +59,7 @@ public Task<IEdmModel> GetModelAsync(ModelContext context, CancellationToken can
#else
var resourceSetTypeMap = new Dictionary<string, Type>();
var resourceTypeKeyPropertiesMap = new Dictionary<Type, ICollection<PropertyInfo>>();
var dbContext = context.ServiceProvider.GetService<DbContext>();
var dbContext = context.GetApiService<DbContext>();

var efModel = (dbContext as IObjectContextAdapter).ObjectContext.MetadataWorkspace;
var efEntityContainer = efModel.GetItems<EntityContainer>(DataSpace.CSpace).Single();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public async Task InitializeAsync(
SubmitContext context,
CancellationToken cancellationToken)
{
DbContext dbContext = context.ServiceProvider.GetService<DbContext>();
DbContext dbContext = context.GetApiService<DbContext>();

foreach (var entry in context.ChangeSet.Entries.OfType<DataModificationItem>())
{
Expand Down Expand Up @@ -145,7 +145,7 @@ private static async Task<object> FindResource(
DataModificationItem item,
CancellationToken cancellationToken)
{
var apiBase = context.ServiceProvider.GetService<ApiBase>();
var apiBase = context.GetApiService<ApiBase>();
IQueryable query = apiBase.GetQueryableSource(item.ResourceSetName);
query = item.ApplyTo(query);

Expand Down
16 changes: 8 additions & 8 deletions src/Microsoft.Restier.Publishers.OData/Model/ModelMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ internal class ModelMapper : IModelMapper
/// Tries to get the relevant type of an entity
/// set, singleton, or composable function import.
/// </summary>
/// <param name="api">
/// An API.
/// <param name="context">
/// The context for model mapper.
/// </param>
/// <param name="name">
/// The name of an entity set, singleton or composable function import.
Expand All @@ -36,12 +36,12 @@ internal class ModelMapper : IModelMapper
/// provided; otherwise, <c>false</c>.
/// </returns>
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<IEdmModel>();
var element = model.EntityContainer.Elements.Where(e => e.Name == name).FirstOrDefault();

if (element != null)
Expand Down Expand Up @@ -73,14 +73,14 @@ public bool TryGetRelevantType(
}
}

return InnerMapper.TryGetRelevantType(api, name, out relevantType);
return InnerMapper.TryGetRelevantType(context, name, out relevantType);
}

/// <summary>
/// Tries to get the relevant type of a composable function.
/// </summary>
/// <param name="api">
/// An API.
/// <param name="context">
/// The context for model mapper.
/// </param>
/// <param name="namespaceName">
/// The name of a namespace containing a composable function.
Expand All @@ -97,7 +97,7 @@ public bool TryGetRelevantType(
/// provided; otherwise, <c>false</c>.
/// </returns>
public bool TryGetRelevantType(
ApiBase api,
ModelContext context,
string namespaceName,
string name,
out Type relevantType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,12 +372,12 @@ public ModelMapper(RestierModelExtender modelCache)

/// <inheritdoc/>
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;
}
Expand All @@ -403,13 +403,13 @@ public bool TryGetRelevantType(

/// <inheritdoc/>
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public async Task<IQueryable> ExecuteOperationAsync(

var parameterArray = method.GetParameters();

var model = context.ServiceProvider.GetService<IEdmModel>();
var model = context.GetApiService<IEdmModel>();

// Parameters of method and model is exactly mapped or there is parsing error
var parameters = new object[parameterArray.Length];
Expand Down Expand Up @@ -195,7 +195,7 @@ private static async Task InvokeAuthorizers(

private static void PerformPreEvent(OperationContext context, CancellationToken cancellationToken)
{
var processor = context.ServiceProvider.GetService<IOperationFilter>();
var processor = context.GetApiService<IOperationFilter>();
if (processor != null)
{
processor.OnOperationExecutingAsync(context, cancellationToken);
Expand All @@ -204,7 +204,7 @@ private static void PerformPreEvent(OperationContext context, CancellationToken

private static void PerformPostEvent(OperationContext context, CancellationToken cancellationToken)
{
var processor = context.ServiceProvider.GetService<IOperationFilter>();
var processor = context.GetApiService<IOperationFilter>();
if (processor != null)
{
processor.OnOperationExecutedAsync(context, cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public async Task<QueryResult> ExecuteQueryAsync<TElement>(
IQueryable<TElement> query,
CancellationToken cancellationToken)
{
var countOption = context.ServiceProvider.GetService<RestierQueryExecutorOptions>();
var countOption = context.GetApiService<RestierQueryExecutorOptions>();
if (countOption.IncludeTotalCount)
{
var countQuery = ExpressionHelpers.GetCountableQuery(query);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ public static Task<ODataRoute> MapRestierRoute<TApi>(
ApiBase.AddPublisherServices(
typeof(TApi),
services =>
{
services.AddODataServices<TApi>();
});
{
services.AddODataServices<TApi>();
});

Func<IContainerBuilder> func = () => new RestierContainerBuilder(typeof(TApi));
config.UseCustomContainerBuilder(func);
Expand Down
4 changes: 2 additions & 2 deletions test/Microsoft.Restier.Core.Tests/Api.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ public Task<IEdmModel> GetModelAsync(ModelContext context, CancellationToken can
private class TestModelMapper : IModelMapper
{
public bool TryGetRelevantType(
ApiBase context,
ModelContext context,
string name, out Type relevantType)
{
relevantType = typeof(string);
return true;
}

public bool TryGetRelevantType(
ApiBase context,
ModelContext context,
string namespaceName, string name,
out Type relevantType)
{
Expand Down
4 changes: 2 additions & 2 deletions test/Microsoft.Restier.Publishers.OData.Test/FallbackTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions test/Microsoft.Restier.Publishers.OData.Test/StoreApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
{
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions test/Microsoft.Restier.TestCommon/PublicApi.bsl
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 0e070dc

Please sign in to comment.