Enterprise Data Validation<br />Hampton Roads .NET User Group<br />November2009<br />W. Kevin Hazzard, C# MVP, MCSD.NET<br />
Problem Statement<br />Getting your data validation synchronized across all the tiers of an application isn&apos;t easy<br...
One Possible Solution<br />Extend SQL Server to support Regular Expression matching<br />Store validation metadata as a se...
What are extended properties?<br />SQL_VARIANT metadata that can be applied to many SQL database objects<br />Examples<br ...
SQL Objects That May Be Extended<br /><ul><li>Aggregate
Asymmetric Key
Assembly
Certificate
Column*
Contract
Database
Default
Event Notification
Filegroup
File name
Schema
Service
Upcoming SlideShare
Loading in …5
×

Enterprise Data Validation

1,536 views
1,429 views

Published on

You write data validation code in your user interfaces to provide a good user experience. Then you write similar validation code in your services because other clients may not implement those validations in the future. Then you write the same data validations again in the database, well, let's face it, because DBAs are justifiably paranoid about the quality of their data. During this talk, Kevin Hazzard will show you how implement your data validations in a way that they can be shared across all the tiers of your enterprise applications.

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,536
On SlideShare
0
From Embeds
0
Number of Embeds
28
Actions
Shares
0
Downloads
15
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Enterprise Data Validation

  1. 1. Enterprise Data Validation<br />Hampton Roads .NET User Group<br />November2009<br />W. Kevin Hazzard, C# MVP, MCSD.NET<br />
  2. 2. Problem Statement<br />Getting your data validation synchronized across all the tiers of an application isn&apos;t easy<br />Storing metadata about validations that need to occur as pure data can be less than ideal<br />
  3. 3. One Possible Solution<br />Extend SQL Server to support Regular Expression matching<br />Store validation metadata as a set of extended properties in SQL Server<br />Expose your validation metadata as a service<br />
  4. 4. What are extended properties?<br />SQL_VARIANT metadata that can be applied to many SQL database objects<br />Examples<br />Add a DATETIME stamp or integer serial number to a table definition to mark its schema version<br />Add Spanish and English description properties to a view as documentation for consumers<br />Add a tag to a public key certificate to record where the private key is escrowed and by whom<br />Add a regular expression to a table&apos;s column definition to record the pattern (input mask) that should be used to validate the data<br />
  5. 5. SQL Objects That May Be Extended<br /><ul><li>Aggregate
  6. 6. Asymmetric Key
  7. 7. Assembly
  8. 8. Certificate
  9. 9. Column*
  10. 10. Contract
  11. 11. Database
  12. 12. Default
  13. 13. Event Notification
  14. 14. Filegroup
  15. 15. File name
  16. 16. Schema
  17. 17. Service
  18. 18. Symmetric Key
  19. 19. Synonym
  20. 20. Table
  21. 21. Trigger
  22. 22. Type
  23. 23. User
  24. 24. View
  25. 25. XML Schema Collection
  26. 26. Function
  27. 27. Message Type
  28. 28. Partition Function
  29. 29. Partition Scheme
  30. 30. Plan Guide
  31. 31. Procedure
  32. 32. Queue
  33. 33. Remote Service Binding
  34. 34. Route
  35. 35. Rule</li></ul>* Columns aren&apos;t objects in SQL server but they can be extended<br />
  36. 36. Extended Property Functions<br />sp_addextendedproperty<br />sp_updateextendedproperty<br />sp_dropextendedproperty<br />fn_listextendedproperty<br />sys.extended_properties<br />
  37. 37. Adding Extended Properties<br />sp_addextendedproperty<br /> [ @name = ] { &apos;property_name&apos; }<br /> [<br /> , [ @value = ] { &apos;value&apos; } <br /> [ , [ @level0type = ] { &apos;level0_object_type&apos; } <br /> , [ @level0name = ] { &apos;level0_object_name&apos; } <br /> [ , [ @level1type = ] { &apos;level1_object_type&apos; } <br /> , [ @level1name = ] { &apos;level1_object_name&apos; } <br /> [ , [ @level2type = ] { &apos;level2_object_type&apos; } <br /> , [ @level2name = ] { &apos;level2_object_name&apos; }]]] <br /> ]<br />Returns 0 (false) for failure or 1 (true) for success.<br />
  38. 38. Add Extended Property to Column<br />EXEC sp_addextendedproperty@name = N&apos;Name of the extended property&apos;,@value = N&apos;Value of the extended property&apos;,@level0type = N&apos;Schema&apos;, @level0name = SCHMNAME,@level1type = N&apos;Table&apos;, @level1name = TBLNAME,@level2type = N&apos;Column&apos;, @level2name = COLNAME;<br />
  39. 39. Listing Extended Properties<br />fn_listextendedproperty ( <br /> { default | &apos;property_name&apos; | NULL } <br /> , { default | &apos;level0_object_type&apos; | NULL } <br /> , { default | &apos;level0_object_name&apos; | NULL } <br /> , { default | &apos;level1_object_type&apos; | NULL } <br /> , { default | &apos;level1_object_name&apos; | NULL } <br /> , { default | &apos;level2_object_type&apos; | NULL } <br /> , { default | &apos;level2_object_name&apos; | NULL } <br /> )<br />Table-valued function returning:<br />objtype, objname and name as sysname<br />value as sql_variant<br />
  40. 40. Listing Extended Properties for a Column<br />SELECT objtype, objname, name, value<br /> FROM fn_listextendedproperty( NULL,<br /> &apos;schema&apos;, &apos;SCHMNAME&apos;,<br /> &apos;table&apos;, &apos;TBLNAME&apos;,<br /> &apos;column&apos;, default );<br />
  41. 41. Querying sys.properties<br />SELECT<br />ep.major_id, ep.minor_id,<br />t.name AS [Table Name],<br />ep.name AS [ExtProp Name],<br />c.name AS [Column Name],<br />ep.value AS [Extended Property]<br /> FROM sys.extended_properties AS ep<br /> INNER JOIN sys.tables AS t<br /> ON ep.major_id = t.object_id<br /> INNER JOIN sys.columns AS c<br /> ON ep.major_id = c.object_id<br /> AND ep.minor_id = c.column_id<br /> WHERE ep.class = 1;<br />
  42. 42. Demonstration 1<br />Experimenting with SQL Extended Properties<br />
  43. 43. Using Regular Expression in SQL Server to Validate Data<br />We can extend SQL Server by adding functions written in C# or Visual Basic<br />The .NET Framework has a great Regular Expressions implementation – let&apos;s use it!<br />public class UserDefinedFunctions<br />{<br /> [SqlFunction(IsDeterministic = true)]<br /> public static SqlBooleanRegexIsMatch(<br />SqlString pattern, SqlString input)<br /> {<br />varrex = new Regex(pattern.Value);<br /> return rex.IsMatch(input.Value);<br /> }<br />};<br />
  44. 44. Don&apos;t Forget This<br />You must enable the SQL CLR<br />It&apos;s disabled by default<br />sp_configure&apos;clr enabled&apos;, 1RECONFIGURE WITH OVERRIDEGO<br />
  45. 45. Demonsration 2<br />Adding a SQL CLR User-Defined Function for<br />Regular Expression Matching<br />
  46. 46. Pulling It All Together<br />What if we could save regular expressions as metadata to validate our OLTP data?<br />What if we had conventions for storing and accessing that metadata?<br />We could write helper functions that took full advantage of those conventions<br />We could write other kinds of assertions and constraints in the database that depended on them, too<br />We could expose that metadata as a service and use it in multiple application tiers<br />
  47. 47. Demonstrations 3 & 4<br />Baking Validation Into SQL Using Metadata and Constraints<br />Implementing a WCF Service to Expose Validation Metadata<br />
  48. 48. For More Information<br />Kevin Hazzard&apos;s Bloghttp://tinyurl.com/KevinHazzard<br />Kevin Hazzard&apos;sSlideshare Sitehttp://slideshare.net/wkhazzard<br />Kevin Hazzard&apos;sSpeakerRate Sitehttp://speakerrate.com/wkhazzard<br />Phil Factor&apos;s RegEx Functions for SQLCLRhttp://tinyurl.com/dmsmm2<br />

×