In this post we’ll explore using SQLCLR integration to provide validation of South African Identity numbers – with the focus being slightly more on the ins and outs of CLR integration, than on how RSA ID numbers get validated. If you’re after the validation algorithm, look here.
So what’s on the slab?...
This solution makes RSA ID number validation possible by exposing a C# table valued function (TVF) to SQL server. The function takes an identity number as its input, and returns a single record with the following columns:
- IsValid - bit: Whether the input identity number is valid.
- IsCitizen - bit: Whether the number belongs to a South African citizen.
- Gender - char: The gender of the individual - ‘M’ for male, ‘F’ for female.
- DateOfBirth - date: The date of birth of the individual.
Why a table valued function?
Glad you asked. (...just smile politely, nod and pretend you did!)
Given the 4 output fields above, it is tempting to think that a smarter solution would be to implement a user defined type (UDT) – after all – an ID number can be thought of as a type, right?
Given the 4 output fields above, it is tempting to think that a smarter solution would be to implement a user defined type (UDT) – after all – an ID number can be thought of as a type, right?
Well.. it can… and that’s exactly the problem. UDTs are possibly the most problematic and controversial part of SQL CLR integration, becauseT-SQL Dependencies on UDTs make it very difficult to upgrade the CLR assembly. In other words, if you use a UDT for a table column, then you cannot upgrade the UDT’s assembly without dropping the column. Also, properties exposed by UDTs have to be aliased in SELECT statements – i.e. the name of a UDT property does not translate into a column name/alias when used in a query.
In contrast, implementing a table valued function gives us:
Upgradability: You can unload and upgrade a TVF’s assembly any time, since you won’t unwittingly create a schema-bound dependency on a TVF.
Aesthetics: Columns returned by a TVF default to having sensible names.
Performance: All columns for a row returned by a TVF are returned in single function call, whereas UDTs require a CLR call per property.
The solution…
The solution outlined here shows a couple of best practices when using CLR integration with SQL server, namely:
- The assembly is marked as safe.
(no naughty pointers, no external calls, no unverifiable code blocks) - The assembly is stateless.
SQL unloads application domains when it detects resource constraints, and assemblies must deal with this gracefully. - Exceptions are never thrown.
This means that queries never crash, no matter what data you feed into the TVF.
The code is reasonably straight forward... there are 3 important components:
- The ValidatedRSAID class - which encapsulated the logic of cracking the DateOfBirth, Gender, Citizenship and validity from an ID number.
- The ValidateRSAID method, which takes a SqlString as it's input, and returns a ValidatedRSAID instance.
- The ValidateRSAIDFillRow method, which SQL uses to decode the fields of the ValidatedRSAID instance into a column values in the resulting query.
Here's the code for parts 2 and 3, which expose the ValidatedRSAID functionality to SQL Server:
...and here's the code for validating an ID number. It's been carefully optimized, at the expense of readability:
To download the full solution, head over to https://validatersaid.codeplex.com
Comments