Custom Rule for database projects is one of my favorite extension point of Visual Studio 2008, and I blogged about it some time ago. Yesterday I tried to convert my sample rule for visual studio 2010 beta2, and there are a lot of things that are changed. The result will be a simple rule that force column name to be in form xxxx_xxxxxxxx as in the past example
First of all the extension needs to specify its details with attributes. The DatabaseSchemaProviderCompatibility permits you to specify the version of the database the rule applies to, in my example a standard sqlDatabase, then the DataRuleAttirbute is used to specify name, id, description, and other attributes of the rule. Finally you need to specify a series of SupportedElementTypeAttribute to declare the kind of database objects you want to validate, for this example you simply need to specify ISqlColumn because I want only to validate column names.
The important aspect is that all the strings passed to DataRuleAttribute needs to be included in a resource file, and the very first things to do is creating a suitable resource file.
Then you need to populate it with name and description that you want to use in your rule.
Now you can create a file that stores all these constants, for an easier use. As you can see the ResourceBaseName string is the name of the resource file.
And the game is done. Now you only need to write the code that validate column names.
This rule is really simple, but it works. One of the most difficult aspect in writing such an extension, is having a knowledge of the structure used by the database edition to represent objects. An important thing is that you can write everywhere in the rule the line
And when you build the test database project, you can debug visual studio, and you can inspect with the debugger whatever object you need.
With the power of the debugger you can better understand data that is passed to the rule by the designer. To register the rule you need to copy into a specific Visual Studio folder, and register it in the gac. You can accomplish all with a series of post build actions.
With these simple three lines we are copying the dll with the custom rule, register in the gat and copy the xml file with the description of the plugin, that is completely equal to the one used with the old example.
Tags: Visual Studio 2010