Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restructure AggregationBinder #1378

Open
wants to merge 1 commit into
base: release-8.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ public static IContainerBuilder AddDefaultWebApiServices(this IContainerBuilder
builder.AddService<IFilterBinder, FilterBinder>(ServiceLifetime.Singleton);
builder.AddService<IOrderByBinder, OrderByBinder>(ServiceLifetime.Singleton);
builder.AddService<ISelectExpandBinder, SelectExpandBinder>(ServiceLifetime.Singleton);
builder.AddService<IAggregationBinder, AggregationBinder>(ServiceLifetime.Singleton);

// HttpRequestScope.
builder.AddService<HttpRequestScope>(ServiceLifetime.Scoped);
Expand Down
95 changes: 95 additions & 0 deletions src/Microsoft.AspNetCore.OData/Common/TypeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Contracts;
using System.Linq;
Expand Down Expand Up @@ -42,6 +43,100 @@ public static bool IsDynamicTypeWrapper(this Type type)

public static bool IsComputeWrapper(this Type type, out Type entityType) => IsTypeWrapper(typeof(ComputeWrapper<>), type, out entityType);

public static bool IsFlatteningWrapper(this Type type)
{
if (type == null)
{
return false;
}

if (type.IsGenericType)
{
Type genericTypeDefinition = type.GetGenericTypeDefinition();

// Default implementation
if (genericTypeDefinition == typeof(FlatteningWrapper<>))
{
Debug.Assert(
typeof(DynamicTypeWrapper).IsAssignableFrom(genericTypeDefinition)
&& genericTypeDefinition.GetInterfaces().Any(d => d.IsGenericType && d.GetGenericTypeDefinition() == typeof(IFlatteningWrapper<>))
&& genericTypeDefinition.GetInterfaces().Any(d => d.IsGenericType && d.GetGenericTypeDefinition() == typeof(IGroupByWrapper<>)),
"FlatteningWrapper<T> must inherit from DynamicTypeWrapper and implement IFlatteningWrapper<T> and IGroupByWrapper<T>");
return true;
}

// FlatteningWrapper<T> must inherit from DynamicTypeWrapper
if (!typeof(DynamicTypeWrapper).IsAssignableFrom(genericTypeDefinition))
{
return false;
}

// Custom implementation
Type[] genericTypeInterfaces = genericTypeDefinition.GetInterfaces();
bool typeImplementsIFlatteningWrapper = false;
bool typeImplementsIGroupByWrapper = false;
for (int i = 0; i < genericTypeInterfaces.Length; i++)
{
Type genericTypeInterface = genericTypeInterfaces[i];
// FlatteningWrapper<T> must implement IFlatteningWrapper<T> and IGroupByWrapper<T>
if (genericTypeInterface.IsGenericType && genericTypeInterface.GetGenericTypeDefinition() == typeof(IFlatteningWrapper<>))
{
typeImplementsIFlatteningWrapper = true;
}

if (genericTypeInterface.IsGenericType && genericTypeInterface.GetGenericTypeDefinition() == typeof(IGroupByWrapper<>))
{
typeImplementsIGroupByWrapper = true;
}

if (typeImplementsIFlatteningWrapper && typeImplementsIGroupByWrapper)
{
return true;
}
}
}

return false;
}

public static bool IsGroupByWrapper(this Type type)
{
if (type == null || type.IsValueType || type == typeof(string))
{
return false;
}

// Default implementation
if (typeof(GroupByWrapper).IsAssignableFrom(type))
{
Debug.Assert(
typeof(DynamicTypeWrapper).IsAssignableFrom(type)
&& type.GetInterfaces().Any(d => d.IsGenericType && d.GetGenericTypeDefinition() == typeof(IGroupByWrapper<>)),
"GroupByWrapper must inherit from DynamicTypeWrapper and implement IGroupByWrapper<T>");
return true;
}

// GroupByWrapper must inherit from DynamicTypeWrapper
if (!typeof(DynamicTypeWrapper).IsAssignableFrom(type))
{
return false;
}

// Custom implementation
Type[] typeInterfaces = type.GetInterfaces();
for (int i = 0; i < typeInterfaces.Length; i++)
{
Type typeInterface = typeInterfaces[i];
// GroupByWrapper must implement IGroupByWrapper<T>
if (typeInterface.IsGenericType && typeInterface.GetGenericTypeDefinition() == typeof(IGroupByWrapper<>))
{
return true;
}
}

return false;
}

private static bool IsTypeWrapper(Type wrappedType, Type type, out Type entityType)
{
if (type == null)
Expand Down
Loading