8000 Gridify should allow intermediate null values · Issue #268 · alirezanet/Gridify · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Gridify should allow intermediate null values #268
Open
@glacorSoul

Description

@glacorSoul

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);
   }

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0