Wednesday, December 16, 2009

C# Coding Standards

I have always been very keen on coding standards and as believe that a strict standard is key to writing maintainable code.
Today i stumbled upon a document that summarizes all the C# coding standards that i usually apply.
Thanks to Lance Hunt for publishing this document!

Get the document here: http://weblogs.asp.net/lhunt/pages/CSharp-Coding-Standards-document.aspx

/Ruud

Tuesday, November 3, 2009

Learn ASP.NET MVC in 2 hours! An introduction to MVC.NET

There is a new sheriff in town and it's called ASP.NET MVC!
This (fairly) new framework from Microsoft provides the possiblity to write applications that truely follow the MVC design pattern which allows clear separation of data, logic and user interface.
That strict seperation results in better testibility and maintanability.
I was very excited about this framework from the start, but didn't get into the details until now and i found that - once again - a blog series from ScottGu is a very good start for those who want to learn to use ASP.NET MVC.

So here we go: learn MVC.NET in 2 hours (depending on how fast you read of course)!

Part 1: ASP.NET MVC Framework
http://weblogs.asp.net/scottgu/archive/2007/11/13/asp-net-mvc-framework-part-1.aspx

Part 2: URL Routing
http://weblogs.asp.net/scottgu/archive/2007/12/03/asp-net-mvc-framework-part-2-url-routing.aspx

Part 3: Passing ViewData from Controllers to Views
http://weblogs.asp.net/scottgu/archive/2007/12/06/asp-net-mvc-framework-part-3-passing-viewdata-from-controllers-to-views.aspx

Part 4: Handling Form Edit and Post Scenarios
http://weblogs.asp.net/scottgu/archive/2007/12/09/asp-net-mvc-framework-part-4-handling-form-edit-and-post-scenarios.aspx

Thank you Scott!

And for those who can't get enough, there is more!
The latest version - ASP.NET MVC 2.0 - is now available as preview and Brad Wilson wrote a blog series about one of the major new features in this release: Templates.

Part 1: Introduction
http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-1-introduction.html

Part 2: ModelMetadata
http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-2-modelmetadata.html

Part 3: Default Templates
http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-3-default-templates.html

Part 4: Custom Object Templates
http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-4-custom-object-templates.html

Part 5: Master Page Templates
http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-5-master-page-templates.html

Thank you Brad!

/Ruud

Thursday, October 22, 2009

.NET trace log implementation with EventLog and TextStream support

When you are developing an application you want to be able to log what is happening in your code.
In .NET you can accomplish that with the use of TraceListeners.
You use the Trace class to write messages during the execution of your application.
While in debug mode, these messages will be displayed in the Visual Studio output console and forwarded to the configured tracelisteners.
The TraceListener can then write the message to a destination like an EventLog, Xml file or a textfile.
One cool thing about TraceListeners is that you can configure them in your App.config or Web.config, so you can control their settings after you have built the application.

I think this is a great tool for implementing logging in your application, but the default implementation has a few setbacks:

1) TraceListeners intercept all trace messages and i would like to configure them so they intercept only those messages that have a specific priority level.
Eg. i want high priority messages to be logged in an eventlog and i want debug messages to be logged in a textfile.
So when i use a release build and something goes wrong, i can activate the TraceListener that writes debug info to a logfile and find out what is happening.

2) The EventLogTraceListener outputs to the Application log and i would like to have my events in a seperate log.

So to solve these problems i wrote my own tracelisteners.
It would be great if Microsoft was so kind to let us inherit from their default tracelistener implementations like EventLogTraceListener and TextStreamTraceListener, but for some reason they decided to seal these classes.
Therefore we have to inherit from the TraceListener class, do our custom stuff and pass the calls to an instance of, for example, EventLogTraceListener.
I wrote two custom tracelisteners: CustomEventLogTraceListener and CustomTextStreamTraceListener.

In my implementation i also wrote a CustomTrace class that acts as a wrapper for the Trace class and adds the possibility of specifying priority levels for trace messages.
The lowest priority is 1 and the highest is 9.
So instead of calling Trace.TraceError("This is wrong") you can call CustomTrace.TraceError("This is wrong", 9) which means this error is of priority level 9, so it is very important.

I created an example solution with the TraceListeners project and an example application.
It shows how to configure the listeners and how to send information (messages) to them.

I will be using this in all my .NET implementations from now on and i hope it comes in handy for you too!

/Ruud

Wednesday, October 21, 2009

Maintenance

Downloads and code highlighting component are temporarily disabled.
They are all hosted on the DotTech domain which is offline for maintenance.
I try to have them back online this weekend.

EDIT: Server is back up!

/Ruud

Tuesday, October 13, 2009

ScottGu's VS2010 and .NET 4 Series

I'd like to point out a nice series of blogposts from Scott Guthrie on Visual Studio 2010 and .NET 4.0
For those of you who don't know Scott Gu (you are a rare breed): he runs the development teams for ASP.NET, CLR, IIS7, Windows Presentation Foundation and some other MS projects.

His blog series about VS2010 and .NET 4.0 is good reading material for those who want to be somewhat prepared when the software is released.

I will update this post when he publishes more articles.
Enjoy!

/Ruud

Tuesday, September 15, 2009

Announcing SharePaste.com

I often found myself in the situation where i quickly want to save whatever is on my clipboard, for example while i'm working on some code and want to remove a section but might need it later.
Sometimes i want to share whatever is on my clipboard, when i want feedback on it or when i want to send someone some code to help him out.

Now, i thought it would be handy to have an online tool for this so that i can quickly paste some text, save it and access it by a short URL so that i can share it with others.
For this reason i created SharePaste, the easy way to save and share your clipboard contents.

SharePaste.com

It provides you with a straight forward interface that allows you to paste and save your text within seconds.
There is support for syntax highlighting and in the future you have the ability to register yourself to keep track of previous pastes.
The tool is still in early beta and registration is not available yet, but it's already very usefull.

Here is a short overview of SharePaste's features:
- Save any text that is on your clipboard
- Share your pastes using their unique, short URL
- Access your saved pastes any time
- Apply syntax highlighting to your pastes
- Find your pastes back by searching on their tags (not yet implemented)
- More features are coming!

And here is one example of what a paste looks like: http://sharepaste.com/p/giD3B7.

I hope you like it and find it as usefull as i do!

/Ruud

Monday, August 31, 2009

Format DateTime in a dropdownlist (with different CultureInfo)

It turned out to be very easy, but it took me at least 30 minutes to figure out the easiest way to do it.
So i figured it's worth a little blog post.

The magic happens by using the DataTextFormatString property of the dropdownlist.
But i didn't know how to format the date properly until i figured out i had to use {0}.
Here is how to do it:

// Create a list with dummy dates
List<DateTime> dates = new List<DateTime>();
for (double i = 1; i <= 31; i++)
{
dates.Add(DateTime.Now.AddDays(i));
}

// Specify the dateformat (e.g. outputs today as "31-08-2009 18:40")
ddlMyDropdown.DataTextFormatString = "{0:dd-MM-yyyy hh:mm}";

// Databinding
ddlMyDropdown.DataSource = dates;
ddlMyDropdown.DataBind();

Note that .NET uses the culture of the current thread to format the date.
So if i used format {0:dd MMMM yyyy} which would output as "31 August 2009" and i want to write it out in another culture, let's say dutch (31 augustus 2009).
Then i need to execute the following code before i perform the databinding:

// Set current culture for current thread to dutch
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("nl-NL");

Hope this saves you some time googling for it ;)

/Ruud

Friday, August 21, 2009

Call document OnLoad after an AJAX.NET request

When you use your own JavaScript combined with AJAX.NET, you might bump into the problem that your document OnLoad event is not fired after AJAX requests.
This can be solved by using AJAX.NET's endRequest event.
Please note that i use jQuery, so you will need that in order for this example to work.

var documentReady = function() {
// here you do all the stuff you would normally do in $(document).ready()
}

$(document).ready(documentReady);

// This will call the documentReady() function every time an AJAX request is finished
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(function() {
documentReady();
});

This can of course be used to call any function after an AJAX request.

2009-08-25: I just found out that you can also use add_pageLoaded() instead of add_endRequest().

/Ruud

Thursday, August 20, 2009

jQuery removeOptionByValue plugin for dropdown list

I needed to remove an option with a specific value from a select element with JavaScript.
It seems that jQuery lacks something like a removeOptionByValue() method.
I tried to use a selector to remove one option, like this: $("#mySelect option[@value=ToBeRemoved]").remove() but that resulted in an error.
So i wrote a little plugin for it.

This is the example HTML:

<select id="ddlTest">
<option value="">- Please choose -</option>
<option value="first">First option</option>
<option value="second">Second option</option>
<option value="third">Third option</option>
</select>

<input id="btnTest" value="Test" type="button">

We would like to remove the option with value "first" when the button is clicked.
This is the javascript i wrote:

(function($) {
$.fn.removeOptionByValue = function(optionValue) {
if ($(this).is("select")) {
var itemIndex = -1;
var dropdownId = $(this).attr("id");

$("#" + dropdownId + " option").each(function() {
itemIndex++;
if ($(this).val() == optionValue) {
$("#" + dropdownId)[0].remove(itemIndex);
}
});
}
}
})(jQuery);

// Test if it works
$("#btnTest").click(function() {
$("#ddlTest").removeOption("first");
});

Download the solution as jQuery plugin here: jquery.removeoptionbyvalue.js

/Ruud

Wednesday, August 19, 2009

window.open resulting in "Invalid argument" error in IE

I kept getting an Invalid Argument error in IE while trying to execute window.open() in JavaScript.
Appearantly in IE you cannot use spaces in the name you give to the window.

So...

window.open("mypage.html", "my window name");

...does not work, while...

window.open("mypage.html", "mywindowname");

...works fine.

/Ruud

Friday, August 14, 2009

TechSite CMS

When i started dotTech i decided to write my own .NET based Content Management System.
My goal was to expand my knowledge of .NET and to be able to offer my customers an easy-to-use content management solution.
I think the result is a steady base for an easily expandable CMS with great possiblities.
As interest towards this CMS grows, i decided to make a blog post about it.


TechSite explained
The CMS is item-based and the user interface is build around a treeview that represents the website's content structure.
From the tree the items can be added, changed, renamed, removed and moved.
When adding an item, the user can choose between different item templates (for example, a product item or a news item).
Item templates consist of regions (e.g. content, specifications) and regions constist of fields (see screenshot).
Fields can be of different types, for example single line text, html text, image or a date.
The current version contains 8 fieldtypes and these are very easy to expand.
How the item templates are treated on the frontend depends on the implementation and is independent of the backend.
That is one of the things i really like about this solution: i can provide the customer with a fully working backend before i start with the implementation of the frontend.

A quick peek at the highlights and features:
- Based on .NET 2.0
- Support for all major browsers (IE6-8, FF2-3, Safari, Chrome)
- Built on .NET DataProviders for future support of other databases
- Uses MooTools for all clientside scripting
- Integrated FCKEditor for user-friendly content editing
- Supports friendly Urls
- Provides an API for development of the frontend

Future
TechSite's future development relies on the needs of my customers.
The CMS is provided for free and the customer pays only for the implementation and for the development of custom features.
So as the usage of TechSite expands, the possiblities grow with it.

A few features that i expect to see in the future:
- PHP connector to enable linux-based customers to use TechSite for their website
- More advanced File Explorer
- Out-of-the-box implementations
- Multi-language support

Demo
There is an online demo of the backend available here: http://techsite.dottech.nl/techsite/default.aspx.
All the save actions (such as adding, updating, (re)moving) have been disabled for the demo!
The demo comes with a simple (unfinished) website which is available here.

EDIT: Demo is temporarily offline due

Last words
Like i said before, TechSite is a steady base for a CMS, but it's not yet a final product so keep that in mind while checking out the demo.
I will keep track of TechSite's development here on my blog.
Feel free to leave any comments or questions you have about TechSite here.

If you would like to use TechSite for your own website, you can contact me by email.

/Ruud

Wednesday, July 29, 2009

JavaScript TreeView component using MooTools library

Some time ago i wrote this TreeView component using the MooTools javascript library.
As far as i know there are not many treeviews around for MooTools, so i decided to put it up for grabs here.

Key Features:
- Load tree data from JSON file
- Load child nodes on demand using AJAX
- Select multiple nodes

View the demo here.
Download DotTech Tree v1.0.1: dottech.tree-1.0.1.zip.

UPDATE: Unfortunately the code is not available at the moment (i lost it and didnt have time to search for it yet)

/Ruud

Tuesday, July 28, 2009

PHP var_dump() method for C#

In PHP there is the var_dump() and print_r() function that displays the contents of an object.
Sometimes i miss this functionality in ASP.NET, especially when you are working with large objects and don't want to step into your code every time.
I came up with this method to list all the properties of a given object by name and value.
It also lists any child properties recursively, until it reaches a certain level of recursion (in this case it stops after 5 recursions).

using System;
using System.Text;
using System.Reflection;
using System.Collections;

public string var_dump(object obj, int recursion)
{
StringBuilder result = new StringBuilder();

// Protect the method against endless recursion
if (recursion < 5)
{
// Determine object type
Type t = obj.GetType();

// Get array with properties for this object
PropertyInfo[] properties = t.GetProperties();

foreach (PropertyInfo property in properties)
{
try
{
// Get the property value
object value = property.GetValue(obj, null);

// Create indenting string to put in front of properties of a deeper level
// We'll need this when we display the property name and value
string indent = String.Empty;
string spaces = "| ";
string trail = "|...";

if (recursion > 0)
{
indent = new StringBuilder(trail).Insert(0, spaces, recursion - 1).ToString();
}

if (value != null)
{
// If the value is a string, add quotation marks
string displayValue = value.ToString();
if (value is string) displayValue = String.Concat('"', displayValue, '"');

// Add property name and value to return string
result.AppendFormat("{0}{1} = {2}\n", indent, property.Name, displayValue);

try
{
if (!(value is ICollection))
{
// Call var_dump() again to list child properties
// This throws an exception if the current property value
// is of an unsupported type (eg. it has not properties)
result.Append(var_dump(value, recursion + 1));
}
else
{
// 2009-07-29: added support for collections
// The value is a collection (eg. it's an arraylist or generic list)
// so loop through its elements and dump their properties
int elementCount = 0;
foreach (object element in ((ICollection)value))
{
string elementName = String.Format("{0}[{1}]", property.Name, elementCount);
indent = new StringBuilder(trail).Insert(0, spaces, recursion).ToString();

// Display the collection element name and type
result.AppendFormat("{0}{1} = {2}\n", indent, elementName, element.ToString());

// Display the child properties
result.Append(var_dump(element, recursion + 2));
elementCount++;
}

result.Append(var_dump(value, recursion + 1));
}
}
catch { }
}
else
{
// Add empty (null) property to return string
result.AppendFormat("{0}{1} = {2}\n", indent, property.Name, "null");
}
}
catch
{
// Some properties will throw an exception on property.GetValue()
// I don't know exactly why this happens, so for now i will ignore them...
}
}
}

return result.ToString();
}

For my example i used an object called Category, but this can be an instance of any class.
Category has a few basic properties and one called Parent which holds another Category object (so we have child properties).
I call the method like this:

Category category = LoadCategory(123);
Response.Write(var_dump(myObject, 0)); // Always start at recursion 0,
// you might want to add an override
// that sets this argument by default

The output of this example is:

CategoryId = 685
ParentId = 287
Name = "Beans"
Description = ""
Parent = MyProject.Data.Category
|...CategoryId = 287
|...ParentId = 174
|...Name = "Cheap food"
|...Description = ""
|...Parent = MyProject.Data.Category
| |...CategoryId = 174
| |...ParentId = 173
| |...Name = "Food"
| |...Description = ""
| |...Parent = MyProject.Data.Category
| | |...CategoryId = 173
| | |...ParentId = 0
| | |...Name = "Things to eat"
| | |...Description = ""
| | |...Parent = null
| | |...Count = 0
| |...Count = 0
|...Count = 13
Count = 0
TestList = System.Collections.Generic.List`1[MyProject.Data.Category]
|...TestList[0] = MyProject.Data.Category
| |...CategoryId = 1
| |...ParentId = 0
| |...Name = "Bla"
| |...Description = ""
| |...Count = 0
|...Count = 1

It's not exactly PHP's var_dump() method, but it works for me! :)
I might expand it some day so it dumps array/list elements too and maybe add some XHTML formatting.

Update on 29-07-2009:
Added support for collection properties.

/Ruud

Reseed the identity column of a table in SQL Server

When you want the identity field of a table to be reset (start with id 1 again), execute the following SQL query:

DBCC CHECKIDENT (tableName, reseed, 0)

Where "tableName" is replaced by the name of your table and the 0 is the new seed.

/Ruud

Target elements with ids that contain special characters using jQuery

Let's say you have some HTML elements with ids like this one:

<div id="form~myform$div~mydiv[1]#value"></div>

And you would like to target this element using jQuery.
This will not work because of the special characters used in the element's id.
If you execute the following jQuery:

$("div").each(function() {
var divId = this.id;
var jQueryFound = $('#' + this.id).length;

alert("Div id is '" + divId + "' and jQuery found " + jQueryFound);
});

The result is: Div id='form~myform$div~mydiv[1]#value' and jQuery found 0.
JQuery cannot find the element id because it contains meta-characters that jQuery parses as part of the selector.
To solve this problem i wrote a method which escapes the special characters so jQuery parses them as part of the id.

var jEscape = function(jquery) {
jquery = jquery.replace(new RegExp("\\$", "g"), "\\$");
jquery = jquery.replace(new RegExp("\~", "g"), "\\~");
jquery = jquery.replace(new RegExp("\\[", "g"), "\\[");
jquery = jquery.replace(new RegExp("\\]", "g"), "\\]");
jquery = jquery.replace(new RegExp("#", "g"), "\\#");
return jquery;
};

Now execute the following code to see that it works:

$("div").each(function() {
var divId = this.id;
var escapedId = jEscape(this.id);
var jQueryFound = $('#' + escapedId).length;

alert("Div id is '" + divId + "' which is escaped to '" + escapedId + "' and jQuery found " + jQueryFound);
});

The result is: Div id is 'form~myform$div~mydiv[1]#value' which is escaped to 'form\~myform\$div\~mydiv\[1\]\#value' and jQuery found 1.

If you want to escape all the possible jQuery meta-characters, expand the method to escape these: ;&,.+*':"!^()=>|/

/Ruud

Monday, July 27, 2009

Generic XML deserializer method

For one of my projects i needed to deserialize several XML files to objects of different data types.
Therefore i wrote this generic deserialization method:

public static T Deserialize(XmlReader xmlReader, string rootElement)
{
T result;

XmlRootAttribute root = new XmlRootAttribute(rootElement);
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T), root);
result = (T)xmlSerializer.Deserialize(xmlReader);

return result;
}

To use it, you first load your XML file into an XmlReader object and call the method like this:

MyTargetType deserialized = Deserialize(xmlReader, "startnode");
(ignore the type name being upper case in my example, it's caused by the code highlighter)

I will post some more later about how you can let your application use its configuration to decide what data type specific XML files should be deserialized to.
This can be very usefull when you have an application that deserializes multiple XML feeds and you want the list of feeds to be configurable outside of the code.

/Ruud

Remove all non-letter characters from a string

Problem: You have a string that may contain various special characters and you need it to contain only letters and digits.
...and you don't want to use regular expressions ;-)

Solution: There are problably a hundred ways to do this, but if you are using C# 3.0 or higher, there is a very quick solution that costs you just one line of code.
First i will show the slightly larger version of the code snippet which explains the workings better:

string dirtyString = "http://www.dottech.nl/#?this%20is%20dirty!";
string cleanString = String.Empty;

// Convert the string to a character array so we can load it into a List
char[] dirtyChars = dirtyString.ToCharArray();

// Create a character list and load the dirtyChars array
List<Char> characters = new List(dirtyChars);

// Use the List's ForEach method to iterate through the characters
characters.ForEach(delegate(char c)
{
if (Char.IsLetter(c))
{
cleanString += c.ToString();
}
});

return cleanString; // "httpwwwdottechnlthisisdirty"

Now, when i wrote this i figured it had to be a little more compact.
So i used a Lambda Expression which is a new feature in C# 3.0 and changed the ForEach line to this:

characters.ForEach(c => cleanString += (Char.IsLetter(c)) ? c.ToString() : String.Empty);

And after some more compacting i ended up with the final snippet:

new List<Char>(dirtyString.ToCharArray()).ForEach(c => cleanString += (Char.IsLetter(c)) ? c.ToString() : String.Empty);

Although it might look complex to someone who sees that line of code for the first time, i still think it's a pretty maintainable line of code as long as you put some comments above it explaining how dirtyString is the input and cleanString the output.

/Ruud

Welcome to Ruud.Tech, a blog for software engineers

Since i regularly stumble upon and come up with nifty code snippets and solutions which might be usefull for other software engineers, i have decided to start this blog and collect them here.

Enjoy!

/Ruud