Tahir Hassan's Blog

My Technical Notes

Saturday, 26 July 2014

F# type with a member constraint

F# is perhaps the only language which supports member constraints. Here is an example of a type (class) which accepts an object satisfying a constraint and then calls a property (Property1): Instead of wrapping the object in another type, we could instead have a static class which has a bunch of static functions which invoke the methods we want to wrap: Note that the best documentation is the MSDN Member Constraints page.

Tuesday, 22 July 2014

Writing a simple LogFactory using F# (with NLog)

Using NLog in F#

The following code instantiates a new Logger object to log to a file "myLogFile.txt" into the temp directory:

Friday, 18 July 2014

Strongly-typed Html.ActionLink

Html.ActionLink is fraught with the possibility that either the controller or action or parameter name(s) have been changed. As a way of making sure that these are always valid, we can use a strongly typed lambda expression.

We first define an ActionLink method which takes in the linkText (a string) and a lambda expression representing the controller, action and parameter(s):

Given a test action TestAction defined in the HomeController class:

We can add to our view:

Wednesday, 2 July 2014

Printing Log Messages with Time in SQL Server

Within stored procedures, PRINT statements appear at the very end when the stored procedure has fully executed. This makes it an unlikely candidate for printing out messages of far the stored procedure has progressed - what we'd rather want is immediate printing of the message.

To this end, I wrote a little stored procedure LogMessage that takes a 'string' as an argument and then prints this together with the time:

To use it: which prints:

Thursday, 19 June 2014

Showing Query Plan Text in SQL Server Managment Studio

Execute the statement:

Thereafter you can always set it to OFF.

Also select the Results to Text option, so it doesn't appear in a table.

See Displaying Execution Plans for more information.

Wednesday, 18 June 2014

Adding CSS Dynamically to a page via Javascript/JQuery

Adding CSS dynamically onto a page can be useful if you are making a simple GreaseMonkey script which changes the css styles to make the text more readable. First define a simple function addCss:

If you do not have JQuery on the page, you can always use vanilla js: Finally we can add any css we want to: