Archives

Archives / 2011 / October
  • Inner Fields and Lazy Initialization in C#

    Using lazy initialization in C#, a class’s state is set up such that each property’s get method performs a check to see if the underlying field is null.  If it is, then it calculates or populates the field before returning it.  This is a very simple and common approach, but it requires that the class follows a convention of only accessing the field via the property.  Unfortunately, there are no language features that can enforce this, so it’s possible for errors to creep in.  Here’s an example of this approach working correctly: public class Order { // other properties   private Customer _customer; public Customer … more

  • RazorEngine Performance and Template Caching

    I’ve been using RazorEngine on a project and have been impressed with its simplicity and ease-of-use.  However, the performance of the application isn’t quite where I need it to be, and I was pretty sure the issue was with how I was using RazorEngine, especially since I could anecdotally see that the processor consumption on the machine running the app was quite high, and looking at the running tasks it was clear that most of that was a result of csc.exe (C# compiler) activity. A bit of searching found this discussion thread on codeplex regarding the re-compiling of existing templates.  The issue is a known one with version 2.1, which is the current release and what I’m … more

  • Install Application as Service on Windows Server 2008

    You can use the sc.exe command to install an EXE as a service on Windows Server 2008.  There’s a good article on creating an application that can easily run as either a console app or as a service here.  From an administrator command prompt, the syntax is something like this: sc \\servername create MyService.ServiceName binpath= d:\services\Foo\Foo.exe displayname= MyService.ServiceName Note that for this particular utility, the command line options include the “=” sign in them, so you must have no space before the “=” and you must have a space after the “=”.   Also the server name must be preceded by \\ to work. Assuming it works, you should see something like this: … more

  • Using If This Then That to Automate Your Life

    I’ve just started getting into If This Then That (ifttt.com) and have set up a simple task that will update my Steve Smith Facebook Page whenever I post something here to my blog.  Getting this set up was extremely simple to do, and so far although I’ve only been using it for about an hour I’m quite impressed and enamored with ifttt.com.  Here’s how easy it is to set up something like this: 1. Register with ifttt.com This took 30 seconds. 2. Create a task If this (the trigger): Then (the action to take – create a new Link Post on my Facebook Page): End Result: This is actually the first post I’m making since adding this task, so if this post shows up on my … more

  • Working with Lazy Loading in Entity Framework Code First

    Entity Framework 4 has Lazy Loading built-in and enabled by default.  Here’s a quick bit of code to show you how to work with this feature.  To get started with this, simply create a new Console Application and in nuget (Package Manager Console), run this command: install-package EntityFramework.Sample This will install a simple blog post example.  Copy and paste the following into your Program.cs file (replace everything): using System; using System.Collections.Generic; using System.Linq; using System.Text; using EFExample.Models;   namespace EFExample { class Program { static void … more