Skip to content

Use compiled expression in AliasToBeanResultTransformer #2021

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

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 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 @@ -8,7 +8,8 @@
//------------------------------------------------------------------------------


using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using NHibernate.Transform;
using NHibernate.Util;
Expand Down Expand Up @@ -225,7 +226,7 @@ public async Task WorksWithManyCandidatesAsync()
{
using (var s = OpenSession())
{
var transformer = Transformers.AliasToBean<NewPropertiesSimpleDTO>();
var transformer = GetTransformer<NewPropertiesSimpleDTO>();
var l = await (s.CreateSQLQuery("select id as ID, Name as NamE from Simple")
.SetResultTransformer(transformer)
.ListAsync<NewPropertiesSimpleDTO>());
Expand All @@ -244,7 +245,7 @@ public void ToPropertiesInsensitivelyDuplicated_WithoutAnyProjectionsAsync()
{
using (var s = OpenSession())
{
var transformer = Transformers.AliasToBean<PropertiesInsensitivelyDuplicated>();
var transformer = GetTransformer<PropertiesInsensitivelyDuplicated>();
Assert.ThrowsAsync<AmbiguousMatchException>(() =>
{
return s.CreateSQLQuery("select * from Simple")
Expand All @@ -264,11 +265,44 @@ public async Task SerializationAsync()
await (AssertSerializationAsync<NewPropertiesSimpleDTO>());
}

enum TestEnum
{ Value0, Value1 }

class TestDto
{
private TestDto()
{ }

public TestDto(bool bogus) { }

public string StringProp { get; set; }
public int IntProp { get; set; }
public int IntPropNull { get; set; }
public int? IntPropNullNullable { get; set; }
public TestEnum EnumProp { get; set; }
}

struct TestDtoAsStruct
{
public string StringProp { get; set; }
public int IntProp { get; set; }
public int IntPropNull { get; set; }
public int? IntPropNullNullable { get; set; }
public TestEnum EnumProp { get; set; }
}

class NoDefCtorDto
{
public NoDefCtorDto(bool bogus)
{
}
}

private async Task AssertCardinalityNameAndIdAsync<T>(IResultTransformer transformer = null, CancellationToken cancellationToken = default(CancellationToken))
{
using (var s = OpenSession())
{
transformer = transformer ?? Transformers.AliasToBean<T>();
transformer = transformer ?? GetTransformer<T>();
var l = await (s.CreateSQLQuery("select * from Simple")
.SetResultTransformer(transformer)
.ListAsync<T>(cancellationToken));
Expand All @@ -285,7 +319,7 @@ public async Task SerializationAsync()
{
using (var s = OpenSession())
{
var transformer = Transformers.AliasToBean<T>();
var transformer = GetTransformer<T>();
var l = await (s.CreateSQLQuery(queryString)
.SetResultTransformer(transformer)
.ListAsync<T>(cancellationToken));
Expand All @@ -301,15 +335,20 @@ public async Task SerializationAsync()
{
try
{
var transformer = Transformers.AliasToBean<T>();
var transformer = GetTransformer<T>();
var bytes = SerializationHelper.Serialize(transformer);
transformer = (IResultTransformer)SerializationHelper.Deserialize(bytes);
transformer = (IResultTransformer) SerializationHelper.Deserialize(bytes);
return AssertCardinalityNameAndIdAsync<T>(transformer: transformer, cancellationToken: cancellationToken);
}
catch (System.Exception ex)
{
return Task.FromException<object>(ex);
}
}

protected IResultTransformer GetTransformer<T>()
{
return Transformers.AliasToBean<T>();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using NHibernate.Transform;
using NHibernate.Util;
Expand Down Expand Up @@ -213,7 +214,7 @@ public void WorksWithManyCandidates()
{
using (var s = OpenSession())
{
var transformer = Transformers.AliasToBean<NewPropertiesSimpleDTO>();
var transformer = GetTransformer<NewPropertiesSimpleDTO>();
var l = s.CreateSQLQuery("select id as ID, Name as NamE from Simple")
.SetResultTransformer(transformer)
.List<NewPropertiesSimpleDTO>();
Expand All @@ -232,7 +233,7 @@ public void ToPropertiesInsensitivelyDuplicated_WithoutAnyProjections()
{
using (var s = OpenSession())
{
var transformer = Transformers.AliasToBean<PropertiesInsensitivelyDuplicated>();
var transformer = GetTransformer<PropertiesInsensitivelyDuplicated>();
Assert.Throws<AmbiguousMatchException>(() =>
{
s.CreateSQLQuery("select * from Simple")
Expand All @@ -252,11 +253,104 @@ public void Serialization()
AssertSerialization<NewPropertiesSimpleDTO>();
}

enum TestEnum
{ Value0, Value1 }

class TestDto
{
private TestDto()
{ }

public TestDto(bool bogus) { }

public string StringProp { get; set; }
public int IntProp { get; set; }
public int IntPropNull { get; set; }
public int? IntPropNullNullable { get; set; }
public TestEnum EnumProp { get; set; }
}

struct TestDtoAsStruct
{
public string StringProp { get; set; }
public int IntProp { get; set; }
public int IntPropNull { get; set; }
public int? IntPropNullNullable { get; set; }
public TestEnum EnumProp { get; set; }
}

[Test]
public void TupleConversion()
{
var o = new TestDto(true)
{
IntProp = 1,
IntPropNull = 0,
StringProp = "hello",
IntPropNullNullable = null,
EnumProp = TestEnum.Value1,
};
string nullMarker = "NULL";
var testData = new Dictionary<string, object>
{
{nameof(o.IntProp), o.IntProp},
{nullMarker, decimal.MaxValue},
{nameof(o.IntPropNull).ToLowerInvariant(), null},
{string.Empty, new object()},
{nameof(o.IntPropNullNullable).ToLowerInvariant(), null},
{nameof(o.EnumProp), 1},
{nameof(o.StringProp), o.StringProp},
};
var aliases = testData.Keys.Select(k => k == nullMarker ? null : k).ToArray();

var tuple = testData.Values.ToArray();

var actual = (TestDto) GetTransformer<TestDto>().TransformTuple(tuple, aliases);
var actualStruct = (TestDtoAsStruct) GetTransformer<TestDtoAsStruct>().TransformTuple(tuple, aliases);
Assert.That(actual.IntProp, Is.EqualTo(o.IntProp));
Assert.That(actual.IntPropNull, Is.EqualTo(o.IntPropNull));
Assert.That(actual.StringProp, Is.EqualTo(o.StringProp));
Assert.That(actual.IntPropNullNullable, Is.EqualTo(o.IntPropNullNullable));
Assert.That(actual.EnumProp, Is.EqualTo(o.EnumProp));
}

[Test]
public void ThrowUserFriendlyException()
{
var o = new TestDto(true) { };

string nullMarker = "NULL";
var testData = new Dictionary<string, object>
{
{nameof(o.IntProp), "hello"},
};
var aliases = testData.Keys.Select(k => k == nullMarker ? null : k).ToArray();
var tuple = testData.Values.ToArray();

var ex = Assert.Throws<System.InvalidCastException>(() => GetTransformer<TestDto>().TransformTuple(tuple, aliases));
Assert.That(ex, Has.Message.Contains(nameof(o.IntProp)));
var ex2 = Assert.Throws<System.InvalidCastException>(() => GetTransformer<TestDtoAsStruct>().TransformTuple(tuple, aliases));
Assert.That(ex2, Has.Message.Contains(nameof(o.IntProp)));
}

class NoDefCtorDto
{
public NoDefCtorDto(bool bogus)
{
}
}

[Test]
public void ThrowsForClassWithoutDefaultCtor()
{
Assert.That(() => GetTransformer<NoDefCtorDto>().TransformTuple(new object[0], new string[0]), Throws.ArgumentException);
}

private void AssertCardinalityNameAndId<T>(IResultTransformer transformer = null)
{
using (var s = OpenSession())
{
transformer = transformer ?? Transformers.AliasToBean<T>();
transformer = transformer ?? GetTransformer<T>();
var l = s.CreateSQLQuery("select * from Simple")
.SetResultTransformer(transformer)
.List<T>();
Expand All @@ -273,7 +367,7 @@ private void AssertCardinalityAndSomething<T>(string queryString = "select s.Nam
{
using (var s = OpenSession())
{
var transformer = Transformers.AliasToBean<T>();
var transformer = GetTransformer<T>();
var l = s.CreateSQLQuery(queryString)
.SetResultTransformer(transformer)
.List<T>();
Expand All @@ -287,10 +381,15 @@ private void AssertCardinalityAndSomething<T>(string queryString = "select s.Nam

private void AssertSerialization<T>()
{
var transformer = Transformers.AliasToBean<T>();
var transformer = GetTransformer<T>();
var bytes = SerializationHelper.Serialize(transformer);
transformer = (IResultTransformer)SerializationHelper.Deserialize(bytes);
transformer = (IResultTransformer) SerializationHelper.Deserialize(bytes);
AssertCardinalityNameAndId<T>(transformer: transformer);
}

protected IResultTransformer GetTransformer<T>()
{
return Transformers.AliasToBean<T>();
}
}
}
Loading