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 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 @@ -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 @@ -260,6 +261,39 @@ 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())
Expand Down Expand Up @@ -299,7 +333,7 @@ public async Task SerializationAsync()
{
var transformer = Transformers.AliasToBean<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)
Expand Down
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 @@ -248,6 +249,99 @@ 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) Transformers.AliasToBean<TestDto>().TransformTuple(tuple, aliases);
var actualStruct = (TestDtoAsStruct) Transformers.AliasToBean<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>(() => Transformers.AliasToBean<TestDto>().TransformTuple(tuple, aliases));
Assert.That(ex, Has.Message.Contains(nameof(o.IntProp)));
var ex2 = Assert.Throws<System.InvalidCastException>(() => Transformers.AliasToBean<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(() => Transformers.AliasToBean<NoDefCtorDto>().TransformTuple(new object[0], new string[0]), Throws.ArgumentException);
}

private void AssertCardinalityNameAndId<T>(IResultTransformer transformer = null)
{
using (var s = OpenSession())
Expand Down Expand Up @@ -285,7 +379,7 @@ private void AssertSerialization<T>()
{
var transformer = Transformers.AliasToBean<T>();
var bytes = SerializationHelper.Serialize(transformer);
transformer = (IResultTransformer)SerializationHelper.Deserialize(bytes);
transformer = (IResultTransformer) SerializationHelper.Deserialize(bytes);
AssertCardinalityNameAndId<T>(transformer: transformer);
}
}
Expand Down
Loading