Building class constructors with ease using CodeRush

Imagine a class with lot of fields.

 

 Now, you would want to build a constructor that initializes all or many of the fields by assigning arguments to coresponding fields so you don't need to initialize all fields one by one, plus you have the guarantee that all fields are initialized. Makes sense, right. Now, how would you write such a constructor? You might just use all of your 20 fingers and type it. Pretty annoying, isn't it. Afterall all the information is already there.

So, meet one of the CodeRush's feature I love: Smart Constructor. Just move the caret to the point where you want your constuctor created and type cc [SPACE] (assuming default CodeRush settings). A window like this will appear:

You can uncheck fields at will. After you are done just press [ENTER].

Isn't this feature just lovely? CodeRush just sucks the metadata from the declaration and creates a constructor with all arguments you've selected. It certainly saves a lot of annoying typing for me and prevents me to type wrong assignments. And this is just one of the million CodeRush's features.

Tst

C Sharp

Problems signing an assembly with a key stored in key container (VS2005)

This is one of those problems you can't understand how could it happen.

Some background: In the past Visual Stuidio versions you were able to sign an assembly using a key stored in a file (snk) or in a safe store named key container. There were two assembly scoped attributes named AssemblyKeyFile and AssemblyKeyName respectively. I prefered the later since I didn't have to deal with file location and the data is stored in a safe place.

Now, here comes Visual Studio 2005 with a project property page for signing assemblies (Signing tab in Project Properties) which has a check box whether to sign assembly or not and a combo box that locates the file being used for signing. And if you wonder how can you sign the assembly using key container you are out of luck - no option there.
Now you say, no problem, I will sign it the old way using assembly scoped attribute. Actually you still can, but you'll get a warning (AssemblyKey* attributes are deprecated):

Warning 1 Use command line option '/keycontainer' or appropriate project settings instead of 'AssemblyKeyName' E:\Visual Studio 2005\Projects\MyProject\Properties\AssemblyInfo.cs 36 12 MyProject

Hmm. Ok, since I don't like warnings, I will go passing that /keycontainer parameter through one of Project Properties page. But you can't. Again, no option there.

Fortunately there is a simple workaround which involves modification of csproj file by hand. You should add this node right under the first <PropertyGroup>node:

<KeyContainerName>YourKeyName</KeyContainerName>

(found via this thread).

Happy signing.

Caching of database schema in CodeSmith

So you've build a template (or using one) for CodeSmith that uses SchemaExplorer.DatabaseSchema class - the one that enables you browsing through database schema with ease (through a nice API) and a killer feature when dealing with databases. There is one catch though:

The first time you run a template it takes a little longer to execute while the next time (if you didn't change it in between). The reasons for this behaviour are two:

  • CodeSmith Professional does template code caching (it compiles the template only once if not closed or changed). This is good for batching - it saves you time.
  • Perhaps less know is the fact that it does caching of database schema, too.

The later fact might be a problem. Imagine you run a template that does database modifications (creates a SQL that you run). Now, the second run will produce the exact same script not taking in account that database was meanwhile changed. Don't worry though as solution is simple. Just invoke this method somewhere at the beginning of the template code and you are fine:

[your instance of DatabaseSchema].Refresh();