Skip to content

Commit d4206e2

Browse files
maca88hazzik
andauthored
Reduce cast usage for aggregate functions (#2036)
Co-authored-by: Alexander Zaytsev <alexzaytsev2019@gmail.com>
1 parent b9dc310 commit d4206e2

File tree

21 files changed

+2385
-44
lines changed

21 files changed

+2385
-44
lines changed

src/NHibernate.DomainModel/FooComponent.cs

+2
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ public Int32 Count
9292
set { _count = value; }
9393
}
9494

95+
public int NotMapped { get; set; }
96+
9597
public DateTime[] ImportantDates
9698
{
9799
get { return _importantDates; }

src/NHibernate.DomainModel/NHSpecific/NullableInt32.cs

+90-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace NHibernate.DomainModel.NHSpecific
77
/// A nullable type that wraps an <see cref="Int32"/> value.
88
/// </summary>
99
[TypeConverter(typeof(NullableInt32Converter)), Serializable()]
10-
public struct NullableInt32 : IFormattable, IComparable
10+
public struct NullableInt32 : IFormattable, IComparable, IConvertible
1111
{
1212
public static readonly NullableInt32 Default = new NullableInt32();
1313

@@ -234,5 +234,94 @@ public static NullableInt32 Parse(string s)
234234
// TODO: implement the rest of the Parse overloads found in Int32
235235

236236
#endregion
237+
238+
#region IConvertible
239+
240+
public TypeCode GetTypeCode()
241+
{
242+
return _value.GetTypeCode();
243+
}
244+
245+
public bool ToBoolean(IFormatProvider provider)
246+
{
247+
return ((IConvertible) _value).ToBoolean(provider);
248+
}
249+
250+
public char ToChar(IFormatProvider provider)
251+
{
252+
return ((IConvertible) _value).ToChar(provider);
253+
}
254+
255+
public sbyte ToSByte(IFormatProvider provider)
256+
{
257+
return ((IConvertible) _value).ToSByte(provider);
258+
}
259+
260+
public byte ToByte(IFormatProvider provider)
261+
{
262+
return ((IConvertible) _value).ToByte(provider);
263+
}
264+
265+
public short ToInt16(IFormatProvider provider)
266+
{
267+
return ((IConvertible) _value).ToInt16(provider);
268+
}
269+
270+
public ushort ToUInt16(IFormatProvider provider)
271+
{
272+
return ((IConvertible) _value).ToUInt16(provider);
273+
}
274+
275+
public int ToInt32(IFormatProvider provider)
276+
{
277+
return ((IConvertible) _value).ToInt32(provider);
278+
}
279+
280+
public uint ToUInt32(IFormatProvider provider)
281+
{
282+
return ((IConvertible) _value).ToUInt32(provider);
283+
}
284+
285+
public long ToInt64(IFormatProvider provider)
286+
{
287+
return ((IConvertible) _value).ToInt64(provider);
288+
}
289+
290+
public ulong ToUInt64(IFormatProvider provider)
291+
{
292+
return ((IConvertible) _value).ToUInt64(provider);
293+
}
294+
295+
public float ToSingle(IFormatProvider provider)
296+
{
297+
return ((IConvertible) _value).ToSingle(provider);
298+
}
299+
300+
public double ToDouble(IFormatProvider provider)
301+
{
302+
return ((IConvertible) _value).ToDouble(provider);
303+
}
304+
305+
public decimal ToDecimal(IFormatProvider provider)
306+
{
307+
return ((IConvertible) _value).ToDecimal(provider);
308+
}
309+
310+
public DateTime ToDateTime(IFormatProvider provider)
311+
{
312+
return ((IConvertible) _value).ToDateTime(provider);
313+
}
314+
315+
public string ToString(IFormatProvider provider)
316+
{
317+
return _value.ToString(provider);
318+
}
319+
320+
public object ToType(System.Type conversionType, IFormatProvider provider)
321+
{
322+
return ((IConvertible) _value).ToType(conversionType, provider);
323+
}
324+
325+
#endregion
237326
}
238327
}

src/NHibernate.DomainModel/Northwind/Entities/Address.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ public string Fax
6161
get { return _fax; }
6262
}
6363

64+
public int NotMapped => 1;
65+
6466
public static bool operator ==(Address address1, Address address2)
6567
{
6668
if (!ReferenceEquals(address1, null) &&
@@ -114,4 +116,4 @@ public override int GetHashCode()
114116
(_fax ?? string.Empty).GetHashCode();
115117
}
116118
}
117-
}
119+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace NHibernate.DomainModel.Northwind.Entities
6+
{
7+
public interface IEntity<TId>
8+
{
9+
TId Id { get; set; }
10+
}
11+
12+
public interface IEntity : IEntity<int>
13+
{
14+
}
15+
}

src/NHibernate.DomainModel/Northwind/Entities/Product.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,11 @@ public virtual float ShippingWeight
103103
set { _shippingWeight = value; }
104104
}
105105

106+
public virtual int NotMapped => 1;
107+
106108
public virtual ReadOnlyCollection<OrderLine> OrderLines
107109
{
108110
get { return new ReadOnlyCollection<OrderLine>(_orderLines); }
109111
}
110112
}
111-
}
113+
}

src/NHibernate.DomainModel/Northwind/Entities/User.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public interface IUser
2828
EnumStoredAsInt32 Enum2 { get; set; }
2929
}
3030

31-
public class User : IUser
31+
public class User : IUser, IEntity
3232
{
3333
public virtual int Id { get; set; }
3434

@@ -50,6 +50,10 @@ public class User : IUser
5050

5151
public virtual EnumStoredAsInt32 Enum2 { get; set; }
5252

53+
public virtual int NotMapped { get; set; }
54+
55+
public virtual Role NotMappedRole { get; set; }
56+
5357
public User() { }
5458

5559
public User(string name, DateTime registeredAt)

src/NHibernate.Test/Async/Linq/SelectionTests.cs

+23
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
using System;
1212
using System.Collections.Generic;
1313
using System.Linq;
14+
using NHibernate.DomainModel.NHSpecific;
1415
using NHibernate.DomainModel.Northwind.Entities;
16+
using NHibernate.Type;
1517
using NUnit.Framework;
1618
using NHibernate.Linq;
1719

@@ -307,6 +309,10 @@ public async Task CanProjectWithCastAsync()
307309

308310
var names5 = await (db.Users.Select(p => new { p1 = (p as IUser).Name }).ToListAsync());
309311
Assert.AreEqual(3, names5.Count);
312+
313+
var names6 = await (db.Users.Select(p => new { p1 = (long) p.Id }).ToListAsync());
314+
Assert.AreEqual(3, names6.Count);
315+
310316
// ReSharper restore RedundantCast
311317
}
312318

@@ -453,6 +459,23 @@ public async Task CanSelectConditionalObjectAsync()
453459
Assert.That(fatherIsKnown, Has.Exactly(1).With.Property("FatherIsKnown").True);
454460
}
455461

462+
[Test]
463+
public async Task CanCastToDerivedTypeAsync()
464+
{
465+
var dogs = await (db.Animals
466+
.Where(a => ((Dog) a).Pregnant)
467+
.Select(a => new {a.SerialNumber})
468+
.ToListAsync());
469+
Assert.That(dogs, Has.Exactly(1).With.Property("SerialNumber").Not.Null);
470+
}
471+
472+
[Test]
473+
public async Task CanCastToCustomRegisteredTypeAsync()
474+
{
475+
TypeFactory.RegisterType(typeof(NullableInt32), new NullableInt32Type(), Enumerable.Empty<string>());
476+
Assert.That(await (db.Users.Where(o => (NullableInt32) o.Id == 1).ToListAsync()), Has.Count.EqualTo(1));
477+
}
478+
456479
public class Wrapper<T>
457480
{
458481
public T item;

0 commit comments

Comments
 (0)