使用FileHelper格式化属性

使用FileHelper格式化属性

本文介绍了使用FileHelper格式化属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个很好的日期转换器,用于字段:

FileHelpers has a nice date converter for fields:

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

FieldConverter不能使用属性。我必须处理使用属性的对象,所以我在寻找这样的东西:

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; }

如何使用属性? b $ b

推荐答案

您不能使用属性转换器。

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.

所以如果你有Customers作为实例,它是一个域持久化数据对象,你可以创建一个类似CustomerRecord, Customer作为构造函数参数,并复制所有数据(或者使用Automapper来轻松地复制值),然后使用文件记录数据模型来执行filehelper操作,而不是域模型。

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.

这篇关于使用FileHelper格式化属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 19:18