Archives / 2010 / April
  • Blog comment spammers … help!

    Tags: Personal

    Hey, I need some advice/help, now I’ve been writing on this blog for a few years, I don’t blog a great deal in comparison to many of my peers and my blog doesn’t get a massive number of hits, BUT I seem to get a fair share of comment spammers!!

    I have image verification on the comments and I have put an Akismet API key into the Geekswithblogs settings, but I still get a bunch of spam comments.

    They could almost be real, until you see the link going off to some dating site or casino crap.

    What does everyone do?? Delete them every time?? Moderate comments??

    I would rather not moderate comments if possible, but is that the only way to stop the crap??


    Technorati Tags: ,,


  • Visual Studio 2010: Custom Start Page

    Tags: Visual Studio

    As Visual Studio 2010 IDE has been mostly written in WPF, extending the start page has become pretty darn easy and I for one find this quite interesting as I always open with the start page and the more customisation I can have the better!

    There are a few things you will need to install first to get going

    • Visual Studio 2010 SDK
    • Start page project template, which you can either get from the New Project dialog, in the online gallery section in VS or download from here

    I was going to write a blog post on how to create a custom start page, but decided that msdn have done such a good job I was pretty much wasting my time, so take a look here, it has in detail everything you need to know to get it done! :)


  • Dynamic connection for LINQ to SQL DataContext

    Tags: .NET 3.5, LINQ, Visual Studio, .NET

    If for some reason you need to specify a specific connection string for a DataContext, you can of course pass the connection string when you initialise you DataContext object.  A common scenario could be a dev/test/stage/live connection string, but in my case its for either a live or archive database.

    I however want the connection string to be handled by the DataContext, there are probably lots of different reasons someone would want to do this…but here are mine.

    • I want the same connection string for all instances of DataContext, but I don’t know what it is yet!
    • I prefer the clean code and ease of not using a constructor parameter.
    • The refactoring of using a constructor parameter could be a nightmare.


    So my approach is to create a new partial class for the DataContext and handle empty constructor in there.

    First from within the LINQ to SQL designer I changed the connection property to None.  This will remove the empty constructor code from the auto generated designer.cs file.


    Right click on the .dbml file, click View Code and a file and class is created for you!


    You’ll see the new class created in solutions explorer and the file will open.

    We are going to be playing with constructors so you need to add the inheritance from System.Data.Linq.DataContext

    1. public partial class DataClasses1DataContext : System.Data.Linq.DataContext
    2.    {
    3.    }

    Add the empty constructor and I have added a property that will get my connection string, you will have whatever logic you need to decide and get the connection string you require.  In my case I will be hitting a database, but I have omitted that code.

    1. public partial class DataClasses1DataContext : System.Data.Linq.DataContext
    2. {
    3.    // Connection String Keys - stored in web.config
    4.    static string LiveConnectionStringKey = "LiveConnectionString";
    5.    static string ArchiveConnectionStringKey = "ArchiveConnectionString";
    7.    protected static string ConnectionString
    8.    {
    9.       get
    10.       {
    11.          if (DoIWantToUseTheLiveConnection) {
    12.             return global::System.Configuration.ConfigurationManager.ConnectionStrings[LiveConnectionStringKey].ConnectionString;
    13.          }
    14.          else {
    15.             return global::System.Configuration.ConfigurationManager.ConnectionStrings[ArchiveConnectionStringKey].ConnectionString;
    16.          }
    17.       }
    18.    }
    20.    public DataClasses1DataContext() :
    21.       base(ConnectionString, mappingSource)
    22.    {
    23.       OnCreated();
    24.    }
    25. }

    Now when I new up my DataContext, I can just leave the constructor empty and my partial class will decide which one i need to use.

    Nice, clean code that can be easily refractored and tested.

    Share this post :