Skip to content

Commit ddb1c83

Browse files
KenitoIncgathogojr
authored andcommitted
Restructure AggregationBinder
1 parent 8547df7 commit ddb1c83

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+4878
-569
lines changed

src/Microsoft.AspNetCore.OData/Abstracts/ContainerBuilderExtensions.cs

+1
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ public static IContainerBuilder AddDefaultWebApiServices(this IContainerBuilder
110110
builder.AddService<IFilterBinder, FilterBinder>(ServiceLifetime.Singleton);
111111
builder.AddService<IOrderByBinder, OrderByBinder>(ServiceLifetime.Singleton);
112112
builder.AddService<ISelectExpandBinder, SelectExpandBinder>(ServiceLifetime.Singleton);
113+
builder.AddService<IAggregationBinder, AggregationBinder>(ServiceLifetime.Singleton);
113114

114115
// HttpRequestScope.
115116
builder.AddService<HttpRequestScope>(ServiceLifetime.Scoped);

src/Microsoft.AspNetCore.OData/Common/TypeHelper.cs

+95
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System;
99
using System.Collections;
1010
using System.Collections.Generic;
11+
using System.Diagnostics;
1112
using System.Diagnostics.CodeAnalysis;
1213
using System.Diagnostics.Contracts;
1314
using System.Linq;
@@ -42,6 +43,100 @@ public static bool IsDynamicTypeWrapper(this Type type)
4243

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

46+
public static bool IsFlatteningWrapper(this Type type)
47+
{
48+
if (type == null)
49+
{
50+
return false;
51+
}
52+
53+
if (type.IsGenericType)
54+
{
55+
Type genericTypeDefinition = type.GetGenericTypeDefinition();
56+
57+
// Default implementation
58+
if (genericTypeDefinition == typeof(FlatteningWrapper<>))
59+
{
60+
Debug.Assert(
61+
typeof(DynamicTypeWrapper).IsAssignableFrom(genericTypeDefinition)
62+
&& genericTypeDefinition.GetInterfaces().Any(d => d.IsGenericType && d.GetGenericTypeDefinition() == typeof(IFlatteningWrapper<>))
63+
&& genericTypeDefinition.GetInterfaces().Any(d => d.IsGenericType && d.GetGenericTypeDefinition() == typeof(IGroupByWrapper<>)),
64+
"FlatteningWrapper<T> must inherit from DynamicTypeWrapper and implement IFlatteningWrapper<T> and IGroupByWrapper<T>");
65+
return true;
66+
}
67+
68+
// FlatteningWrapper<T> must inherit from DynamicTypeWrapper
69+
if (!typeof(DynamicTypeWrapper).IsAssignableFrom(genericTypeDefinition))
70+
{
71+
return false;
72+
}
73+
74+
// Custom implementation
75+
Type[] genericTypeInterfaces = genericTypeDefinition.GetInterfaces();
76+
bool typeImplementsIFlatteningWrapper = false;
77+
bool typeImplementsIGroupByWrapper = false;
78+
for (int i = 0; i < genericTypeInterfaces.Length; i++)
79+
{
80+
Type genericTypeInterface = genericTypeInterfaces[i];
81+
// FlatteningWrapper<T> must implement IFlatteningWrapper<T> and IGroupByWrapper<T>
82+
if (genericTypeInterface.IsGenericType && genericTypeInterface.GetGenericTypeDefinition() == typeof(IFlatteningWrapper<>))
83+
{
84+
typeImplementsIFlatteningWrapper = true;
85+
}
86+
87+
if (genericTypeInterface.IsGenericType && genericTypeInterface.GetGenericTypeDefinition() == typeof(IGroupByWrapper<>))
88+
{
89+
typeImplementsIGroupByWrapper = true;
90+
}
91+
92+
if (typeImplementsIFlatteningWrapper && typeImplementsIGroupByWrapper)
93+
{
94+
return true;
95+
}
96+
}
97+
}
98+
99+
return false;
100+
}
101+
102+
public static bool IsGroupByWrapper(this Type type)
103+
{
104+
if (type == null || type.IsValueType || type == typeof(string))
105+
{
106+
return false;
107+
}
108+
109+
// Default implementation
110+
if (typeof(GroupByWrapper).IsAssignableFrom(type))
111+
{
112+
Debug.Assert(
113+
typeof(DynamicTypeWrapper).IsAssignableFrom(type)
114+
&& type.GetInterfaces().Any(d => d.IsGenericType && d.GetGenericTypeDefinition() == typeof(IGroupByWrapper<>)),
115+
"GroupByWrapper must inherit from DynamicTypeWrapper and implement IGroupByWrapper<T>");
116+
return true;
117+
}
118+
119+
// GroupByWrapper must inherit from DynamicTypeWrapper
120+
if (!typeof(DynamicTypeWrapper).IsAssignableFrom(type))
121+
{
122+
return false;
123+
}
124+
125+
// Custom implementation
126+
Type[] typeInterfaces = type.GetInterfaces();
127+
for (int i = 0; i < typeInterfaces.Length; i++)
128+
{
129+
Type typeInterface = typeInterfaces[i];
130+
// GroupByWrapper must implement IGroupByWrapper<T>
131+
if (typeInterface.IsGenericType && typeInterface.GetGenericTypeDefinition() == typeof(IGroupByWrapper<>))
132+
{
133+
return true;
134+
}
135+
}
136+
137+
return false;
138+
}
139+
45140
private static bool IsTypeWrapper(Type wrappedType, Type type, out Type entityType)
46141
{
47142
if (type == null)

0 commit comments

Comments
 (0)