Esempio n. 1
0
        public static CsvPersonDbo GetNullValue()
        {
            var dbo = new CsvPersonDbo();

            dbo.Id       = 0;
            dbo.LastName = "";
            dbo.Name     = "";
            dbo.SetCityAndZipCode("");
            dbo.Color = "";

            return(dbo);
        }
Esempio n. 2
0
        /// <summary>
        ///     Returns a <see cref="CsvPersonDbo"/>
        /// </summary>
        /// <param name="predicate"></param>
        /// <returns></returns>
        public IEnumerable <CsvPersonDbo> GetPerson(Func <CsvPersonDbo, bool> predicate)
        {
            var config = new CsvConfiguration(CultureInfo.InvariantCulture)
            {
                HasHeaderRecord   = false,
                MissingFieldFound = MissingFieldHandler,
                TrimOptions       = TrimOptions.Trim
            };

            try
            {
                var results = new List <CsvPersonDbo>();
                using (var reader = new StreamReader(this.configuration.Path))
                    using (var csv = new CsvReader(reader, config))
                    {
                        var records = csv.GetRecords <CsvPersonDbo>();

                        foreach (var r in records)
                        {
                            if (SkipCurrentIndex)
                            {
                                SkipCurrentIndex = false;
                                continue;
                            }
                            r.Id = csv.Context.Parser.RawRow;
                            if (predicate.Invoke(r))
                            {
                                results.Add(r);
                            }
                        }
                    }

                var restoredRecords         = RestoreBadRecords();
                var filteredRestoredRecords = restoredRecords.Where(p => predicate(p)).ToList();
                results.AddRange(filteredRestoredRecords);

                if (!results.Any())
                {
                    results.Add(CsvPersonDbo.GetNullValue());
                }

                return(results);
            }
            catch (Exception e)
            {
                throw new CsvDataSourceException(e.Message);
            }
        }
Esempio n. 3
0
 /// <summary>
 ///     Creates a <see cref="CsvPersonDbo"/>
 /// </summary>
 /// <param name="personDbo"></param>
 public void CreatePerson(CsvPersonDbo personDbo)
 {
     try
     {
         using (var writer = new StreamWriter(this.configuration.Path, append: true))
             using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
             {
                 csv.Context.Writer.NextRecord();
                 csv.WriteRecord(personDbo);
             }
     }
     catch (Exception e)
     {
         throw new CsvDataSourceException(e.Message);
     }
 }
Esempio n. 4
0
        public static bool TryToBuild(string lastname, string name, string zipAndCity, string color, long id, out CsvPersonDbo dbo)
        {
            dbo          = new CsvPersonDbo();
            dbo.Id       = id;
            dbo.LastName = lastname;
            dbo.Name     = name;
            dbo.SetCityAndZipCode(zipAndCity);
            dbo.Color = color;

            if (dbo.LastName == string.Empty ||
                dbo.Name == string.Empty ||
                dbo.ZipCode == string.Empty ||
                dbo.City == string.Empty ||
                dbo.Color == string.Empty)
            {
                return(false);
            }

            return(true);
        }