Click here to Skip to main content

Welcome to the Lounge

   

For lazing about and discussing anything in a software developer's life that takes your fancy.
The Lounge is rated PG. If you're about to post something you wouldn't want your kid sister to read then don't post it. No flame wars, no abusive conduct and please don't post ads.

Technical discussions are welcome, but if you need specific help please use the programming forums.


 
GeneralRe: Try to read this message PinmvpOriginalGriff9-Feb-13 5:51 
GeneralRe: Try to read this message Pinmember ProgramFOX9-Feb-13 5:54 
GeneralRe: Try to read this message Pinmember Michael Martin 9-Feb-13 5:49 
GeneralRe: Try to read this message Pinmember ProgramFOX9-Feb-13 5:51 
GeneralRe: Try to read this message PinmvpOriginalGriff9-Feb-13 5:53 
GeneralRe: Try to read this message PinmvpOriginalGriff9-Feb-13 5:52 
GeneralRe: Try to read this message Pinmember Michael Martin 9-Feb-13 6:17 
GeneralPerhaps I should write an article PinmemberCDP18029-Feb-13 4:47 
I'm sitting here rewriting my former C# libraries in C++, and have come to a subject which I obviously see very differently than the rest of the world.
 
I'm talking about data objects, those objects which are passed between all layers of an application from the UI down to the database. Wherever you look, you are told that the data objects should be simple containers.
 
That's where I start to see things differently. I think each property should be initialized to a certainly invalid value, not just left to whatever defaults the properties may have in a freshly created data object. Picking such values may not be so easy. Just think of a integer database column that allows NULL. The definition of invalid values should also be done in a non-redundant way, not in the constructor of some data object. Anyway, the initially invalid values help in detecting bugs when properties of the data objects are accidentally not filled.
 
That assumes, of course, that the values of data objects are validated at all. How should the validation be done? The application logic must validate the data objects before doing anything with them. That's its job. It can't simply assume that validation has already been done in the UI. Who guarantees that the validation in the UI was complete and correct or was done at all? How do we guarantee that the UI and the application logic validate exactly in the same manner?
 
My answer: A smarter data object, not just a simple struct. To begin with, the data objects get a collection to hold data field objects which now represent the properties. The data fields define invalid and (where needed) maximum and minimum values for all basic data types. They form a small class hierarchy and allow you to create more project specific types by inheritance.
 
Let's take a string as an example. In the database a column may be declared as VARCHAR(some length). The corresponding field in the database should then make sure that the string never exceeds the size of the column. Exceptions or truncation may otherwise be the result, both not wanted. Now let's say that not just any string of up to this length will be. Let's say it's supposed to hold a mail address and has to be checked against a regex. It's just a matter of deriving a regex data field from the string data field and overriding its Validate() method.
 
In the constructor of the data object this field and all others that are needed. In this case the maximum length and the regex to check against would have to be set. Now we have the constructor of the data object as the one single place where we define what is valid and what is not. This definition is complete and non-redundant. Validation of the data object is done by simply going through the entire collection of data fields and calling their Validate() method. This can be done in whatever layer at any time and again we have the benefit of assuring a complete validation in a non-redundant way.
 
The code to implement the base class of the data objects and the data fields is almost beginner level, very reusable and expandable or adaptable. It is not in the way of any ORM, unless it insists on auto-generating the data objects for you. NHibernate, for example, only wants to know how to map a column from a database table to a property of a data object. It does not care about anything inside the data object.
 
And there are more things I would add to the data objects. How about factory methods to prepare new instances? They make preparing new data objects that pass validation much easier. What about application specific logic like encrypting passwords? Will we also smear those things over all layers or just put them where they are actually used?
 
I have made a few applications that turned out to be very reliable and, if needed, easy to diagnose. A small dispute with some Java developers over this (I actually only wanted to add a validation method to the data objects, not the whole bible) also cost me my last job in the end. Anything but struct-like data objects was not their 'standard'.
 
Am I wrong? Am I overlooking something? Or did some guru just not think of this and now the whole world is religiously imitating the guru's 'standard'?
GeneralRe: Perhaps I should write an article Pinmemberwout de zeeuw9-Feb-13 5:02 
GeneralRe: Perhaps I should write an article PinmemberCDP18029-Feb-13 5:50 
GeneralRe: Perhaps I should write an article Pinmemberwout de zeeuw9-Feb-13 6:01 
GeneralRe: Perhaps I should write an article PinmemberCDP18029-Feb-13 6:17 
GeneralRe: Perhaps I should write an article Pinmemberwout de zeeuw9-Feb-13 6:28 
GeneralRe: Perhaps I should write an article PinmemberCDP18029-Feb-13 7:10 
GeneralRe: Perhaps I should write an article Pinmemberwout de zeeuw9-Feb-13 8:11 
GeneralRe: Perhaps I should write an article PinmemberPIEBALDconsult9-Feb-13 5:20 
GeneralRe: Perhaps I should write an article PinmemberCDP18029-Feb-13 6:02 
GeneralRe: Perhaps I should write an article [modified] PinmvpEspen Harlinn9-Feb-13 7:22 
GeneralRe: Perhaps I should write an article PinmemberCDP18029-Feb-13 12:06 
GeneralRe: Perhaps I should write an article PinmvpEspen Harlinn9-Feb-13 15:39 
GeneralRe: Perhaps I should write an article PinmemberBillWoodruff9-Feb-13 9:17 
GeneralRe: Perhaps I should write an article PinmemberCDP18029-Feb-13 10:52 
RantRe: Perhaps I should write an article PinmemberVivic9-Feb-13 17:25 
GeneralRe: Perhaps I should write an article PinmemberCDP18029-Feb-13 21:55 
GeneralRe: Perhaps I should write an article Pinmemberjschell10-Feb-13 10:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


Advertise | Privacy | Mobile
Web04 | 2.8.1411022.1 | Last Updated 22 Nov 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid