In today’s episode of our Meet the Author podcast series, Fritz Onion talks to John Sonmez about his course Enterprise Library Validation Application Block. In the interview John discusses the two ways of doing validation of business objects using the framework as well as some of the various implementation models you can use the validations in. He then gives some examples of how the validations can be made configurable at runtime via web.config or app.config as well as how to validate nested object structures.
Listen to the Audio (MP3)
[Fritz] Hi this is Fritz Onion. I am here today speaking with John Sonmez about his new course Enterprise Library Validation Application Block. John is a long time C#/.NET developer with roots in C++. He has recently started developing applications for Android and iPhone as well putting a focus on mobile application development. John has spent many years working as a consultant for a number of different projects and has deep ties to the Agile community. He is also very interested in automated testing using tools like WatiN or Selenium. On any given day John could be programming in C#, Java, Objective-C, VB.NET or all of the above. Thanks John for talking with us.
[John] Sure no problem.
[Fritz] So John this is your sixth course in the Enterprise Library Series and I think we have just one more to go, the security and encryption coming up next. But this one covers the validation block, the validation application block and my understanding is the validation block gives you a way of integrating data validation into your application. So do you want to kind of give us an overview of how this fits into your app and how the validation system works?
[John] Sure. So this is a pretty powerful validation system. I would say that it’s mostly targeted towards validating your business objects and it basically works by one of two ways. You can either annotate your classes with some attributes that say what the validation is. Say for example, you know you might have a class and you might have a name on there and you might say well the name can’t be null, it has to be a certain length so you write some annotations directly onto that class and you create a validator and you can validate that class and it would give you results, error results that would tell you whether or not its valid and if not how did it fail, what was the problem. And so that is the one way you can use it but you can also use it as a configuration base. So you could basically not annotate your class at all and you could in the enterprise library configuration which would go into your web.config or your app.config, you can specify a set of rules that apply to a particular type and then allow or perform that validation. You still have to put some code in your app to actually do the validation but the rules would be coming from a config source as opposed to being hard coded which is pretty neat because it gives you that power to be able to change your validation at run time or without having to recompile your application and you still get all the benefits of using a pretty powerful validation engine.
[Fritz] Yeah so that validation does sound very powerful. Let me get an understanding of exactly how this works. So you are saying that if I had, for example, an employee object in my system and that had an attribute on it called salary, I could declaratively in my configuration file say for all instances of the employee class make sure the salary range falls within a range of say zero to 100,000 or something. Is that what you are saying we can do?
[John] Exactly, yeah you would be able to specify that and the nice thing about that would be, let’s say you wanted to change that, you could change that without having to recompile your app, you could just go out there edit that config, change that value and then you would be up and running with that new value. The only thing you have to do in your code is to actually create a validator object from the validation factory and then call validate on it and then process your, you know, generic validation results, so as long as you have that part in your app then you know where it actually get the rules for validation, that can all be configurable.
[Fritz] Right, yeah I can see that and actually the example I chose is apropos so if your salaries happen to increase then you can go in and make that change.
[Fritz] Great, okay so you mentioned that this is probably best targeted at your business layer, your business objects and you manually make the call to perform the validation and look at the results to make sure they were good or look at the errors and messages if they were bad. So do you want to describe a couple of scenarios you know with different technologies of where this would fit in, like with say a WPF application or a WCF application or even a web application, where it might fit in to each of these categories of apps if at all.
[John] Oh sure. So you know there actually is integration for this block into pretty much all the technologies so that there are WPF and WCF and ASP.NET integrations so you can actually use this in your front end if you want to. It’s probably not the best choice in a lot of cases like for example ASP.NET just because ASP.NET already has a pretty powerful validation engine that does the client side as well as the server side. Where this would only do the server side but you know in my course I do include, I do show you how to do WCF and I include some links for WPF and ASP.NET. But really where I would use this is or where I have used this in the past is at that, a little bit below that UI layer where you are trying to validate some business logic where perhaps in your application before you send in objects to database to save it you want to check some rules on that to make sure that its correct. Instead of trying to enforce those constraints in the database where it’s more difficult and you have to deal with the error handling of SQL exceptions and what not. It’s a little bit nicer to move that up one layer and to put this validation right before you do saves or you know after you changed the state of an object and I really like doing it there as well because you can apply different rule sets as well so that is one thing you can do with this block. You could you know for let’s say you had your employee object, you could have different validation rulesets that apply in a different context. That is all configurable, so you might say OK when I am saving this employee under this context, here is the salary requirements, it needs to be between zero to 100 but when we are saving a CEO salary we are going to put different or maybe executive teams salary, we are putting different set of validation requirements to validate that object so even though it’s the same object in the system you are changing the rule sets based on the context.
[Fritz] Interesting, yeah that does sound powerful. Great, so you can specify it based on type in your config file and you can also annotate your classes with it. Are there any restrictions in terms to what type of objects you can validate or you know where to apply these validation rules?
[John] Actually its works pretty well on pretty much any object, you know you can even validate a tree of objects which is pretty neat as well. So let’s say that you had, like maybe, you had an employee object for your example and it has some collections on it, maybe it had subordinate employees or like their immediate reports and that was a collection of employees. Or it had some other collection on there of data on there or data or other objects that you wanted to validate. So if you want to validate that entire tree there’s even rules that you can specify in your configuration to say that when you validate this object. This property on here is a list of employees as well and validate all those, so you can do that nested level of validation. It’s really, I can’t think of any scenarios of the top of my head where you would not be able to apply the validation it’s definitely pretty powerful.
[Fritz] Huh, now that does sound compelling to be able to kind of recursively enumerate your data structures and apply it all the way down to the bottom. Great so this is the Enterprise Library Validation Application Block, if you’re looking for a way to introduce validation into your business objects this may be the tool you’re looking for. New course by John Sonmez, thanks very much John.
[John] You’re welcome.