CsvHelper icon indicating copy to clipboard operation
CsvHelper copied to clipboard

Fluent Mappings and Writing returns Exception

Open glorioustephan opened this issue 14 years ago • 7 comments

Hello -

I am having some difficulty using a custom fluent mapping and then writing those records into a file using the fluent mappings. Before, I approached the solution by using Custom Attributes, but I pulled the class back into a library to be reused by others, so I needed the use of the fluent mappings to alleviate customizing the export and manipulating the inheritance chain.

Here was my code before, which worked beautifully:

var csv = new CsvWriter(new StreamWriter(fileName));

csv.WriteRecords(myList);

csv.Dispose();
return fileName;

I added a custom class map like so:

public sealed class ContactsExport: CsvClassMap<ContactModel>
{
    public ContactsExport()
    {
        Map(m => m.ID).Ignore();
        Map(m => m.IsActive).Ignore();
        Map(m => m.L1GroupID).Ignore();
        Map(m => m.L2GroupID).Ignore();
        Map(m => m.PersonID).Ignore();
    }
}        

and then modified the first section of code above to incorporate the configuration:

var csv = new CsvWriter(new StreamWriter(fileName));
csv.Configuration.ClassMapping(new ContactsExport());
csv.WriteRecords(myList);
csv.Dispose();

return fileName;

I also tried to use the overloaded method like so, replacing: csv.Configuration.ClassMapping(new ContactsExport()); with: csv.Configuration.ClassMapping<ContactsExport, ContactModel>();

This is the error I receive:

Object reference not set to an instance of an object. (NullReferenceException)
CsvHelper.CsvWriter.WriteRecords[T](IEnumerable`1 records)

C:\Projects\CsvHelper\src\CsvHelper\CsvWriter.cs line 193
Stack Trace:
NullReferenceException at CsvHelper.CsvWriter.WriteRecords[T](IEnumerable`1 records) in C:\Projects\CsvHelper\src\CsvHelper\CsvWriter.cs:line 193

The List<ContactModel> called myList is, in both cases, instantiated and does have records. In my case, three. If I comment out the ClassMapping line, it works fine.

Any pointers would be helpful.

glorioustephan avatar Jul 10 '12 20:07 glorioustephan

What is the class structure of ContactModel?

JoshClose avatar Jul 10 '12 21:07 JoshClose

That brings up a good point. The architecture of the ContactModel, which is an architecture used commonly among other developments we're using for pulling data down from a datasource, does not account for a setter, only a getter.

Here's a rough example:

public class ContactModel : DataSourceModel 
{
    public ContactModel() {}

    public string PersonID { get { return ValueString("PersonID"); } }
    public string FirstName { get { return ValueString("FirstName"); } }
    public string LastName { get { return ValueString("LastName"); } }
    //etc
}

Other than that, this class only has these types of values, all of them nullable properties, and an empty constructor.

glorioustephan avatar Jul 10 '12 22:07 glorioustephan

If you comment out the class mapping line, it uses attribute mapping by default, which just uses the properties.

Your mapping file is just ignoring things. When doing a class map, you need to specify the properties you want mapped.

Having just a getter should not be a problem.

I created a small example that is similar to this and it worked fine. Are you able to create an example console app that fails? If so, email it to me and I'll take a look. If not, we can try a few more things.

JoshClose avatar Jul 11 '12 02:07 JoshClose

I see. So, just to make sure I understand - when using a class mapping, I have to map every field I want as well as specify the ones I am ignoring? However, when using it normally, it will just include all properties that do not have the ignore attribute?

glorioustephan avatar Jul 11 '12 16:07 glorioustephan

When doing class mapping you need to map the properties you want to use. You really don't need to ignore when using class mapping, because it only uses the properties you specify. I could probably even remove that from the options in class mapping, actually.

The default behavior if you don't do any mapping is to grab all the properties and use them. This uses attribute mapping to do so, and if you have an attribute, it will use what you put there. So, for attribute mapping the ignore makes sense.

JoshClose avatar Jul 11 '12 17:07 JoshClose

That makes sense. I've found another solution that alleviates having to use that directly, so I am good to close this ticket. I would love to have a way to exclude certain properties at the location of instantiation and configuration to alleviate modifying derived classes. Any suggestions on this approach without having to map each field?

glorioustephan avatar Jul 11 '12 17:07 glorioustephan

Hmm... You could call the attribute mapping method so it maps all properties by default, then find the properties you want to ignore in the list of mapped properties and turn ignore on, or even remove them from the list.

JoshClose avatar Jul 11 '12 17:07 JoshClose