Skip to content

Commit 210746a

Browse files
AnonType: more polishing (#265)
1 parent 9c6351f commit 210746a

File tree

9 files changed

+57
-57
lines changed

9 files changed

+57
-57
lines changed

net/DevExtreme.AspNet.Data.Tests/AnonTypeTests.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using DevExtreme.AspNet.Data.Helpers;
2-
using DevExtreme.AspNet.Data.Types;
1+
using DevExtreme.AspNet.Data.Types;
32
using System;
43
using System.Collections.Generic;
54
using System.Linq;
@@ -62,8 +61,8 @@ public void GetHashCode_Null() {
6261
[Fact]
6362
public void Accessor() {
6463
var o = new AnonType<int, int>(42, 84);
65-
Assert.Equal(42, Accessors.AnonType.Read(o, "I0"));
66-
Assert.Equal(84, Accessors.AnonType.Read(o, "I1"));
64+
Assert.Equal(42, AnonTypeAccessor.Instance.Read(o, "I0"));
65+
Assert.Equal(84, AnonTypeAccessor.Instance.Read(o, "I1"));
6766
}
6867

6968
}

net/DevExtreme.AspNet.Data/Helpers/Accessors.cs

Lines changed: 0 additions & 18 deletions
This file was deleted.

net/DevExtreme.AspNet.Data/RemoteGrouping/RemoteAvgAggregator.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@ public RemoteAvgAggregator(IAccessor<T> accessor)
1616
}
1717

1818
public override void Step(T container, string selector) {
19-
var itemIndex = Int32.Parse(selector);
20-
21-
_countAggregator.Step(container, AnonType.ITEM_PREFIX + (itemIndex + 1));
22-
_valueAggregator.Step(container, AnonType.ITEM_PREFIX + itemIndex);
19+
_countAggregator.Step(container, AnonType.IndexToField(1 + AnonType.FieldToIndex(selector)));
20+
_valueAggregator.Step(container, selector);
2321
}
2422

2523
public override object Finish() {

net/DevExtreme.AspNet.Data/RemoteGrouping/RemoteGroupExpressionCompiler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public Expression Compile(Expression target) {
5353
var orderParam = Expression.Parameter(groupingType, "g");
5454
var orderAccessor = Expression.Field(
5555
Expression.Property(orderParam, "Key"),
56-
AnonType.ITEM_PREFIX + i
56+
AnonType.IndexToField(i)
5757
);
5858

5959
target = Expression.Call(
@@ -76,7 +76,7 @@ Expression MakeAggregatingProjection(Expression target, Type groupingType, int g
7676
};
7777

7878
for(var i = 0; i < groupCount; i++)
79-
projectionExprList.Add(Expression.Field(Expression.Property(param, "Key"), AnonType.ITEM_PREFIX + i));
79+
projectionExprList.Add(Expression.Field(Expression.Property(param, "Key"), AnonType.IndexToField(i)));
8080

8181
projectionExprList.AddRange(MakeAggregates(param, _totalSummary));
8282

net/DevExtreme.AspNet.Data/RemoteGrouping/RemoteGroupTransformer.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using DevExtreme.AspNet.Data.Aggregation;
2-
using DevExtreme.AspNet.Data.Helpers;
32
using DevExtreme.AspNet.Data.ResponseModel;
43
using DevExtreme.AspNet.Data.Types;
54
using System;
@@ -16,9 +15,9 @@ public static RemoteGroupingResult Run(IEnumerable<AnonType> flatGroups, int gro
1615
List<Group> hierGroups = null;
1716

1817
if(groupCount > 0) {
19-
hierGroups = new GroupHelper<AnonType>(Accessors.AnonType).Group(
18+
hierGroups = new GroupHelper<AnonType>(AnonTypeAccessor.Instance).Group(
2019
flatGroups,
21-
Enumerable.Range(0, groupCount).Select(i => new GroupingInfo { Selector = AnonType.ITEM_PREFIX + (1 + i) }).ToArray()
20+
Enumerable.Range(0, groupCount).Select(i => new GroupingInfo { Selector = AnonType.IndexToField(1 + i) }).ToArray()
2221
);
2322
}
2423

@@ -32,7 +31,7 @@ public static RemoteGroupingResult Run(IEnumerable<AnonType> flatGroups, int gro
3231

3332
transformedTotalSummary.Add(new SummaryInfo { SummaryType = AggregateName.REMOTE_COUNT });
3433

35-
var totals = new AggregateCalculator<AnonType>(dataToAggregate, Accessors.AnonType, transformedTotalSummary, transformedGroupSummary).Run();
34+
var totals = new AggregateCalculator<AnonType>(dataToAggregate, AnonTypeAccessor.Instance, transformedTotalSummary, transformedGroupSummary).Run();
3635
var totalCount = (int)totals.Last();
3736

3837
totals = totals.Take(totals.Length - 1).ToArray();
@@ -61,13 +60,13 @@ static List<SummaryInfo> TransformSummary(SummaryInfo[] original, ref int fieldI
6160
} else if(originalType == AggregateName.AVG) {
6261
result.Add(new SummaryInfo {
6362
SummaryType = AggregateName.REMOTE_AVG,
64-
Selector = fieldIndex.ToString()
63+
Selector = AnonType.IndexToField(fieldIndex)
6564
});
6665
fieldIndex += 2;
6766
} else {
6867
result.Add(new SummaryInfo {
6968
SummaryType = originalType,
70-
Selector = AnonType.ITEM_PREFIX + fieldIndex
69+
Selector = AnonType.IndexToField(fieldIndex)
7170
});
7271
fieldIndex++;
7372
}

net/DevExtreme.AspNet.Data/Types/AnonType.Generated.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ public AnonType(T0 i0) {
1010
I0 = i0;
1111
}
1212

13-
public override int Size {
13+
protected override int Size {
1414
get { return 1; }
1515
}
1616

1717
public override object this[int index] {
1818
get {
1919
if(index == 0) return I0;
20-
throw new ArgumentOutOfRangeException();
20+
throw new IndexOutOfRangeException();
2121
}
2222
}
2323

@@ -32,15 +32,15 @@ public AnonType(T0 i0, T1 i1) {
3232
I1 = i1;
3333
}
3434

35-
public override int Size {
35+
protected override int Size {
3636
get { return 2; }
3737
}
3838

3939
public override object this[int index] {
4040
get {
4141
if(index == 0) return I0;
4242
if(index == 1) return I1;
43-
throw new ArgumentOutOfRangeException();
43+
throw new IndexOutOfRangeException();
4444
}
4545
}
4646

@@ -63,7 +63,7 @@ public AnonType(T0 i0, T1 i1, T2 i2, T3 i3) {
6363
I3 = i3;
6464
}
6565

66-
public override int Size {
66+
protected override int Size {
6767
get { return 4; }
6868
}
6969

@@ -73,7 +73,7 @@ public override object this[int index] {
7373
if(index == 1) return I1;
7474
if(index == 2) return I2;
7575
if(index == 3) return I3;
76-
throw new ArgumentOutOfRangeException();
76+
throw new IndexOutOfRangeException();
7777
}
7878
}
7979

@@ -112,7 +112,7 @@ public AnonType(T0 i0, T1 i1, T2 i2, T3 i3, T4 i4, T5 i5, T6 i6, T7 i7) {
112112
I7 = i7;
113113
}
114114

115-
public override int Size {
115+
protected override int Size {
116116
get { return 8; }
117117
}
118118

@@ -126,7 +126,7 @@ public override object this[int index] {
126126
if(index == 5) return I5;
127127
if(index == 6) return I6;
128128
if(index == 7) return I7;
129-
throw new ArgumentOutOfRangeException();
129+
throw new IndexOutOfRangeException();
130130
}
131131
}
132132

@@ -197,7 +197,7 @@ public AnonType(T0 i0, T1 i1, T2 i2, T3 i3, T4 i4, T5 i5, T6 i6, T7 i7, T8 i8, T
197197
I15 = i15;
198198
}
199199

200-
public override int Size {
200+
protected override int Size {
201201
get { return 16; }
202202
}
203203

@@ -219,7 +219,7 @@ public override object this[int index] {
219219
if(index == 13) return I13;
220220
if(index == 14) return I14;
221221
if(index == 15) return I15;
222-
throw new ArgumentOutOfRangeException();
222+
throw new IndexOutOfRangeException();
223223
}
224224
}
225225

@@ -354,7 +354,7 @@ public AnonType(T0 i0, T1 i1, T2 i2, T3 i3, T4 i4, T5 i5, T6 i6, T7 i7, T8 i8, T
354354
I31 = i31;
355355
}
356356

357-
public override int Size {
357+
protected override int Size {
358358
get { return 32; }
359359
}
360360

@@ -392,7 +392,7 @@ public override object this[int index] {
392392
if(index == 29) return I29;
393393
if(index == 30) return I30;
394394
if(index == 31) return I31;
395-
throw new ArgumentOutOfRangeException();
395+
throw new IndexOutOfRangeException();
396396
}
397397
}
398398

net/DevExtreme.AspNet.Data/Types/AnonType.Generated.tt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ namespace DevExtreme.AspNet.Data.Types {
2525
<# } #>
2626
}
2727

28-
public override int Size {
28+
protected override int Size {
2929
get { return <#= size #>; }
3030
}
3131

@@ -34,7 +34,7 @@ namespace DevExtreme.AspNet.Data.Types {
3434
<# for(var i = 0; i < size; i++) { #>
3535
if(index == <#= i #>) return I<#= i #>;
3636
<# } #>
37-
throw new ArgumentOutOfRangeException();
37+
throw new IndexOutOfRangeException();
3838
}
3939
}
4040

net/DevExtreme.AspNet.Data/Types/AnonType.cs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,12 @@
88
namespace DevExtreme.AspNet.Data.Types {
99

1010
partial class AnonType {
11-
public const string ITEM_PREFIX = "I";
12-
13-
public virtual int Size {
11+
protected virtual int Size {
1412
get { return 0; }
1513
}
1614

1715
public virtual object this[int index] {
18-
get { throw new ArgumentOutOfRangeException(); }
16+
get { throw new IndexOutOfRangeException(); }
1917
}
2018

2119
public override bool Equals(object obj) {
@@ -38,10 +36,8 @@ public override int GetHashCode() {
3836
// http://stackoverflow.com/a/1646913
3937
unchecked {
4038
var result = 17;
41-
for(var i = 0; i < Size; i++) {
42-
var item = this[i];
43-
result = result * 31 + (item == null ? 0 : item.GetHashCode());
44-
}
39+
for(var i = 0; i < Size; i++)
40+
result = result * 31 + EqualityComparer<object>.Default.GetHashCode(this[i]);
4541
return result;
4642
}
4743
}
@@ -71,10 +67,18 @@ public static NewExpression CreateNewExpression(IEnumerable<Expression> expressi
7167
return Expression.New(
7268
type.GetConstructor(typeArguments),
7369
expressions,
74-
Enumerable.Range(0, typeArguments.Length).Select(i => type.GetField(ITEM_PREFIX + i))
70+
Enumerable.Range(0, typeArguments.Length).Select(i => type.GetField(IndexToField(i)))
7571
);
7672
}
7773

74+
public static string IndexToField(int index) {
75+
return "I" + index;
76+
}
77+
78+
public static int FieldToIndex(string field) {
79+
return int.Parse(field.Substring(1));
80+
}
81+
7882
}
7983

8084
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using DevExtreme.AspNet.Data.Helpers;
2+
using System;
3+
using System.Linq;
4+
5+
namespace DevExtreme.AspNet.Data.Types {
6+
7+
class AnonTypeAccessor : IAccessor<AnonType> {
8+
public static readonly AnonTypeAccessor Instance = new AnonTypeAccessor();
9+
10+
private AnonTypeAccessor() {
11+
}
12+
13+
public object Read(AnonType container, string selector) {
14+
return container[AnonType.FieldToIndex(selector)];
15+
}
16+
}
17+
18+
}

0 commit comments

Comments
 (0)