0

For a project we are letting employees upload data. This data will all be put into a staging table that is varchars because we don't want to reject the file with bad data. We want to allow someone to fix the invalid fields in a web interface and post the update. We also can't use web field validation because the values are not being loaded through a form but a flat file. We want to iterate over the staging table columns, and have our web service validate each of the fields and ensure they are right (for instance Price might need to be a number but they accidentally typed 'price:123,567.12').

public class SomeClass
{
    public string Code { get; set; }
    public string CodeDesc { get; set; }
    public string Price { get; set; }
}

1 option:

public class SomeClass
{
    public string Code { get; set; }
    public string CodeIsValid { get; set; }
    public List<string> CodeErrorCodes { get; set; }
    public string CodeDesc { get; set; }
    public string CodeDescIsValid { get; set; }
    public List<string> CodeDescErrorCodes { get; set; }
    public string Price { get; set; }
    public string Price IsValid { get; set; }
    public List<string> Price ErrorCodes { get; set; }
}

Option 2. We looked at using IValidateableObject but didn't know if there might be any draw backs to using this and then sending the errors back through a web service.

thanks in advance!

3
  • What is your question? Commented Oct 20, 2015 at 18:19
  • I'm trying to determine the best way to send an object to a method and validate the values of the object. For instance in the example above, say Code can only be 10 characters long. I want to be able to validate the object by running a method against the property value, then return if its valid or not and what the errors are. And then do that for each property in the object. Preferably return the entire object along with any validation errors. Here is (hopefully) an example the clarifies it some, but it seems like there would be an easier way. Commented Oct 22, 2015 at 0:47
  • public class SomeClass { public ValidateableProperty Code { get; set; } public ValidateableProperty CodeDesc { get; set; } public ValidateableProperty Price { get; set; } } public class ValidateableProperty { public string value { get; set; } public bool IsValid { get; set; } public List<string> listValidationErrors { get; set; } } Commented Oct 22, 2015 at 0:47

1 Answer 1

0

Then you should have a look at System.ComponentModel.DataAnnotations

You can use the ones defined in the name or simply create your own by inheriting from a class and overriding its IsValid(Object) method.

You simply add the attributes to your data model (one or more), it will look like

[Required, Url, YourOwn] // just an example
public string MyProp { get; set; }

Here is the Url example

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.