0

I'm trying to map a NetTopologySuite.Geometries.MultiPoint with Automapper but I keep getting the error System.ArgumentException: NetTopologySuite.Geometries.MultiPoint needs to have a constructor with 0 args or only optional args.

var config = new MapperConfiguration(cfg => {});
var mapper = config.CreateMapper();
     
MultiPoint mp1 = null;
MultiPoint mp2 = mapper.Map<MultiPoint>(mp1); // throws

Indeed, this type doesn't have a constructor with 0 arguments. I've tried specifying how to instantiate the type:

new MapperConfiguration(cfg => {
    cfg.CreateMap<MultiPoint, MultiPoint>()
        .ConstructUsing(mp => new MultiPoint((Point[])mp.Geometries));
});

Same error. To reproduce with a simpler code, I created a class with no 0 args constructor.

var config = new MapperConfiguration(cfg => { });
var mapper = config.CreateMapper();

TestCollection tc1 = null;
TestCollection tc2 = mapper.Map<TestCollection>(tc1); // throws

class Test
{
}

class TestCollection : IEnumerable<Test>
{
    public TestCollection(Test[] tests) => Tests = tests;
    public Test[] Tests { get; set; }
    public IEnumerator<Test> GetEnumerator() => new TestCollectionEnumerator();
    IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}

class TestCollectionEnumerator : IEnumerator<Test>
{
    object IEnumerator.Current => Current;
    public Test Current { get; }
    public bool MoveNext() => false;
    public void Reset() { }
    public void Dispose() { }
}

Is it a bug or am I missing something?

Greg
  • 792
  • 2
  • 9
  • 24
  • The full code is in the first snippet. The same snippet in a [dotnetfiddle](https://dotnetfiddle.net/m8Lktd). – Greg Mar 09 '21 at 13:27
  • I think I'm using the latest version here (10.1.1) – Greg Mar 09 '21 at 15:33
  • I've updated the question with a repro without `MultiPoint`. – Greg Mar 09 '21 at 19:14
  • Well, yes, but you do need the map and the `ConstructUsing` :) And you cannot map smth both as a collection and a POCO at the same time. Check [the execution plan](https://docs.automapper.org/en/latest/Understanding-your-mapping.html). – Lucian Bargaoanu Mar 09 '21 at 19:23
  • I'm not sure to understand. Is it expected? Is there any alternative? Looking at the execution plan, it is not clear where it throws. In does contain a ` $typeMapDestination = ($dest ?? .New A.TestCollection(.Call System.Array.Empty()));` which means the `ConstructUsing` seems to work. – Greg Mar 09 '21 at 20:11

1 Answers1

0

I think it's because your MultiPoint is null. With Nettopologysuite the multipoint (points inside can't be null).

MultiPoint mp1 = null;

To

MultiPoint mp1 = NetTopologySuite.Geometries.MultiPoint.Empty;
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
  • In the end I've used `MultiPoint.Empty` but I don't get why null isn't mapped to null. – Greg Sep 03 '21 at 10:06