面向对象的方法来验证文本文件的分隔行

Object oriented way to validate a delimited line of a text file

我在文本文件中有一个管道分隔线。验证线路的最佳方法是什么。对于行中的每个标记应该如何,我有一个明确的格式,例如;说第 5 项应该是日期。

谁能帮我实现这个目标的最佳面向对象方法是什么?有什么设计模式可以实现这个目标吗?

谢谢

您正在寻找特定的验证模式。这可以通过多种方式完成,但最简单的是在对象的构造函数中进行验证。由于您正在寻找一种更面向对象的方法,您可能会考虑创建一个对象来表示文件和一个对象来表示每条记录。除了协会之外,这里没有真正的模式。但是,您可以利用迭代器模式让您的记录在循环中迭代。你说的是读取一个文本文件,所以这个过程还不够复杂,但如果是的话,你可以考虑使用工厂模式来创建文件对象。如果有很多要验证的内容,那么您可以创建一个单独的方法来验证 class 中的每一个。这是我正在谈论的一个例子...

 static void Main(string[] args)
    {

        DataFile myFile = new DataFile(@"C:\...");

        foreach (DataRecord item in myFile)
        {
            Console.WriteLine("ID: {0}, Name: {1}, StartDate: {2}", item.ID, item.Name, item.StartDate);
        }

        Console.ReadLine();
    }


    public class DataFile : IEnumerable<DataRecord>
    {
        HashSet<DataRecord> _items = new HashSet<DataRecord>();


        public DataFile(string filePath)
        {
            // read your file and obtain record data here... 
            //I'm not showing that
            //... then begin iterating through your string results
            //... though I'm only showing one record for this example

            DataRecord record = new DataRecord("1234|11-4-2015|John Doe");
            _items.Add(record);
        }


        public IEnumerator<DataRecord> GetEnumerator()
        {
            foreach (DataRecord item in _items)
            {
                yield return item;
            }
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }

    public class DataRecord
    {
        private int _id;

        public int ID
        {
            get { return _id; }
            private set { _id = value; }
        }

        private DateTime _startDate;

        public DateTime StartDate
        {
            get { return _startDate; }
            private set { _startDate = value; }
        }

        private string _name;

        public string Name
        {
            get { return _name; }
            private set { _name = value; }
        }

        internal DataRecord(string delimitedRecord)
        {
            if (delimitedRecord == null)
                throw new ArgumentNullException("delimitedRecord");

            string[] items = delimitedRecord.Split('|');


            //You could put these in separate methods if there's a lot

            int id = 0;
            if (!int.TryParse(items[0], out id))
                throw new InvalidOperationException("Invalid type...");

            this.ID = id;

            DateTime startDate = DateTime.MinValue;
            if (!DateTime.TryParse(items[1], out startDate))
                throw new InvalidOperationException("Invalid type...");

            this.StartDate = startDate;

            //This one shouldn't need validation since it's already a string and 
            //will probably be taken as-is
            string name = items[2];

            if (string.IsNullOrEmpty(name))
                throw new InvalidOperationException("Invalid type...");

            this.Name = name;
        }


    }

实现此目的的 "clean" 方法是使用正则表达式。这是一个基本示例:

var allLines = new List<string>();

for (int i = 0; i < 5; i++)
{
     allLines.Add("test" + i);
}

// if you add this line, it will fail because the last line doesn't match the reg ex
allLines.Add("test");

var myRegEx = @"\w*\d"; // <- find the regex that match your lines
Regex regex = new Regex(myRegEx);
var success = allLines.All(line => regex.Match(line).Success);

在这个例子中,我的正则表达式等待一个紧跟数字的单词。您所要做的就是找到与您的行匹配的正则表达式。

您还可以通过使用更复杂的正则表达式来避免 linq 表达式。

请提供您的令牌,以便我们为您提供帮助。