Formatting properties with FileHelper

。_饼干妹妹 提交于 2019-12-06 03:17:17

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/14761076/formatting-properties-with-filehelper

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!