Open
Description
Details
Currently Gridify throws ObjectNullReferenceException
if it tries to access a property from an intermediate null object. For instance, given the following mapper
public class TestClassGridifyMapper : GridifyMapper<TestClass>
{
public TestClassGridifyMapper()
{
GenerateMappings()
.AddMap("ChildName", e => e.ChildClass.Name);
}
}
The member ChildClass can be null, leading Gridify to throw an Exception when accessing Name
property. The current work arround is to define the mapping with a ternary expression, like so:
.AddMap("ChildName", e => e.ChildClass == null ? null : e.ChildClass.Name);
I would Gridify to lift that responsibility from me.
Given the previously defined Mapper, the following test should pass:
[Fact]
public void Mapping_ShouldAllowNullProperty()
{
GridifyGlobalConfiguration.AvoidNullReference = true;
var fakeList = AutoFaker.Generate<TestClass>(10);
fakeList.Add(new TestClass() { ChildClass = null });
fakeList.Add(new TestClass() { ChildClass = new TestClass() });
fakeList.Add(new TestClass() { ChildClass = new TestClass() { Name = "glacor" } });
var mapper = new TestClassGridifyMapper();
var result = fakeList.AsQueryable().ApplyFiltering("ChildName=glacor", mapper).Distinct().ToList();
var result2 = fakeList.AsQueryable().ApplyFiltering("ChildName=", mapper).Distinct().ToList();
Assert.Single(result);
Assert.Equal(2, result2.Count);
}