For quite a few years I’ve got into the habit of storing application config files externally to web.config using the configSource attribute, as I’m sure most people do.

Just to recap this means that you can have a set of development, test and live configuration files and just change your web.config to point to whatever environment you’re deploying to.  Advantage being that configuration like database connections which vary according to the environment they are deployed for are contained in an obvious folder like dev or test or live.

So in toepoke.co.uk <blatantPlug/>I have something akin to the following:

webRoot\config\dev\dbConnections.config
webRoot\config\test\dbConnections.config

and on my dev box in my web.config I have

<connectionStrings configSource="config\dev\dbConnections.config" />

Nothing very new so far.  However, what I don’t like about this though is that the config folder is kind of in the middle of everything in the solution, coupled with the fact that toepoke is an MVC app, and I keep opening the Content folder instead … which makes me swear … lots …

Now ASP.NET has a special folder called App_Data that has additional permissions when deployed so the content isn’t available by URL (so nasty people can’t get to it).  You use this for application data (funnily enough :) ) like a local database, or some XML lookup files you may have.

So (and you can probably guess where I’m going with this!) config files are application data, so it makes sense to put them into the App_Data folder and gain the extra protection it offers (granted .config files aren’t available though the URL either, so this is just additional protection).  So now the file structure looks like:

webRoot\App_Data\config\dev\dbConnections.config
webRoot\App_Data\config\test\dbConnections.config

and in the web.config:

<connectionStrings configSource="App_Data\config\dev\dbConnection.config" />

Which I think just looks much, much neater.  And it all works, nothing more than a copy and paste and change some paths.

Hope this is useful to someone !

I treat myself to one of these (think this is a later model, but pretty much the same telly) a few months back to replace my ageing CRT which was on it’s last legs.  Anyway it came with a plethora of connections, 3 HDMIs, couple of SCARTs, but no SVIDEO :( .  It did however have a PC IN connection, which is basically a VGA input.

Always had it in my head that SVIDEO would be best, but fortunately it turns out that VGA is better (not as good as HDMI, but sadly my laptop doesn’t have one).

Anyway got myself a cheap VGA cable from Argos and connected it up, brought up the display settings, and extended the desktop … and saw a very small section of my desktop on the telly :(

What I got first time I tried connecting

I tried faffing with the telly settings, repositioning the screen, etc and didn’t get anywhere.  In fact I went backwards as I ended up with just a blank screen.  Not to be out done I did what everyone with a problem does these days and asked Google :) .  Initial hit looked promising but in the end was telling me to do what I was already doing :( !

So to get the punchline I had a bit of inspiration and thought perhaps the graphics driver was interferring, so popped up Start > Control Panel > Additional Options > NVIDIA Control Panel, changed the source on the telly to PC and job’s a good ‘un :) .

woohoo

Of course being VGA only the picture goes to the telly, so for the sound a twin phono to 3.5mm stereo jack from Maplin should do the trick :)

Hope this helps someone else out.

Working on toepoke today and I wanted to loop through a list of objects and sync up some properties, so I started off by looping through a normal foreach construct. 

I vaugely remember there was a ForEach method hanging off the intellisense for list objects, so a quick google revealed … not much (granted I didn’t do a extensive search … I’m very much in the ”Not on the first page … I’ll figure it out myself!” camp :-) ).  Anyway seems like a good opportunity for a blog.

So what example to use as an illustration?  What about a noddy Person object with the following properties:

  • Name – useful for seeing what’s going on
  • DOB – which we’ll generate when create a list of Person
  • Age – which is the property we’re going to loop around and calculate.

Which gives us the following noddy class:

public class Person
{
 public string Name { get; set; }
 public DateTime DOB { get; set; }
 public int Age { get; set; }
}

We’re going to drive this example through a unit test (see full excert below), but I’ll just concerntrate on the relevant bits here.  So next up let’s populate a list of people to create our test data.

List<Person> people = new List<Person>() {
 new Person() { Name = "Fred Flintstone", DOB = DateTime.Now.AddYears(-65) },
 new Person() { Name = "Homer Simpson", DOB = DateTime.Now.AddYears(-40) }
};

Simple enough, couple of cases, one aged 65, the other aged 40.  Our simple ForEach will loop through and work out the age of each instance.  To do this we’ll just take the DOB away from the current time to delivery the number of years. 

The ForEach method is simply a delegate (which in essence is just a function) which may or may not take any arguments.  Ours isn’t taking any arguments (it’s a simple example after all :-) ).  The code looks like this:

people.ForEach(
 person => { person.Age = (DateTime.Now - person.DOB).TotalYears(); }
);

Now we’re working against a list of type Person which is the person => bit.  The right hand side inside the curly brackets is in basically a method created on the fly for the purposes of the ForEach method.  So in essence the above is saying “for each person in the list of person set the age to the total number of years between the person’s date of birth and now”.  Doesn’t look to scary now does it :-) ?

The more observant of you will have noticed that TotalYears doesn’t exist in a TimeSpan object.  And you’d be absolutely correct; sadly the TimeSpan object doesn’t provide a TotalYears method because it’s quite complex to work out due to leap years and … erm … other stuff that’s hard to work out ;-) .  So to help out with our example, it’s time for a little extension method on the TimeSpan class:

public static class TimeSpan_Extensions
{
 public static int TotalYears(this TimeSpan ts)
 {
  return (int)ts.TotalDays / 365;
 }
}

I would recommend you don’t use that method for any production code whatsoever, I will guarantee bugs!  But it’s perfect for our purposes. 

Hopefully you’ve found this useful, and the full source is posted below, good luck and good searching.

using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Blog.Tests
{
 public static class TimeSpan_Extensions
 {
  public static int TotalYears(this TimeSpan ts)
  {
   return (int)ts.TotalDays / 365;
  }
 }
 [TestClass]
 public class ForEach
 {
  public class Person
  {
   public string Name { get; set; }
   public DateTime DOB { get; set; }
   public int Age { get; set; }
  }
  [TestMethod]
  public void Can_Calculate_Age_Using_ForEach()
  {
   List<Person> people = new List<Person>() {
    new Person() { Name = "Fred Flintstone", DOB = DateTime.Now.AddYears(-65) },
    new Person() { Name = "Homer Simpson", DOB = DateTime.Now.AddYears(-40) }
   };
   // Yup we could use ForEach here too, but I don't want to confuse what we know have to use (foreach) with what we don't (ForEach)!
   foreach (Person p in people) {
    Assert.AreEqual(0, p.Age);
   }
   // loop through each person and set the Age property to their calculated age
   people.ForEach(
    person => { person.Age = (DateTime.Now - person.DOB).TotalYears(); }
   );
   // element 0 is Fred Flintstone 
   Assert.AreEqual(people[0].Age, 65);
   // element 1 is Homer Simpson
   Assert.AreEqual(people[1].Age, 40);
   
   // wooo, another ForEach sample, just spitting out the results
   people.ForEach(
    a => { 
     TestContext.WriteLine(
      string.Format("Person ({0}, {1}, {2})", a.Name, a.DOB.ToShortDateString(), a.Age)
     ); 
    } 
   );
  }
  private TestContext testContextInstance;
  /// <summary>
  ///Gets or sets the test context which provides
  ///information about and functionality for the current test run.
  ///</summary>
  public TestContext TestContext
  {
   get
   {
    return testContextInstance;
   }
   set
   {
    testContextInstance = value;
   }
  }
 }
}</pre>
</span></span></span></span><span style="color: #0000ff;"><span style="color: #000000;"><span style="color: #0000ff;"><span style="color: #000000;">

I was on hols in Spain this year (nice, except for the 26 hours coach trip :( , but that’s another story) and quite fancied getting a lilo (those little inflatable things you lie on in the sea … not last in last out for any nerds reading :) ).

Thing is our hotel was a good 20 minute walk from the sea and I really didn’t fancy carrying the bloomin’ thing for that amount of time (though many of the residents did … mad!), particulary as I had a rucksack full of techie books to carry that I had no intention of reading …

Anyway, got me thinking … consider.

  1. There’s loads of shops selling lilos near the beach.
  2. There’s loads of lazy people like me who can’t be bothered carrying them to the beach and back.
  3. If you buy one at the start of the week, are you really going to carry it back home … nope you’re gonna dump it somewhere you naughty boy/girl.

Now here’s the thing.  A lilo was costing about €5, so I figure if you own a shop by the sea, let me rent a lilo for one euro with a €5 deposit, if I burst it you’ve got the deposit (i.e. you’ve made your sale) and you only have to rent it out 5 times before you’ve made the equivalent of a sale.  I don’t have to carry the thing around, you don’t have to go to the wholesaler as often to stock up, less lilos need to be produced so the environment is better off.

Job’s a good ‘un.

Hope you enjoyed my first post … hopefully next time it will actually be about www.toepoke.co.uk

Follow

Get every new post delivered to your Inbox.