Monthly Archives: July 2008

NHibernate SessionFactory configuration

My preferred way for configuring NHibernate is as following:

  • Create a hibernate.cfg.xml file for session-factory settings.
  • Add mapping files, named type.hbm.xml, as embedded resources to the library project that implements the repository.

When i looked at the documentation i found the following:

Another alternative (probably the best) way is to let NHibernate load all of the mapping files contained in an Assembly:

Configuration cfg = new Configuration();
  .AddAssembly( "NHibernate.Auction" );

Whenever i tried that code i received an InvalidOperationException: Could not find the dialect in the configuration. In order to make the configuration work the way i prefer you have to do the following:

Configuration cfg = new Configuration().Configure();
cfg.AddAssembly(typeof(MyRepository).Assembly);

Another attention point is the fact that i believe that all connectionStrings belong in App.Config. This can be realised by using the connection.connection_string_name attribute:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
 <session-factory>
  <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property
  <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
  <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
  <property name="connection.connection_string_name">AdventureWorks</property>
 </session-factory>
</hibernate-configuration>

Error loading testrunconfig: Failed to instantiate type Microsoft.VisualStudio.TestTools.WebStress.WebTestRunConfig

Earlier this week i ran into the following exception when opening a solution: “Error loading TestRunConfig1.testrunconfig: Failed to instantiate type Microsoft.VisualStudio.TestTools.WebStress.WebTestRunConfig”.

screenshot of testrunconfig not expected format dialog box

Apparently (here and here) the VS2005 Developer edition is missing a couple of libraries that the VS2005 Tester edition adds to the testrunconfig. If your tests don’t depend on these, the simplest way to solve this problem is to remove all the values nodes, and their childnodes, where the type is defined in the Microsoft.VisualStudio.QualityTools.LoadTest, Microsoft.VisualStudio.QualityTools.WebTest and Microsoft.VisualStudio.QualityTools.LoadTest.WebStress assemblies.

About the Specification pattern

A couple of days ago i mentionned Yves Goeleven’s blog as a reference for solutions using DDD principles. Let’s have a look at his implementation of the AndSpecification and OrSpecification in Design Patterns – The Specification Pattern – Part I:

class AndSpecification<t> : CompositeSpecification<t>
{
 public override bool IsSatisfiedBy(T candidate)
 {
  bool isSatisfied = true;
  foreach( ISpecification<t> spec in Specifications)
  {
   isSatisfied &= spec.IsSatisfiedBy(candidate);
  }
  return isSatisfied;
 }
}
class OrSpecification<t> : CompositeSpecification<t>
{
 public override bool IsSatisfiedBy(T obj)
 {
  bool isSatisfied = false;
  foreach( ISpecification<t> spec in Specifications)
  {
   isSatisfied |= spec.IsSatisfiedBy(obj);
  }
  return isSatisfied;
 }
}

I believe that it would be better if the implementations had the same lazy evaluation behaviour as the C# && and || operators. Eg: in C# one can write (a && b) and if a evaluates to false, then b is not evaluated anymore. Consider the following code:

if (person != null && person.Age > 18) { }

Let’s rewrite that same code using Specifications:

class NotNullSpecification<t> : ISpecification<t>
 where T : class
{
 public bool IsSatisfiedBy(T item)
 {
  return item != null;
 }
}

class OlderThanSpecification : ISpecification<person>
{
 private int age;

 public OlderThanSpecification(int age)
 {
  this.age = age;
 }

 public bool IsSatisfiedBy(Person person)
 {
  return person.Age > this.age;
 }
}

class PersonNotNullAndOlderThan18Specification : AndSpecification<person>
{
 public PersonNotNullAndOlderThan18Specification()
 : base(new NotNullSpecification<person>(), new OlderThanSpecification(18))
 {
 }
}

Using the mentionned AndSpecification implementation the following unittest will fail:

[TestMethod]
public void TestPersonNotNullAndOlderThan18Specification()
{
 Person person = null;

 ISpecification<person> specification = new PersonNotNullAndOlderThan18Specifictation();
 Assert.IsFalse(specification.IsSatisfiedBy(person));
}

In order to make that test pass we could rewrite the specifications as following:

public abstract class CompositeSpecification<t> : ISpecification<t>
{
 private readonly IEnumerable<ispecification<t>> specifications;

 public CompositeSpecification(IEnumerable<ispecification<t>> specifications)
 {
  this.specifications = specifications;
 }

 protected IEnumerable<ispecification<t>> Specifications
 {
  get { return this.specifications; }
 }

 abstract public bool IsSatisfiedBy(T item);
}

public class AndSpecification<t> : CompositeSpecification<t>
{
 public AndSpecification(IEnumerable<ispecification<t>> specifications)
  :base(specifications)
 { }

 override public bool IsSatisfiedBy(T item)
 {
  foreach (ISpecification<t> specification in this.Specifications)
  {
   if (!specification.IsSatisfiedBy(item))
   {
    return false;
   }
  }

  return true;
 }
}

public class OrSpecification<t> : CompositeSpecification<t>
{
 public OrSpecification(IEnumerable<ispecification<t>> specifications)
  : base(specifications)
 { }

 override public bool IsSatisfiedBy(T item)
 {
  foreach (ISpecification<t> specification in this.Specifications)
  {
   if (specification.IsSatisfiedBy(item))
   {
    return true;
   }
  }

  return false;
 }
}

.NET Domain-Driven Design with C#: Problem – Design – Solution

Since i didn’t find many reviews on Tim McCarthy’s book: .NET Domain-Driven Design with C#: Problem – Design – Solution, i’ve decided to write a short one myself:

This book offers a real-world example of a project using DDD. If you already have experience with DDD you will read pretty quickly through the chapters.

Here are a couple of items in the book i found noteworthy:

  • The IAggregateRoot marker interface that is used as a constraint on the IRepository<T> interface.
  • Code that demonstrated the power of WPF through the implementation of Model-View-ViewModel and usage of the ICommand.
  • Used the IUnitOfWork.Commit method to plug a Synchronization Service into the application.

All in all, the book is worth the 26$. In case that you’re looking for more examples, i would recommend Domain Driven Design – Table of contents and source code as another source of inspiration…

Presenting a generic DiscreteValuesRange

Let me start with a real world example demonstrating the usefulness of a generic DiscreteValuesRange. Imagine that i run a grid computing business and my clients want to book capacity on the grid for a given period. Before their booking is approved, i have to verify that the client has contracts that allow him to use the system for each day of the booking period. Usually, such a check is implemented as following:

bool CheckEachDayIsCovered(Booking booking, Client client)
{
 DateRange periodToCheck = booking.Period;
 DateTime endOfTime = new DateTime(9999, 12, 31);

 DateTime dayToCheck = periodToCheck.Begin;
 while (dayToCheck <= periodToCheck.End)
 {
  bool dayIsCovered = false;

  foreach (Contract contract in client.Contracts)
  {
   if (contract.Period.Includes(dayToCheck))
   {
    dayIsCovered = true;

    if (contract.Period.End < endOfTime)
    {
     dayToCheck = contract.Period.End.AddDays(1);
    }
    else
    {
     return true;
    }
   }
  }

  if (!dayIsCovered)
  {
   return false;
  }
 }

 return true;
}

After a while clients want to buy licenses for only a couple of hours instead of a full day. I realise that my check can remain the same, but that the concept “EndOfTime” has become DateTime(9999, 12, 31, 23, 59, 59) and that i can advance only an hour instead of a day. Since i want to reuse my code i define the IDiscreteValueProvider<T> interface (See Discrete Space):

interface IDiscreteValuesProvider<t>
{
 T GetNextValue(T value);
 T MaxValue { get; }
}

After this i’m able to implement a DiscreteValuesRange<T> that implements the following interface (See: Generic Range):

interface IDiscreteValuesRange<t> : IRange<t>
{
 bool IsCoveredByRanges(IEnumerable<idiscreteValuesRange<t>> ranges);
}

Now, with all this infrastructure i can rewrite my original method as following:

bool CheckEachDayIsCovered(Booking booking, Client client)
{
 return booking.Period.IsCoveredByRanges(client.GetContractPeriods());
}

class Client
{
 ...
 IEnumerable<range<dateTime>> GetContractPeriods()
 {
  foreach (Contract contract in this.Contracts)
  {
   yield return contract.Period;
  }
 }
}

Feel free to download all this infrastructure: DiscreteRange.zip and use it for your next coverage check.