예제 #1
0
        private static IRecordValidator CreateCustom(this ValidatorBuilder validator)
        {
            var customConfig = config.GetSection("custom");

            return(validator
                   .ValidateFirstName(customConfig.GetSection("firstName:min").Get <int>(), customConfig.GetSection("firstName:max").Get <int>())
                   .ValidateLastName(customConfig.GetSection("lastName:min").Get <int>(), customConfig.GetSection("lastName:max").Get <int>())
                   .ValidateDateOfBirthValidator(customConfig.GetSection("dateOfBirth:from").Get <DateTime>(), customConfig.GetSection("dateOfBirth:to").Get <DateTime>())
                   .ValidateSalary(customConfig.GetSection("salary:min").Get <decimal>(), customConfig.GetSection("salary:max").Get <decimal>())
                   .ValidateDepartment(customConfig.GetSection("department:from").Get <short>(), customConfig.GetSection("department:to").Get <short>())
                   .ValidateClass(customConfig.GetSection("class:min").Get <char>(), customConfig.GetSection("class:max").Get <char>())
                   .Create());
        }
예제 #2
0
        /// <summary>
        /// Read all records from stream.
        /// </summary>
        /// <returns>List of records.</returns>
        public IList <FileCabinetRecord> ReadAll()
        {
            List <FileCabinetRecord> list = new List <FileCabinetRecord>();
            var    validator = new ValidatorBuilder().CreateDefault();
            string rec       = this.stream.ReadLine();

            if (string.IsNullOrEmpty(rec))
            {
                return(list);
            }

            do
            {
                string[] elements = rec.Split(", ");
                if (int.TryParse(elements[0], out int id) &&
                    DateTime.TryParse(elements[3], out DateTime dateOfBirth) &&
                    char.TryParse(elements[4], out char gender) &&
                    short.TryParse(elements[5], out short passportId) &&
                    decimal.TryParse(elements[6], out decimal salary))
                {
                    RecordData recordData = new RecordData(elements[1], elements[2], dateOfBirth, gender, passportId, salary);
                    try
                    {
                        validator.ValidateParameters(recordData);
                        var record = new FileCabinetRecord
                        {
                            Id          = id,
                            FirstName   = recordData.FirstName,
                            LastName    = recordData.LastName,
                            DateOfBirth = recordData.DateOfBirth,
                            Gender      = recordData.Gender,
                            PassportId  = recordData.PassportId,
                            Salary      = recordData.Salary,
                        };
                        list.Add(record);
                    }
                    catch (ArgumentNullException e)
                    {
                        Console.WriteLine(e.Message);
                    }
                    catch (ArgumentException e)
                    {
                        Console.WriteLine(e.Message);
                    }
                }

                rec = this.stream.ReadLine();
            }while (rec != null);

            return(list);
        }
예제 #3
0
        /// <summary>
        /// Read all records from stream.
        /// </summary>
        /// <returns>List of records.</returns>
        public IList <FileCabinetRecord> ReadAll()
        {
            List <FileCabinetRecord> result = new List <FileCabinetRecord>();
            RecordForSerializer      list;

            var validator = new ValidatorBuilder().CreateDefault();

            XmlSerializer ser = new XmlSerializer(typeof(RecordForSerializer));

            try
            {
                list = (RecordForSerializer)ser.Deserialize(this.stream);
            }
            catch (InvalidOperationException ex)
            {
                Console.WriteLine(ex.Message);
                return(result);
            }

            foreach (var record in list.Record)
            {
                try
                {
                    RecordData data = new RecordData(record.Name.FirstName, record.Name.LastName, record.DateOfBirth, record.Gender, record.PassportId, record.Salary);
                    validator.ValidateParameters(data);
                    result.Add(new FileCabinetRecord()
                    {
                        Id          = record.Id,
                        FirstName   = record.Name.FirstName,
                        LastName    = record.Name.LastName,
                        DateOfBirth = record.DateOfBirth,
                        Gender      = record.Gender,
                        PassportId  = record.PassportId,
                        Salary      = record.Salary,
                    });
                }
                catch (ArgumentNullException e)
                {
                    Console.WriteLine(e.Message);
                }
                catch (ArgumentException e)
                {
                    Console.WriteLine(e.Message);
                }
            }

            return(result);
        }
예제 #4
0
        /// <summary>
        /// Creates custom validator.
        /// </summary>
        /// <param name="validatorBuilder">The instance of ValidatorBuilder class.</param>
        /// <returns>The instance of IRecordValidator class.</returns>
        public static IRecordValidator CreateСustom(this ValidatorBuilder validatorBuilder)
        {
            var rulesets = GetRulesets();

            ValidationRuleset ruleset = rulesets.Custom;

            return(new CompositeValidator(new List <IRecordValidator>
            {
                new FirstNameValidator(ruleset.FirstName.MinLength, ruleset.FirstName.MaxLength),
                new LastNameValidator(ruleset.LastName.MinLength, ruleset.LastName.MaxLength),
                new DateOfBirthValidator(ruleset.DateOfBirth.From, ruleset.DateOfBirth.To),
                new CabinetNumberValidator(ruleset.CabinetNumber.MinValue, ruleset.CabinetNumber.MaxValue),
                new CategoryValidator(ruleset.Category.MinValue, ruleset.Category.MaxValue),
                new SalaryValidator(ruleset.Salary.MinValue),
            }));
        }