예제 #1
0
        public bool TryValidate(OrderValidatable order, out ICollection <string> errors)
        {
            var context = new ValidationContext(order);
            var results = new List <ValidationResult>();

            var isValid = Validator.TryValidateObject(order, context, results, validateAllProperties: false);

            errors = results.Select(r => r.ErrorMessage).ToList();

            return(isValid);
        }
예제 #2
0
        public OrderValidatable Parse(string line)
        {
            var order = new OrderValidatable();

            var values = line.Split('\t');

            for (var i = 0; i < _propertyNames.Length; i++)
            {
                if (i == values.Length)
                {
                    break;
                }

                switch (_propertyNames[i])
                {
                case "id":
                    if (int.TryParse(values[i], out var id))
                    {
                        order.Id = id;
                    }

                    break;

                case "dt":
                    if (DateTime.TryParse(values[i], out var dateTime))
                    {
                        order.Dt = dateTime;
                    }

                    break;

                case "product_id":
                    if (int.TryParse(values[i], out var productId))
                    {
                        order.ProductId = productId;
                    }

                    break;

                case "amount":
                    if (float.TryParse(values[i], out var amount))
                    {
                        order.Amount = amount;
                    }

                    break;
                }
            }

            return(order);
        }