Saturday, October 23, 2010

Code contracts

Interesting article by Jon Skeet about code contracts.

Saturday, October 2, 2010

Export changes from Subversion with SharpSvn

private static void ExportRevisionRange(
Uri repositoryUrl,
long fromRevision,
long toRevision,
string exportFolder)
{
var client = new SvnClient();
var from =
new SvnUriTarget(repositoryUrl, fromRevision);
var to =
new SvnUriTarget(repositoryUrl, toRevision);
Collection<SvnDiffSummaryEventArgs> list;
client.GetDiffSummary(from, to, out list);

foreach (SvnDiffSummaryEventArgs item in list)
{
if (item.DiffKind == SvnDiffKind.Deleted ||
item.NodeKind != SvnNodeKind.File)
continue;
var target = new SvnUriTarget(item.ToUri);
string exportPath =
Path.Combine(
exportFolder,
item.Path.Replace("/", "\\"));
var fi = new FileInfo(exportPath);
if (!fi.Directory.Exists)
fi.Directory.Create();
client.Export(
target,
exportPath,
new SvnExportArgs { Revision = toRevision });
}}

Friday, September 17, 2010

Windows XP-programmer i Windows 7

Denne artikel er en dansk version af denne artikel: Running Windows XP-only apps on Windows 7

Hvordan man i Windows 7 kan køre programmer der ellers kun kan køre på Windows XP (trin som er specifikke for Windows 7 Home er beskrevet her).

Scott Hanselman beskriver her hvordan man kan køre Windows XP-programmer i Windows 7:
Windows 7 - Seamless Apps in Windows Virtual PC (Virtual XP) and Application Compatibility

Men Hanselman beskriver ikke hvad man skal gøre hvis man ikke har adgang til at downloade Windows XP Mode, som fx når man har Windows 7 Home.

Denne artikel beskriver hvordan man kan installere Windows Virtual PC, installere en ny virtuel maskine med Windows XP, installe et program der ellers kun kører på Windows XP, og køre dette program fra Windows 7:

1) Download og installér Windows Virtual PC:
Windows Virtual PC

2) Hvis du kører en maskine uden "hardware virtualization", Windows Virtual PC vil ikke køre, så installér dette fix fra Microsoft, enten:
a) på 32 bit systemer: 32 bit fix
b) på 64 bit systemer: 64 bit fix

3) Kør Windows Virtual PC og opret en ny virtuel maskine.

4) Installér og start Windows XP på den virtuelle maskine.

5) Installér "Integration Functions" via den indbyggede disk.

6) På den virtuelle maskine: installér Windows XP-programmet.

7) Luk den virtuelle maskine ved at klikke på krydset (X), hvilket vil sætte maskinen i dvale.

8) I Windows 7 Start menuen kan du finde navnet for programmet og klikke på det. Den virtuelle Windows XP maskine vil starte i baggrunden og starte dit program op som om det kørte i Windows 7.

For flere detaljer bag nogle af trinnene, se Hanselmans artikel:
Windows 7 - Seamless Apps in Windows Virtual PC (Virtual XP) and Application Compatibility

Running Windows XP-only apps on Windows 7

How to run Windows XP-only applications on Windows 7 (particular steps necessary for Windows 7 Home are described here):

Scott Hanselman describes how to run Windows XP apps nicely in Windows 7:
Windows 7 - Seamless Apps in Windows Virtual PC (Virtual XP) and Application Compatibility

However, Hanselman does not cover when you have no access to the Windows XP Mode download.

This article describes how to install Windows Virtual PC, install a new virtual machine with Windows XP, install your Windows XP-only application, and run it from Windows 7:

1) Download and install Windows Virtual PC:
Windows Virtual PC

2) If you are running a machine without hardware virtualization, Windows Virtual PC will not run, so install this fix from Microsoft, either:
a) on 32 bit systems: 32 bit fix
b) on 64 bit systems: 64 bit fix

3) Run Windows Virtual PC and create a new virtual machine.

4) Install and start Windows XP on the virtual machine.

5) Install Integration Functions via the built-in disk.

6) Install the Windows XP application that you want to run on Windows 7.

7) Close the virtual machine by clicking the X, which will make the machine hibernate.

8) In Windows 7 Start menu you can find the name of your application and click it. The Windows XP virtual machine will start in the background and bring your application up as if it was running on Windows 7.

For more details behind some of the steps, see Hanselman's article:
Windows 7 - Seamless Apps in Windows Virtual PC (Virtual XP) and Application Compatibility

Monday, August 16, 2010

Browsing Subversion with SharpSvn

Using SharpSvn to browse a Subversion repository:
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using SharpSvn;

public IEnumerable<string> GetSubBranches(string baseUrl)
{
SvnClient client = new SvnClient();
SvnTarget target = new SvnUriTarget(baseUrl);
var list = new Collection();
client.GetList(target, out list);
return list.
Where(l => !string.IsNullOrEmpty(l.Path)).
Select(l => l.Path);
}

Sunday, August 15, 2010

Personal wiki

Last month I read Pragmatic Thinking and Learning, and have since picked up the suggestion that I should keep a personal wiki of ideas, notes, research, and whatever other things I usually scribble down or drop in a text file. I use it daily now and I find it makes more sense now to keep all the notes, when it's easier to search and navigate between the notes.

On the technical side, I use TiddlyWiki, which is a single html file with a lot of javascript to make things smooth.

Wednesday, July 21, 2010

A meditation plan

Monday
  • Focus on object
  • Visualize travel
Tuesday
  • Investigate thoughts
  • Visualize flowers in four corners
Wednesday
  • Focus on breathing
  • Visualize achievement
Thursday
  • Visualize healing
  • Inner guide
Friday
  • Unify with upper self
  • Compassion
Saturday
  • Unity with God
  • Focus on inner body
Sunday
  • Focus on senses
  • Visualize a golden warmth

Wednesday, July 14, 2010

Kata: SimpleMembership

I did a coding exercise with ASP.NET Membership, with focus on managing users and roles in ASP.NET MVC 2:

Features include:
  • Add users with user name, email address, and password
  • List of users
  • Add roles
  • List of users in roles
  • Add users to roles
  • Update user's email address, approved status, and password


New learnings for me and possibly interesting things to draw from this:
  • The use of MultiSelectList in view models and views
  • The flexibiliy of the built-in Membership
  • How easy it really is to add these administration pages...


The project is added to Google code: kata-simplemembership

The source can be obtained or browsed here:

Monday, June 28, 2010

Merge between branches more than one level apart

Task: merge between trunk and branches that are more than one level away.

Setup:
trunk
|
|- branch1
|
|- branch2

Solution: When branch1 has been merged to trunk and the same changes have been merged from branch1 to branch2, and all revisions from trunk have been (nominally) merged to branch1 and branch2, it is possible to reintegrate branch2 in trunk without conflicts.

Example of procedure to reproduce this:

1) trunk with file readme.txt
2) trunk |branch> branch1
3) branch1: edit readme.txt
4) branch1 |branch> branch2
5) branch2: edit readme.txt
6) branch1: edit readme.txt
7) branch1 |merge reintegrate> trunk
8) branch1 |merge range of revisions> branch2
9) trunk |merge range of revisions> branch1
10) trunk |merge range of revisions> branch2
11) branch2 |merge reintegrate> trunk

Sunday, May 30, 2010

Exception formatting

public StackFrame ExtractPrimaryFrame(
  Exception exception)
{
  if (exception.InnerException != null)
  {
    return ExtractPrimaryFrame(exception.InnerException);
  }

  return ExtractPrimaryFrame(new StackTrace(exception, true));
}

public StackFrame ExtractPrimaryFrame(
  StackTrace stackTrace)
{
  if (stackTrace == null ||
    stackTrace.GetFrames() == null)
  {
    return null;
  }

  foreach (var frame in stackTrace.GetFrames())
  {
    if (frame.GetFileName() != null)
      return frame;
  }

  return null;
}

public string FormatException(Exception exception)
{
  StackFrame primaryFrame = ExtractPrimaryFrame(exception);
  return string.Format("{0} || {1}",
    FormatFrame(primaryFrame),
    exception);
}

public string FormatFrame(StackFrame frame)
{
  if (frame == null)
  {
    return string.Empty;
  }

  return string.Format(
    "FileName: {0}; Line: {1}; FilePath: {2}; Full info: {3}",
    ExtractFileName(frame.GetFileName(), "src") ?? "(none)",
    frame.GetFileLineNumber(),
    frame.GetFileName() ?? "(none)",
    frame.ToString());
}

private static string ExtractFileName(string filePath)
{
  if (string.IsNullOrEmpty(filePath))
    return null;
  FileInfo fi = new FileInfo(filePath);
  return fi.Name;
}

public string ExtractFileName(string filePath, string folderStart)
{
  if (string.IsNullOrEmpty(filePath))
    return null;
  FileInfo fi = new FileInfo(filePath);
  DirectoryInfo dir = fi.Directory;
  while (true)
  {
    if (dir.Parent == null || dir.Name == folderStart)
      break;
    dir = dir.Parent;
  }

  if (dir.Parent == null)
    return ExtractFileName(filePath);
  return fi.FullName.Replace(dir.FullName + "\\", "");
}

Saturday, March 20, 2010

Testing multiple implementations

Challenge: Run automated tests of multiple implementations of an interface, for example one repository for Sql Server and another for MySql.

A solution: Have one test class for one of the implementations, and create a derived test class for the other implementation, which injects the proper implementation in the constructor of the first test class.

Example with NUnit:

[TestFixture]
public class CustomerRepositoryTests
{
   private ICustomerRepository _repository;

   public CustomerRepositoryTests() :
      this(new CustomerRepository())
   {
   }

   public CustomerRepositoryTests(ICustomerRepository repository)
   {
      _repository = repository;
   }

   [Test]
   public void Add_ValidCustomer_Ok()
   {
      // Arrange:
      var customer = new Customer();

      // Act:
      _repository.Add(customer);
   }

}

[TestFixture]
public class MySqlCustomerRepositoryTests :
   CustomerRepositoryTests
{
   public MySqlCustomerRepositoryTests() :
      base(new MySqlCustomerRepository())
   {
   }
}

Wednesday, March 17, 2010

Manual definition of dependent tables with LinqToSql

The following is a code example of how to declare dependent tables in C# and generate and populate them with LinqToSql [it will generate the tables Parent(Id, Name, ChildId) and Child(Id, Name)]:

using System;
using System.Data.Linq;
using System.Data.Linq.Mapping;

namespace ManualLinqToSql
{
   class Program
   {
      static void Main(string[] args)
      {
         const string connectionString =
            @"Server=.\SqlExpress;Database=LinqTest;Integrated Security=true;";
         TestRepository repository =
            new TestRepository(connectionString);
         try
         {
            repository.CreateDatabase();
            repository.Add("A", "A1");
            repository.Add("B", "B1");
            Console.WriteLine("OK");
         }
         catch (Exception ex)
         {
            Console.WriteLine(ex);
         }
         Console.ReadKey();
      }
   }

   public class TestRepository
   {
      private readonly TestDataContext _dataContext;

      public TestRepository(string connectionString)
      {
         _dataContext = new TestDataContext(connectionString);
      }

      public void Add(string parentName, string childName)
      {
         var parent = new Parent(parentName, childName);
         _dataContext.ParentTable.InsertOnSubmit(parent);
         _dataContext.SubmitChanges();
      }

      public void CreateDatabase()
      {
         _dataContext.DeleteDatabase();
         _dataContext.CreateDatabase();
      }
   }

   public class TestDataContext : DataContext
   {
      public TestDataContext(string connectionString)
         : base(connectionString)
      {
      }

      public Table<Parent> ParentTable
      {
         get { return GetTable<Parent>(); }
      }

      public Table<Child> ChildTable
      {
         get { return GetTable<Child>(); }
      }
   }

   [Table]
   public class Parent
   {
      private EntityRef<Child> _child;

      public Parent()
      {
      }

      public Parent(string name, string childName)
      {
         Name = name;
         Singleton = new Child { Name = childName };
      }

      [Column(IsPrimaryKey = true, IsDbGenerated = true)]
      public int Id { get; set; }

      [Column(CanBeNull = false)]
      public string Name { get; set; }

      [Association(Storage = "_child", ThisKey = "ChildId", IsForeignKey = true)]
      public Child Singleton
      {
         get
         {
            return _child.Entity;
         }
         set
         {
            _child.Entity = value;
            ChildId = value.Id;
         }
      }

      [Column(UpdateCheck = UpdateCheck.Never)]
      private int ChildId { get; set; }
   }

   [Table]
   public class Child
   {
      [Column(IsPrimaryKey = true, IsDbGenerated = true)]
      public int Id { get; set; }

      [Column(CanBeNull = false)]
      public string Name { get; set; }
   }

   public void Detach()
   {
      _child = default(EntityRef<Child>);
   }
}

Sunday, March 7, 2010

MSBuild: Passing arrays between targets

In MSBuild, if you have a target that operates on a list of items of a specific type, and you want to call that operation several times with different lists, here is a way to do that using the DependsOnTargets attribute:

<Target Name="UserInterface">
  <MSBuild Projects="$(MSBuildProjectFile)" Targets="Service"
    Properties="ServiceDependsOn=PersonRepository-1" />
  <MSBuild Projects="$(MSBuildProjectFile)" Targets="Service"
    Properties="ServiceDependsOn=PersonRepository-2" />
</Target>

<Target Name="Service" DependsOnTargets="$(ServiceDependsOn)">
  <Message Text="Persons: @(Persons->'%(Name) (%(Identity))')" />
</Target>

<Target Name="PersonRepository-1">
  <ItemGroup>
    <Persons Include="Al"><Name>Al Omaha</Name></Persons>
    <Persons Include="Ben"><Name>Ben Patterson</Name></Persons>
  </ItemGroup>
</Target>

<Target Name="PersonRepository-2">
  <ItemGroup>
    <Persons Include="Adrian"><Name>Adrian Quist</Name></Persons>
    <Persons Include="Britta"><Name>Britta Ruud</Name></Persons>
  </ItemGroup>
</Target>


and here is sample output:

Project "C:\Projects\MSBuildItems.xml" on node 0 (default targets).
Project "C:\Projects\MSBuildItems.xml" (1) is building "C:\Projects\MSBuildItems.xml" (1:2) on node 0 (Service target(s)).
 Persons: Al Omaha (Al);Ben Patterson (Ben)
Done Building Project "C:\Projects\MSBuildItems.xml"
(Service target(s)).

Project "C:\Projects\MSBuildItems.xml" (1) is building "C:\Projects\MSBuildItems.xml" (1:3) on node 0 (Service target(s)).
 Persons: Adrian Quist (Adrian);Britta Ruud (Britta)
Done Building Project "C:\Projects\MSBuildItems.xml"
(Service target(s)).

Done Building Project "C:\Projects\MSBuildItems.xml" (default targets).

MSBuild with dependency injection

I am about to start refactoring a MSBuild file with around 1000 lines of xml, and I find that the targets are heavily dependent on each other.

Maybe dependency injection can help here?!

In MSBuild this could look something like this:

UserInterface.xml:
<Import Project="ApplicationService.xml" />
<Import Project="Repository.xml" />
(or <Import Project="FakeRepository.xml" />)
<Target Name="UserInterface-Action">
  <CallTarget Targets="ApplicationService-Action" />
</Target>


ApplicationService.xml:
<Target Name="ApplicationService-Action">
  <CallTarget Targets="IRepository-Action" />
</Target>


Repository.xml:
<Target Name="IRepository-Action">
  <Message Text="Repository action" />
</Target>


FakeRepository.xml:
<Target Name="IRepository-Action">
  <Message Text="Fake repository action" />
</Target>


For comparison, here is the corresponding structure in C#:
public class UserInterface
{
  private readonly IRepository repository;

  public UserInterface(IRepository repository)
  {
    this.repository = repository;
  }

  public void Action()
  {
    var service = new ApplicationService(this.repository);
    service.Action();
  }
}

public class ApplicationService
{
  private readonly IRepository repository;

  public ApplicationService(IRepository repository)
  {
    this.repository = repository;
  }
  
  public void Action()
  {
    this.repository.Action();
  }
}

public interface IRepository
{
  void Action();
}

public class Repository : IRepository
{
  public void Action()
  {
    System.Console.WriteLine("Repository action");
  }
}

public class FakeRepository : IRepository
{
  public void Action()
  {
    System.Console.WriteLine("Fake repository action");
  }
}