Formatting properties with FileHelper
Asked Answered
A

1

6

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?

Albright answered 7/2, 2013 at 21:12 Comment(0)
M
4

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.

Marcum answered 7/2, 2013 at 21:50 Comment(2)
This is exactly what I was trying to avoid. Lazy me. :PAlbright
Yep, sorry lol. Though if you think about it, you gain all kinds of advantages this way. You can flatten nested object hierarchies into a single level, which is going to be required for filehelper files (you can't store via XML or relational data structures). The library is great for flat records though.Marcum

© 2022 - 2024 — McMap. All rights reserved.