Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting properties with FileHelper

FileHelpers has a nice date converter for fields:

[FieldConverter(ConverterKind.Date, "MM-dd-yyyy")] 
public DateTime MyDate;

FieldConverter does not work with properties though. I have to deal with objects that use properties so I was looking for something like this:

[PropertyConverter(ConverterKind.Date, "MM-dd-yyyy")] 
public DateTime MyDate { get; set; }

How do I do this with properties?

like image 318
Jaguir Avatar asked Feb 07 '13 21:02

Jaguir


1 Answers

You cannot use converters with Properties.

However, what you can do is create a data model just for the import/export record which is not tied to a domain object. This data model can have fields instead of properties.

So if you have Customers for instance, which are a domain persisted data object, you could create something like CustomerRecord which takes a Customer as a constructor parameter and copies all the data (or use something like Automapper to copy the values for you easily), then just use the file record data model to perform filehelper operations, rather than the domain models.

This seems like additional work, and it is, but it also decouples your domain model from the file operations which is a good design pattern for maintainability.

like image 57
David C Avatar answered Nov 03 '22 02:11

David C