Rory Primrose

Learn from my mistakes, you don't have time to make them yourself

View project on GitHub

Dynamic searching in TSQL

As I have been writing some SQL Server based software recently, I want to expose a standard way of providing searching capabilities for tables and views. The initial implementation started with a set of WHERE conditions like the following:

WHERE (FieldA = @FieldAValue OR @FieldAValue IS NULL)
(FieldB = @FieldBValue OR @FieldBValue IS NULL)

I now want to extend this functionality to include sorting and perhaps paging as well. I came across this article by Erland Sommarskog (SQL Server MVP) which address a ton of sorting variations.

Read More

Running an asynchronous workflow with impersonation

I encountered a bit of a curly one today with my workflows.

Generally, I am executing WF workflows synchronously because I am using them as my business layer implementation for distributed services. This means that in order to return a value from a service call, the workflow needs to complete first. Because WF executes asynchronously by default, I am using the ManualWorkflowSchedulerService to execute workflows on the same thread as the calling process.

This became a problem when I actually want a combination of synchronous and asynchronous workflow executions for a service call. With the DefaultWorkflowSchedulerService and ManualWorkflowSchedulerService in WF, this wouldn’t be supported in the one workflow runtime instance.

My first solution was to host two runtimes, one using the ManualWorkflowSchedulerService for executing workflows synchronously, and one using the DefaultWorkflowSchedulerService for executing the asynchronous workflows. The hitch is that I also need impersonation, but this doesn’t appear to be possible when executing workflows using the DefaultWorkflowSchedulerService as the impersonated credentials get lost.

Read More

Object cache keys and equality

For some bizarre reason (meaning I really don’t know why), I have always built cache key values as strings when the key being represented is a set of values. On Friday, my eyes were opened to the fact that it is much better to create a rich object that represents the key values. The question then come down to equality testing of the reference type to make sure that the cache keys are correctly identified. Understanding Equality in C# on CodeProject is a concise article that was helpful for getting this right.

Read More

Bitten by UserData

I have written some custom activities for WF, but have recently found a bit of a problem with my implementation. I have wanted to persist information against the workflow instance that contains my custom activity (or many instances of the custom activity). I thought I was being very clever by resolving the owning workflow and then storing my information against the workflows UserData IDictionary property.

Even the description of the UserData property sounded appropriate according to my good intentions.

Gets an IDictionary that associates custom data with this class instance.

The problem that came up was that the data put into this property was persisted between multiple calls to execute the workflow type in the workflow runtime across any user that called it. I have had to change my implementation to store the data elsewhere to avoid this issue.

Read More

WF and missing build output from referenced project

Ever come across a WorkflowValidationFailedException at runtime even though the project containing the workflow was successfully validated and compiled without any errors? The reason for this occurring is how the compiler manages references.

Lets look at a simple solution that contains ProjectA, ProjectB and ProjectC. ProjectA references ProjectB which then references ProjectC.

ProjectA contains a form that references ClassB in ProjectB like this:

Read More

Code generation rules

Paul Stovell has posted some great points about the usage of code generation. This is of great interest to me because I am using code generation to produce a base service implementation as I develop an SOA application. I have a couple of thoughts to add to his list:

7. Code generation is not a license to avoid unit testing.

There is a tendency to not test generated code because the original unit testing of the templates was successful. Once you have generated code, it should now be considered alive in that it will be run in an environment, interact with other systems and will be required to work with data that didn’t exist in the code generation templates or their original test scenarios. Anything could cause problems because specific circumstances weren’t considered as the template was developed.

8. Code generation does not produce a product.

This point aligns with Paul’s 5th and 6th points. Code generation being a tool, shouldn’t produce a product. Use code generation to output common base level code that can then be customized and built upon. Code generation output can be the basis of a product, but shouldn’t be the entire product. If you have done this, then either you have wasted too much time on your templates, or your product design needs some serious CPR.

On a side note, the code generator I decided to use SmartCode because it is the easiest to use and understand in the freeware market. It is also open source so I can deal with any issues or restrictions that I come across. I have since also started to contribute my changes to the project.

Read More

What are the best refactor and profiler tools for VS2005?

If you have some experience with refactoring or profiling tools (preferably with experience of multiple tools), I would be really interested to know what you think.

For refactoring tools, the pick seem to be either JetBRAINS ReSharper or DevExpress RefactorPro. ReSharper is $250 and seems to have similar functions to RefactorPro which weighs in at $99 with free updates for a year, or $250 if bundled with CodeRush. There is also JustCode and C# Refactory but these both seem to be less mature compared to ReSharper and RefactorPro.

Read More

Misfire on the keyboard

Every now and then, and sometime more often than that, I happened to not have my fingers on the right keys. The results are usually terrible, but can sometimes be kinda cool. I just tried typing “similar” and came up with “sunukar” instead. Maybe that should be the name of my next piece of software.

Following in one of the trends of Microsoft, I have been naming software according to town names. Could this be the new method of generating code names? Just bash the keyboard in semi-random positions.

Read More