tags:

views:

86

answers:

3

This question arose when I was trying to figure out a larger problem that, for simplicity sake, I'm omitting.

I have to represent a certain data structure in C#. Its a protocol that will be used to communicate with an external system. As such, it has a sequence of strings with predefined lengths and integer (or other, more complicated data). Let's assume:

SYSTEM : four chars
APPLICATION : eight chars
ID : four-byte integer

Now, my preferred way to represent this would be using strings, so

class Message
{
    string System {get; set; };      // four characters only!
    string Application {get; set; }; // eight chars
    int Id {get; set; };
}

Problem is: I have to ensure that string doesn't have more than the predefined length. Furthermore, this header will actually have tenths of fields, are those will change every now and then (we are still deciding the message layout).

How is the best way to describe such structure? I thought, for example, to use a XML with the data description and use reflection in order to create a class that adheres to the implementation (since I need to access it programatically).

And, like I said, there is more trouble. I have other types of data types that limits the number of characters/digits...

+1  A: 

Take a look at DataAnnotations attributes, you can use a validation routine after for all your objects, see this link

Gregoire
I actually was looking for something like this. Intense use of reflection, alright, but still, it would do what I wan't.
Bruno Brant
+1  A: 

You may find this post helpful.

klausbyskov
Klausbyskov, your implementation was exactly what I did. I create a TextField class to represent my fixed length strings, and the constructor receives it as a parameter. What was the result of your coding? Did you find any pitfalls that I should avoid when using such classes to abstract my domain model?
Bruno Brant
+7  A: 

For starters: the whole length issue. That's easily solved by not using auto-properties, but instead declaring your own field and writing the property the "old-fashioned" way. You can then validate your requirement in the setter, and throw an exception or discard the new value if it's invalid.

For the changing structure: If it's not possible to just go in and alter the class, you could write a solution which uses a Dictionary (well, perhaps one per data type you want to store) to associate a name with a value. Add a file of some sort (perhaps XML) which describes the fields allowed, their type, and validation requirements.

However, if it's just changing because you haven't decided on a final structure yet, I would probably prefer just changing the class - if you don't need that sort of dynamic structure when you deploy your application, it seems like a waste of time, since you'll probably end up spending more time writing the dynamic stuff than you would altering the class.

Michael Madsen
+1 Those three paragraphs describe my thoughts on this question so preceisly that I am truly frightened.
Jeffrey L Whitledge
Michael, good answer, mainly because it adheres to KISS. However, my desire to use XML would be to allow non-c#-programmers to change the description of the data. I dislike the idea of changing the class because of the effort involved. But maybe I'm just lazy. +1 for the simplicity.
Bruno Brant
@Bruno: I don't really know enough about your specific scenario to know whether there's even any point in letting them do that, but remember that it's not enough to just change the definition of this class. You would need code to handle these new and changed fields anyway - something needs to know how to load and store stuff in this class. Also, since you say this is an abstraction of a protocol for communication with an external system, how will that system know how to handle the changes?
Michael Madsen
Based on that, I'm not sure it makes sense to allow this - but if it *does* make sense, make sure You Really Need It™.
Michael Madsen