anon
anon

Reputation: 816

No public parameterless constructor found when using Interface

When reading my CSV and using the class map i get an error saying "No public parameterless constructor found." i am 100% sure this is because my Class in my ClassMap<> has a property which is an interface in my case IAddress. Is there a way that this property can be mapped to the class implementation which is Address?

I have tried using a reference map like so References(m => m.Address, mappings);

Here is my code (some properties omitted for brevity):


  public class Customer
  {
    public int Id { get; set; }

    public IAddress CurrentAddress { get; set; }

    public Customer()
    {
    }
  }

  public sealed class CustomerMap : ClassMap<Customer>
  {
      public CustomerMap(Dictionary<string, string> mappings)
      {
          References<AddressMappings>(m => m.CurrentAddress, mappings);
      }
  }

 public class AddressMappings : ClassMap<IAddress>
 {
     public AddressMappings(Dictionary<string, string> mappings)
     {
         Map(m => m.FlatNumber).Name(mappings["FlatNumber"]);
         Map(m => m.PropertyNumber).Name(mappings["PropertyNumber"]);
         Map(m => m.PropertyName).Name(mappings["PropertyName"]);
         Map(m => m.AddressLine1).Name(mappings["AddressLine1"]);
         Map(m => m.AddressLine2).Name(mappings["AddressLine2"]);
         Map(m => m.AddressLine3).Name(mappings["AddressLine3"]);
         Map(m => m.Town).Name(mappings["Town"]);
         Map(m => m.City).Name(mappings["City"]);
         Map(m => m.Ward).Name(mappings["Ward"]);
         Map(m => m.Parish).Name(mappings["Parish"]);
         Map(m => m.County).Name(mappings["County"]);
         Map(m => m.Country).Name(mappings["Country"]);
         Map(m => m.Postcode).Name(mappings["Postcode"]);
     }
 }

using (var reader = new StreamReader(filePath))
using (var csv = new CsvReader(reader))
{
    csv.Configuration.Delimiter = ",";
    var mappingObject = new CustomerMap(mappings);
    csv.Configuration.RegisterClassMap(mappingObject);
    var records = csv.GetRecords<Customer>();
    return records?.ToList();
}


Upvotes: 0

Views: 706

Answers (1)

David Specht
David Specht

Reputation: 9094

I think this might work for you.

public sealed class CustomerMap : ClassMap<Customer>
{
    public CustomerMap(Dictionary<string, string> mappings)
    {
        Map(m => m.CurrentAddress).ConvertUsing(row => 
            new Address {
                FlatNumber = row.GetField<int>(mappings["FlatNumber"]),
                PropertyNumber = row.GetField<int>(mappings["PropertyNumber"]),
                AddressLine1 = row.GetField(mappings["AddressLine1"]),
                AddressLine2 = row.GetField(mappings["AddressLine2"]),
                AddressLine3 = row.GetField(mappings["AddressLine3"]),
                Town = row.GetField(mappings["Town"]),
                City = row.GetField(mappings["City"]),
                Ward = row.GetField(mappings["Ward"]),
                Parish = row.GetField(mappings["Parish"]),
                County = row.GetField(mappings["County"]),
                Country = row.GetField(mappings["Country"]),
                Postcode = row.GetField(mappings["Postcode"])
            });
    }
}

Upvotes: 1

Related Questions