Forum Cross Platform with Xamarin
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

PCL Based Object Validation

I've been spending a bit of time looking at all the options for doing cross platform validation and I was wondering what the folks here thought.

I've been looking at lots of various solutions to this that involve the Bait and Switch methods and various forms of TypeDescriptor tricks to swap out implementations based on the target Frameworks and so on.

All very good and neat tricks but when it comes to enforcing validation I can't help but think these feel like because you have a hammer everything looks like a nail (sorry for any non English speakers that's a funny coloquial saying around here).

My feeling about this in relation to validation is I don't just want to share the same class across my server and clients. For instance:

public class User { [StringLength(100), Required] public string Name { get; set; } }

What I want is the exact same validation to happen at both ends of the pipe. When the server gets a request I want it to execute the exact same rules as my own authored client application. It seems that from some implementations I've seen a bare un-attributed version of the class is used on the client and the DataAnnotations based validation exists only on the server side, that just doesn't make sense to me in terms of validation.

In my MvvmCross application I've made use of the naming convention based type searching to make all my view models self validating using the PCL library FluentValidation to provide the heavy lifting.

In my MVC WebAPI up until now I've been using DataAnnotations and whilst there are some tricks to get them to work I can't help feeling that an entirely different approach makes sense when it comes to validation.

Why send a potentially expensive invalid request to the server only to get an error back? Makes more sense to share the validation implementation and validate long before a request is even considered worth sending since you know the server will reject it.

I guess I'm just wondering if anyone has done something similar and what they approach ended up looking like.


  • josemmortegajosemmortega ESUniversity ✭✭✭

    Hi @JamesGreen.8031. Yes, I use there similar validation, that you suggest in your post. I use attributtes for every property and use a ValidationService that I call when need a validation, I send for parameters the data that require validation and check for Reflection.

    I think is the better solution to include validation and it's very reusable.

Sign In or Register to comment.