I always wanted to build a class that could have some value in the real life and that could serve as an example for some (good) practices. It seems that I have the opportunity right now.
In every country, mine included, there is some sort of identification number, unique for all its citizens. In the US there is the SSN and in Romania there is a PNC (CNP spelled in romanian) which stands for Personal Numeric Code.
Some applications (web sites and other kinds) may require you, at some point, to specify a valid PNC. These apps usually validate it (lightly most of them) and none of them I've seen to take advantage of the info the PNC conveys for pre-filling certain fields. This sparked me the idea to write a dedicated class for this.
The specifications for this romanian SSN (the PNC) can be found on Wikipedia : translated to english version and romanian (original version).
In short there are 13 digits like so :
S YY MM DD CC III X
S represents the sex and the century of the birth date. Odd digits for males and even digits for females. 1 and 2 for 20th century, 3 and 4 for 19th century, 5 and 6 for 21st century, 7 and 8 for resident foreigners and 9 for non-resident foreigners
YY represents the two-digit version of the birth date's year
MM the same for the month
DD the same for the day of the month
CC represents the two-digit code of the county of birth
III represents an index number - unique per county per day
and finally
X represents the check digit
Not very complicated, isn't it? Let's take mine for example :
1810623420056
So:
- I am a male, born in the 20th century (1)
- I was born in 1981
- .. in June
- on the 23rd day
- in the 42 county (that is Bucharest, 2nd district)
- The fifth in the birth center
- and the check digit is 6
What I would like from a class that models this entity :
- To have a parameterless (default constructor) so it can be easily serializable
- To not throw exceptions in the other constructors if the data is bogus but rather to sum up a list of violations (for example the day is 32)
- To be bind friendly in Winforms, WPF, ASP.NET Web forms and ASP.NET MVC
- To expose a read/write property (we'll call it "Value") to enable easy binding with the ASP.NET MVC's default binder
- The XML serialization should take minimal space (only relevant data should be serialized and elements and attributes should have short names - but still understandable)
- The same for binary serialization
- It should implement IConvertible so the class users should have minimal effort in converting it to/from other types
- It should implement IEquatable and IEquatable<T> so it is easy for one to compare two instances
- Similar thing for IComparable so sorting should be fast and efficient
- There should be a compact form so large collections could be marshalled efficiently even over slow channels
- Last but not least it should provide (a) static method(s) to generate random values
There's a long list of requirements... let's see how we'll manage over the course of the next parts :)
432c4aa1-406d-4c22-bec8-d1864ec9927e|0|.0