用于在平面文件中对字段进行分组和结构化数据的设计模式 [英] Design pattern for grouping fields and structuring data in a flat file

查看:85
本文介绍了用于在平面文件中对字段进行分组和结构化数据的设计模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用FileHelpers C#库将文件读取到将要处理的自定义对象数组中。例如,部分文件定义:

I am using the FileHelpers C# library to read a file into an array of custom objects that will be processed. For instance, a partial file definition:

[FixedLengthRecord]
public class Row
{
    [FieldFixedLength(9)]
    [FieldTrim(TrimMode.Right)]
    public string Ssn;

    [FieldFixedLength(15)]
    [FieldTrim(TrimMode.Right)]
    public string LastName;

    ...
}

我正在尝试在执行此操作时要遵守OOP原则,我注意到某些字段具有自然分组(例如 SSN ClientID EmployerID EmployerName 等),所以我试图将它们分成单独的类(例如客户雇主)。但这似乎有问题,因为某些字段需要在对象之间共享(例如 ClientID 需要知道关联的 EmployerID )。

I'm trying to abide by OOP principles while doing this and I've noticed that some fields have a natural grouping (e.g. SSN and ClientID, EmployerID and EmployerName, etc.), so I tried to break them up into separate classes (e.g. Client and Employer). But this seems problematic because some of the fields need to be shared across objects (e.g. the ClientID needs to know the associated EmployerID).

为了使事情更加复杂,我想在类定义中添加具有 [FieldNotInFile] 属性的字段( s),因为在处理期间,我的应用程序将查询与特定行字段匹配的数据库记录,并填充行的相应 [FieldNotInFile] s(例如,获取客户端的 FName 来自基于 SSN 的数据库,因为它不在文件中。)

To further complicate things, I would like to add fields with the [FieldNotInFile] attribute to the class definition(s) because during processing, my application will query for database records that match a specific row field and populate the row's respective [FieldNotInFile]s (e.g. get client's FName from database based on SSN since it's not in the file).

您将如何构成?我只是想将整个文件行都保留为一个类,但是它接近75个字段,这似乎很荒谬。有想法吗?

How would you structure this? I kind of just want to keep the whole file row as one class, but it's approaching 75 fields, which seems ridiculous. Thoughts?

推荐答案

FileHelpers类只是使用C#语法定义平面文件规范的一种方式。

A FileHelpers class is just a way of defining the specification of a flat file using C# syntax.

因此,FileHelpers类是C#类的不寻常类型,您应该尝试使用公认的OOP原则。 (FileHelpers类有很多违反OOP原则的方式,最明显的是它要求您使用公共字段而不是属性)。 FileHelpers不应具有FileHelpers库所使用的属性或方法。

As such, the FileHelpers classes are an unusual type of C# class and you should not try to use accepted OOP principles. (There are many ways a FileHelpers class violates OOP principles, most obviously it requires you to use public fields instead of properties). FileHelpers should not have properties or methods beyond the ones used by the FileHelpers library.

仅将FileHelpers类视为CSV格式的规范。那应该是它的唯一作用。然后,如果您需要更正常对象中的记录,则将结果映射到更好的对象:

Think of the FileHelpers class as the 'specification' of your CSV format only. That should be its only role. Then if you need the records in a more 'normal' object, then map the results to something better:

FileHelperEngine engine = new FileHelperEngine<FileHelpersOrder>(); 
var records = engine.ReadFile("FileIn.txt");

var niceOrders = records.Select(
    x => new NiceOrder() 
       { Number = x.Number,  
         Customer = x.Customer 
         // etc.
       });

其中 FileHelpersOrder 是CSV规范, NiceOrder 类将是具有属性,方法等的适当OOP类。

Where FileHelpersOrder is your CSV specification and the NiceOrder class would be a proper OOP class with properties, methods, etc. as necessary.

这篇关于用于在平面文件中对字段进行分组和结构化数据的设计模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆