Blog posts


  • 3 must have tools for .net developers

    April 30, 2011 | Tags: ASP.NET, C#, Regular expressions

    ReSharper

    ReSharper from Jetbrain is one of the most useful development tools I've ever come across. As a plugin to Visual Studio it offers code analysis - and suggesting changes to improve the code quality, smart refactoring and much more. You'll find a full list of features here.

    Download ReSharper (30 day trial)


    FxCop

    FxCop from Microsoft is also a great piece of software to increase the build quality of your project. By analyzing your assemblies (DLLs) against a best-practice set of rules it can pin-point problems in your code and offer suggestions.

    The installation is a bit daunting since it require you to download the complete Windows SDK. But it's worth it!

    Information on how to download FxCop


    Regulazy

    Constructing regular expressions can sometimes be really tricky. Wouldn't it be great to have a tool that helps you create a regular expression from a given string and then tweak it? Well that's what Regulazy does.

    Download Regulazy

  • Lazy loading in C# / .NET

    April 20, 2011 | Tags: C#, Snippet
    A quite interesting design pattern is lazy loading which defers the initialization of an object until it's accessed for the first time.

    Lazy loading magic

    In the following sample we will expose an object of our class MyClass with lazy loading:
    private MyClass _myPropertyValue;

    public MyClass MyProperty {
    get { return _myPropertyValue ?? (_myPropertyValue = GetTheValue(123)); }
    }

    Explaining the magic


    ?? in C# is an operator to ensure that a non-null value is passed.

    This example:
    if(_myPropertyValue != null) {
    return _myPropertyValue;
    }
    else {
    return defaultValue;
    }
    ..can easier be expressed as:
    return _myPropertyValue ?? defaultValue;

    What the lazy loading code does is to check if _myPropertyValue is set, if it is the value is returned. If it's not _myPropertyValue is populated with a value by calling the function GetTheValue() and then is the same value returned. The next time the property is accessed _myPropertyValue will have a value and no new function call is needed.

    Notice that (_myPropertyValue = GetTheValue(123)) is surrounded with parenthesis, if ommited the code won't work.

    This way to handle lazy loading is called lazy initialization.

  • Stripped WebControl without all font and color stuff

    April 19, 2011 | Tags: ASP.NET, C#, Snippet
    When developing your own web controls, it can sometimes be quite nice to get rid off all the junk that WebControl includes, such as Font, BackgroundColor etc.

    If your doing simple HTML you can - instead of WebControl - inherit from:
    System.Web.UI.HtmlControls.HtmlControl

    This will however disable some of your possibilities with sub controls and such. You might want to go with System.Web.UI.Control which works for most cases.

    All but one!

    Because if you're writing a templated controll where you have the Container carry data just inhereting from Control won't do. You also need to tell .NET that it should parse it's children by adding the ParseChildren attribute before your class declaration. Like this:
    using System;
    using System.ComponentModel;
    using System.Web.UI;

    namespace MyProject.WebControls {
    [ParseChildren(true), PersistChildren(false)]
    public abstract class MyControl : Control, INamingContainer {
    // Do your magic here :)
    }
    }

  • Sign a non strong named assembly

    April 19, 2011 | Tags: ASP.NET, C#, Troubleshooting
    If your assembly is signed by strong name key file you will have problems calling non strong named assemblies. You can bypass this with reflection, but it adds a couple of other issues.

    There's another possibility. And that is to sign a non strong named assembly using MSIL.

    Here are the steps:

    1. Obtain MSIL of the given non signed assembly:
    C:\ildasm NonSigned.dll /OUT:MyAsm.il
    2. Rename the original assebmly to prevent it from getting overwritten.

    3. Rebuild the assembly from IL.
    C:\ILASM MyAsm.il /DLL /KEY:MyStrongKey.snk

    Credits goes to Jayant D. Kulkarni who posted this in a comment over at CodeProject.

  • A Smarter Way to Include JavaScripts and CSS to Reduce Page Load Times

    April 18, 2011 | Tags: Javascript, ASP.NET, Open source, CodeProject, CSS, C#
    Updated my open source project SmartInclude which handles runtime compression and minifying javascript and stylesheet.

    The project have been constructed to be:
    • Powerful - use well proven compressors from YUI together with automatic GZip to further decrease the file transfer.
    • Easy to use - just replace your script-tags with a custom webcontrol to implement in your project.
    • Easy to develop with - set a single property in your web.config to disable the compression to help debugging scripts.

    The main idea is to have all scripts and css-files in a readible form on the server and compile them to one compressed and minified file when transfered to the visitor in order to decrease bandwidth and most important of all: load time of the page.

    The compressed file is cached client side with a hash stamp that makes the web browser request a new file if it has changed since last request from the server.

    Take a look at the article describing the project:
    SmartInclude article and code at CodeProject

    You can always find the latest code over at smartinclude.codeplex.com.