A collection of learnings and opinions.

Wednesday, December 1, 2010

Inversion of Control with Dependency Injection

Dependency Injection (DI) is a way of implementing the general Inversion of Control (IoC) -pattern. We do this to make our classes independent of each other. Combined with a preference for programming against interfaces instead of directly referencing the implementing classes we can achieve what is called loose coupling. The implementation details of one class have minimal impact on the innards classes using it. Additionaly using Dependency Injection makes your classes easier to unit-test, as they expose their dependencies externally and you can pass in mocks and stubs instead of full implementations.

It is easier to understand the pattern by looking at code-examples. We're creating code for a coffee-shop, and we're looking at the implemetation of a barista (the scrawny, wierd guy who makes the coffee). In my examples I will not show the interfaces, and just assume that all methods are defined in them.

Our barista has one method, where he uses his experience, a nice, big coffe-maker and some extras to concoct a potion:

   1:      public class Barista : IBarista {
   2:          private const int _yearsOfExperience = 5;
   3:          private IEducation _major = new LiberalArtsMajor();     
   4:   
   5:          public IBeverage MakeMeAn(IOrder order) {
   6:              var knowledge = new Experience(_yearsOfExperience, _major);
   7:              var plan = knowledge.HowDoIMake(order);
   8:   
   9:              var coffeeMaker = new BigAstra();
  10:   
  11:              var beverage = coffeeMaker.MakeBeverageAccordingTo(plan);
  12:   
  13:              var otherStuff = new AssortedCreamsAndSyrups();
  14:   
  15:              var additives = knowledge.WhatShouldIAddAccordingTo(plan);
  16:              beverage.Add(additives);
  17:   
  18:              return beverage;
  19:          }
  20:      }

This piece of code is pretty readable, so it's not the worst code in the world. However, it is tighlty coupled to several other classes. Firstly it binds to the LiberalArtsMajor -class (which is apt, but still not a good thing) to set its private field _major. Inside the method itself it binds to the classes Experience, BigAstra and AssortedCreamsAndSyrups. All of these object have to be created for the Barista to make a lovely, warm, black and brown elixir of wakefullnes. Thus these direct couplings make our Barista brittle.

Experience tells us that our classes should be as small and simple as possible, and that they should have one and only one reason to change. It is not far-fetched to imagine the BigAstra class acquiring constructor parameters requiring the caller to provide a source of water, a source of power and a repository of coffee-beans (as this fits neatly with what it would need in real-life).

   1:      public class BigAstra : ICoffeeMaker {
   2:          public BigAstra(IWaterSource water, IPowerSourceAt220Hz power,   ICoffeeRepository godlyBeans) {
   3:              // something very clever and difficult...
   4:          }
   5:          // the rest is left as an exercise for the reader
   6:      }

This change means that we will have to change the implementation of our barista because the implementation of the coffee-maker he is using changed. Furthermore, this change in how you make a BigAstra coffe-maker will cascade into all the places where the BigAstra is used. The same is true for all the other classes used by our Barista and in fact all the classes in the system. Good tools can certainly help us deal with this to a certain extent, but we quickly find ourselves in what I call combinatorial hell. Our barista has too many reasons to change - he is too tightly coupled to what he uses. How can we improve on this?

One solution to this problem I've seen used is to forgo object-orientation altogether and make the classes into a simple collection of static methods. By doing this we are binding ourselves tightly to one implementation, and refusing ourselves powerful object-oriented techniques like inheritance and polymorphism. In essence we are creating a Singleton implementation of the object we need. This technique transports us back into imperative programming in a fell swoop. Please, please do not do this! If you want to read more about why this is a bad idea I recomment Steve Yegge's "Singleton Considered Stupid" -post, and for a shorter view you can read Rasmussen's answer on StackOverflow. Seriously, kittens die and hamsters get palsy when we do this.

How can we solve our combinatorial problem without throwing 30 years of object-oriented goodness on the pyre? By reversing the control through dependency injection, that's how (how's that for a tagline?). This is not a new technique, in fact you've already seen it in this post. When our BigAstra changed its constructor to expose what it needed (its dependencies) it enabled Dependency Injection. There are several flavours of Dependency Injection, but common to them all is that they expose what the class depends on to the outside, allowing its behaviour to be modified without changing its innards. This is also known as the open / closed principle, which says the class should be open to extension but closed to modification. By exposing its dependencies BigAstra is also explicitly signalling that while it needs these things to function it does not accept responsibility for creating them (which follows from the single responsibility principle).

Some ways to acheive DI are to expose the dependencies using a Factory -pattern, expose them through properties / public fields (property injection), require them as constructor parameters (constructor injection) or we could use a Service Locator to build them up runtime (I'm sure there are other ways that slip my mind right now).

I strongly prefer using constructor injection, and also exposing any dependencies that have to change run-time to property injection. When I set up the object-graph I prefer to use a Service Locator which lets me declare how to construct objects when needed and call this when required. In the best of all possible worlds this should be exactly once in every application's lifetime.

We are going to change our barista to support DI by pulling all of his dependencies out into the class and exposing them through the constructor. The coffee-maker and the condiments may need to be replaced in the barista's lifetime (though baristas tend to seem quite frail, and cooffee-machines always look strong and shiny), so we will expose these to property injection as well as constructor injection.

   1:  public class Barista : IBarista {
   2:      private const int _yearsOfExperience = 5;
   3:      private IEducation _major;
   4:      private IExperience _attitude;
   5:      public ICoffeeMachine ShinyThing { get; set; } // Property Injection
   6:      public ICondiments Flourish { get; set; }      // Property Injection
   7:      
   8:      // Constructor Injection
   9:      public Barista(IEducation educatedAs, IExperience relevantExperience, 
  10:                     ICoffeeMachine toolOfTrade, ICondiments sugarAndSpice) {
  11:          _major = educatedAs;
  12:          _attitude = relevantExperience;
  13:          ShinyThing = toolOfTrade;
  14:          Flourish = sugarAndSpice;
  15:      }
  16:   
  17:      public IBeverage MakeMeAn(IOrder order) {
  18:          var plan = _attitude.HowDoIMake(order);
  19:          var beverage = ShinyThing.MakeBeverageAccordingTo(plan);
  20:          var additives = Flourish.WhatShouldIAddAccordingTo(plan);
  21:          beverage.Add(additives);
  22:          
  23:          return beverage;
  24:      }
  25:  }

It is now apparent what our barista depends upon by just looking at the constructor's parameters. Actually, there's something a bit off here. Is the barista really dependent upon his education? Isn't he merely using it as a part of his experience? When we explicitly expose our dependencies like this we'll often find such overlapping (and even conflicting) dependencies. This is a good opportunity for some cleanup, and we end up with a slightly cleaner barista as follows:

   1:  public class Barista : IBarista {
   2:      private IExperience _attitude;
   3:      public ICoffeeMachine ShinyThing { get; set; }
   4:      public ICondiments Flourish { get; set; }
   5:   
   6:      public Barista(IExperience questionableExperience, ICoffeeMachine toolOfTrade,
   7:                     ICondiments sugarAndSpice) {
   8:          _attitude = questionableExperience;
   9:          ShinyThing = toolOfTrade;
  10:          Flourish = sugarAndSpice;
  11:      }
  12:   
  13:      public IBeverage MakeMeAn(IOrder order) {
  14:          var plan = _attitude.HowDoIMake(order);
  15:          var beverage = ShinyThing.MakeBeverageAccordingTo(plan);
  16:          var additives = Flourish.WhatShouldIAddAccordingTo(plan);
  17:          beverage.Add(additives);
  18:   
  19:          return beverage;
  20:      }
  21:  }  

We are happy with this - our barista is ready to have his dependencies set on creation, two of them can even be changed run-time (this is not thread-safe). The barista has no knowledge of how to set up a coffee-machine, how to get the condiments or how to make the experience he got. He doesn't know or care about you or your band, he just makes killer Cortados.

Haven't we just moved our problem, though? Mustn't whosoever calls the barista now need to know how to set up all the barista's dependencies? In our case the barista is used by a poor, underpaid Cashier. She used to look like this:

   1:  public class Cashier : ICashier {
   2:      public IBeverage GetDrinkForCustomer(ICustomer currentCustomer){
   3:          var order = ConstructAnOrderFrom(currenCustomer.Demands);
   4:          
   5:          var jerk = new Barista();
   6:          var drink = jerk.MakeMeAn(order);
   7:          
   8:          return drink;
   9:      }
  10:  }

After we've meddled with the Barista's constructor to appease our false gods of object-orientation she now has to look like this abomination?

   1:  public class Cashier : ICashier {
   2:      private const int _yearsOfExperience = 5;
   3:      
   4:      public IBeverage GetDrinkForCustomer(ICustomer currentCustomer){
   5:          var order = ConstructAnOrderFrom(currenCustomer.Demands);
   6:          
   7:          var experience = new Experience(_yearsOfExperience, 
   8:                                          new LiberalArtsMajor());
   9:          
  10:          /probably has some municipality and plumber dependencies
  11:          var waterTap = new WaterTap(...);
  12:          //probably some municipality and electrician dependencies
  13:          var powerSource = new PowerSource(...); 
  14:          //probably depenent on suppliers and service getting container from stock
  15:          var coffeeBeans = new CoffeStore(...); 
  16:          var coffeMakerForJerk = new BigAstra(waterTap,
  17:                                  powerSource,
  18:                                  coffeeBeans);
  19:          
  20:          // dependent on suppliers I guess?
  21:          var condiments = new AssortedCreamsAndSyrups(...); 
  22:          
  23:          var jerk = new Barista(experience, 
  24:                                 coffeMakerForJerk,
  25:                                 condiments);
  26:          var drink = jerk.MakeMeAn(order);
  27:          
  28:          return drink;
  29:      }
  30:  }

Ouch! The answer to this is obviously to push the dependencies of our cashier into her constructor, making the next level up the chain responsible for the instantiation. But this seems like a reductio-ad-absurdum, as we'll have to have a god-class on top setting up our entire object-graph. That can't be good, can it? Well, yes and no. What we want is a Service Locator, or in our case an IoC container. I like StructureMap, as it's easy to work with and lets me define in a declarative way how it should work. It is one of the oldest of its kind in the .Net world, and time-proven.

To get StructureMap's help we have to tell it how to make the objects we are going to require. StructureMap has the concept of a Registry, which lets us declare what to do when asked for types in a declarative way, like "When asked for A you should return B". StructureMap is smart enough to inspect the constructors of the Bs it can return, and if the constructor requires something it knows how to make it puts that into it. This works well as long as you have defined all the types you need (the configuration is valid), and there are no circle-dependencies (ie. Foo requires a Bar, which requires a Foo). A registry for our coffee-shop might look like this:

   1:  public CoffeeShopRegistry : Registry {
   2:      //could be gotten from a config-file or somesuch
   3:      private const int _yearsOfExperience = 5; 
   4:      
   5:      public CoffeShopRegistry() {
   6:          ForRequestedType<ICoffeeShop>()
   7:              .TheDefaultIsConcreteType<CoffeShop>();
   8:          ForRequestedType<ICashier>()
   9:              .TheDefaultIsConcreteType<Cashier>();
  10:          ForRequestedType<IBarista>()
  11:              .TheDefaultIsConcreteType<Barista>();
  12:          
  13:          ForRequestedType<IEducation>()
  14:              .TheDefaultIsConcreteType<LiberalArtsMajor>();
  15:          ForRequestedType<ICoffeeMaker>()
  16:              .TheDefaultIsConcreteType<BigAstra>();
  17:          ForRequestedType<IWaterSource>()
  18:              .TheDefaultIsConcreteType<WaterTap>();
  19:          ForRequestedType<IPowerSourceAt220Hz>()
  20:              .TheDefaultIsConcreteType<MainsLine>();
  21:          ForRequestedType<ICoffeeRepository>()
  22:              .TheDefaultIsConcreteType<SomeCoffeStorage>();
  23:          
  24:          ForRequestedType<IExperience>()
  25:              .TheDefault.Is
  26:              .ConstructedBy(context => 
  27:                  new Experience(_yearsOfExperience, 
  28:                                 context.GetInstance<IEducation>()));
  29:      }
  30:  }

StructureMap won't initialize any objects until asked to, so defining the types our barista depend on after the barista itself is not a problem as long as the internal graph of StructureMap contains all it needs when it is called upon to provide an IBarista. StructureMap can also do other, cool things - like managing the lifetime of the objects, but that's a subject for another post.

We want to kick off StructureMap as one of the very first things in our application, and preferrably we'd only like to call upon it to construct something once (or as few times as possible). A possible Main would be:

   1:  public CoffeShopProgram {
   2:      public static void Main(string[] args) {
   3:          ObjectFactory.Initialize(context => 
   4:              context.AddRegistry(new CoffeeShopRegistry()));
   5:          
   6:          // Exception if we've missed anything
   7:          ObjectFactory.AssertConfigurationIsValid();
   8:          
   9:          // depends on having a cashier and a barista - MAGIC!
  10:          var shop = ObjectFactory.GetInstance<ICoffeShop>(); 
  11:          
  12:          shop.Open();
  13:          while (shop.IsOpen) {
  14:              var customer = ObjectFactory.GetInstance<ICustomer>();
  15:              shop.ServeCustomer(customer);
  16:          }
  17:      }
  18:  }

We're setting up ObjectFactory by adding the registry we made, in effect telling it what types it should know about, and how to return something when called upon. We haven't seen any ICoffeeShop implementors, but let's just say that is a class depending on an ICashier for its constructor. StructureMap knows how to make an ICashier - that's an instance of the Cashier -class. But Cashier depends on an IBarista! No problem, StructureMap knows that's a Barista instance, etc.

Implementing Dependency Injection loosens our classes' coupling against other classes, in effect delegating that to somewhere else. The actual calling of constructors is done by our IoC -container, freeing us up to simply declare that we want an IBarista, and assuming one will be created for us. So, instead of needing to know the intimate details of the implementing classes' constructors and dependencies to use an IBarista we just have to ask for one.

And, that's all kinds of cool!

Thursday, May 20, 2010

Getting a WeekNumber from a Date in SSRS 2005 and 2008

Many reports have to deal with week numbers. Often you'll have a DateTime to work with, either from a dataset or a parameter.

So, how do we convert this lovely little DateTime to a WeekNumber? Search for that online and you'll get some solutions suggesting you change your query to give you a weeknumber, some solutions will do some fancy (and mostly erroneous) math to calculate the WeekNumber and yet others suggest you just don't do that.

Getting the WeekNumber from your DataSet really is what you want, but then again you don't really want your DataSource to calculate the WeekNumber in all instances. The reason for that is related to the reason why DateTime does not have a WeekNumber:

What, really, is a weeknumber? Why, silly, it's the number of the week the day is a part of.

That's true, but how are the weeks numbered? Do they start with week 0 or week 1? If the week numbering starts at week 1 the last week should be week 53, or should it be week 52 (a week with more than 7 days)? How about if the year starts on a non-monday, what week are the first days in (they could be in the last week of the previous year, in the first (0th or 1st) of the new year, or perhaps even week-less).

How about weeks, do they start on Mondays, Sundays, Saturdays, Fridays or some other day? That's kind of important when trying to calculate the first week of the year and counting the weeks until a date.

Calendaring issues get complicated very fast, and the fun thing about them is that there are so many ways people keep track of their measured days. The year could start at wildly differing dates, it's silly to assume that the first of January is the first day of a year, and if it's not you'd better not be DateDiffing that modulus 7 to get a WeekNumber.

I'll just happily ignore systems with weeks of more or less than 7 days. That would get hard.

With all this in mind also consider that your datasource may not be using the same calendaring as your reporting system. If your report may be run in Tehran and your DataSource is a database of some sort in Tel-Aviv they may not be using the same calendar, but it'd be an admirable instance of Persian/Semittic cooperation.

To get your week number you really should defer all the international issues to the people who've researched it and been paid to do a good job of it. Luckilly the .Net framework is produced by just such people, and I just love it when I can let it do the work for me.

The .Net framework (in which you report is running) knows about the locale it's running in, and this information can be used to derive the WeekNumber without ever adding together two days.

You just add this code snippet to your Code tab in your report properties:

Function getWeekNumber(ByVal d As DateTime) As Integer
    dim culture as System.Globalization.CultureInfo
    culture = system.Globalization.CultureInfo.CurrentCulture
 
    dim weekRule as System.Globalization.CalendarWeekRule
    weekRule = culture.DateTimeFormat.CalendarWeekRule
 
    dim firstDayOfWeek as System.DayOfWeek
    firstDayOfWeek = culture.DateTimeFormat.FirstDayOfWeek
 
    return culture.Calendar.GetWeekOfYear(d, weekRule, firstDayOfWeek)
End Function

Explanation:
First we get the current culture the report is running under (this would be the culture of the locale of the server running SSRS in our case). From this culture you extract the rule for starting weeks relative to the year and which day of the week the weeks start on. These two values, together with the date you have are passed into the Calendar of the current culture to get the WeekNumber of the date in that context.

It really is harder than it should be, but with this code (which you can just copy/paste) you make a new Calculated Field on your dataset with the following expression (I assume the Date field you want to convert to a WeekNumber is called Date in this instance):

=Code.getWeekNumber(Fields!Date.Value)

And, as they say, Robert is your mother's brother.

Addendum:

If you don't want to go through adding the WeekNumber to your DataSet you could of course just use the above expression straight in your textboxen or the like.

If you don't want the code you can inline it all as such (this is UGLY, don't do it!):
=System.globalization.CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(Fields!Date.Value, System.globalization.CultureInfo.CurrentCulture.DateTimeFormat.CalendarWeekRule, System.globalization.CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek)

Monday, October 5, 2009

Simpler reference equality on mocks

When mocking with MoQ I find myself regularly setting up the .Equals() method on the mocks. It just makes for more readable code to be able to ask a collection whether it .Contains() the mock than writing out the iteration myself.

But it does bloat my test-cases somewhat. Here's an example setting up a mock of a class with the equality set that returns true if it's compared to itself:
[Test(Description = "Mock of equals on an instance succeeds")]
public void MockingEquals_SameInstance_AreEqual() {
 var mocker = new Mock<SomeClass>();
 mocker.Setup(x => x.Equals(mocker.Object)).Returns(true);

 var mockedObject = mocker.Object;

 Assert.That(mockedObject.Equals(mockedObject));
}

That's fine if it's only in one test, but I don't like to repeat that all over my code. My first thought was to make a convenience method which set up the equality for me:
public static Mock<T> ReferenceEquals<T>(Mock<T> mock) where T : class {
 mock.Setup(m => m.Equals(mock.Object)).Returns(true);
 return mock;
}

Sticking this in some public place now allows me to set up my test from before like this:

[Test(Description = "Mock of equals on an instance succeeds")]
public void MockingEquals_SameInstance_AreEqual() {
 var mocker = new Mock<SomeClass>();
 ReferenceEquals(mocker);

 var mockedObject = mocker.Object;

 Assert.That(mockedObject.Equals(mockedObject));
}

That's certainly better. But, we can do better than that, can't we? Let's make it an extension-method, for great success! To use this code the class containing the extension-method must be static.

public static void HasReferenceEquality<T>(this Mock<T> mock) where T : class {
 mock.Setup(m => m.Equals(mock.Object)).Returns(true);
}

And with that our test has been transferred to the (in my opinion) much more readable:
[Test(Description = "Mock of equals on an instance succeeds")]
public void MockingEquals_SameInstance_AreEqual() {
 var mocker = new Mock<SomeClass>();
 mocker.HasReferenceEquality();

 var mockedObject = mocker.Object;

 Assert.That(mockedObject.Equals(mockedObject));
}

Great success!

Friday, September 25, 2009

MoQ with Collections, or equality woes

Still working with the fine mocking-framework MoQ, I came across a somewhat surprising facet (and I don't like surprises).
I'm mocking out a class which should be added to a collection after a method-call, like so:

public class SomeClass{

}

public class Container {
private List<SomeClass>; classes = new List<SomeClass>();

public IEnumerable<SomeClass> Classes {
get {
return classes;
}
}

public void addSomeClass(SomeClass instance) {
classes.Add(instance);
}
}

[Test]
public void ContainerContainsAddedClassAfterAdd() {
var mockSomeClass = new Mock<SomeClass>();
mockSomeClass.Setup(c => c.Equals(mockSomeClass.Object)).Return(true);

var Container = new Container();
Container.addSomeClass(mockSomeClass.Object);

Assert(Container.Classes.Contains(mockSomeClass.Object));
}

This works well, the mock is added to the Container's collection and the setup of the Equals method on the mock makes sure the IEnumerable.Contains() return true.
However there's always some complication. The class I'm really mocking out is not as simple as our SomeClass. It's something like this:

public class SomeClassOverridingEquals{
public virtual Equals(SomeClassOverridingEquals other) {
return false;
}

public override Equals(object obj) {
var other = obj as SomeClassOverridingEquals;

if (other != null) return Equals(other); // calls the override
return false;
}
}

[Test]
public void ContainerContainsAddedClassOverridingEqualsAfterAdd() {
var mockSomeClass = new Mock<SomeClassOverridingEquals>();
mockSomeClass.Setup(c => c.Equals(mockSomeClass.Object)).Return(true);

var Container = new Container();
Container.addSomeClass(mockSomeClass.Object);

Assert(Container.Classes.Contains(mockSomeClass.Object)); // fails
}

The class contains an override for the Equals method for its own specific type, and the Setup method for the mock does not seem to be able to mock out that specific method (only overriding the more general Equals(object)). Thus the test fails.

I have so far found no way of working around this quite common pattern, other than rewriting the class not to use the overriding equals.

I don't like that.

Anyone have any ideas?

Thursday, September 24, 2009

Mocking expectations on Property setters with MoQ

When mocking out your classes using the fabulous MoQ mocking-framework you may sometimes need to mock out an expectation that a property on your mocked object should be set.

It took me some time to figure out how to do this. The documentation on the MoQ quickstart wiki says to use the following pattern:

// expects an invocation to set the value to "foo"
mock.SetupSet(foo => foo.Name = "foo");


Well, I tried this with an object:

public class SomeClass {
public string Something { get; set; }
}

[Test]
public void PropertySetupOnMock() {
const string someString = "Should be set";
var mocker = new Mock<SomeClass>();

mocker.SetupSet(x => x.Something = someString); //exception here
mocker.Object.Something = someString;
mocker.VerifySet(x => x.Something);
}

This only gave me an error on the line with the SetupSet:
System.ArgumentException: Invalid expectation on a non-overridable member.

Well, that's strange. After some looking around it turns out that MoQ does its magic by extending the class that's being mocked. It cannot extend non-virtual properties, so this Something property cannot be extended and thus cannot be Setup.
What can be done about this? The easiest immediate solution would be to mark the property virtual, like so:

public class SomeClass {
public virtual string Something { get; set; }
}

But do we really want to decorate every property on the objects as virtual? Not really. A better solution is to push the property up to an interface and mock that interface instead, like so:

public interface IWithProperty {
public string Something { get; set; }
}

public class SomeClass : IWithProperty {
public string Something { get; set; }
}

[Test]
public void PropertySetupOnInterfaceMock() {
const string someString = "Should be set";
var mocker = new Mock<IWithProperty>();

mocker.SetupSet(x => x.Something = someString); //no exception here
mocker.Object.Something = someString;
mocker.VerifySet(x => x.Something);
}

Thus allowing me to mock it out and test that the property setter was indeed called. Yay!
If anyone has any better ways of doing this I'd love to hear from you!

Tuesday, February 10, 2009

Listing the properties of an object

Sometimes you just want to print out the state of some reasonably rich object (for logging or GUI mock-up or whatever). This code gives you a PropertyGetter class with two easy-to-use static methods. listProperties returns a name: value string listing of the properties on the input object, for easy logging.

If you want the object's properties as a list of properties you can get that as well.

NB: This simple class does not recurse through complex objects, so you will only ever get the top-level of a property.

Code:

using System.Collections.Generic;
using System.Reflection;
using System.Text;

namespace Code_Examples {
class PropertyGetter {
public static string listProperties(object someObject) {
StringBuilder sb = new StringBuilder();

List<pair> props = getProperties(someObject);

foreach (Pair prop in props) {
sb.AppendLine(prop.Name + ": " + prop.Value);
}
return sb.ToString();
}

public static List<pair> getProperties(object someObject) {
List<pair> dic = new List<pair>();

PropertyInfo[] props = someObject.GetType().GetProperties();
foreach (PropertyInfo prop in props) {
Pair t = new Pair {
Name = prop.Name,
Value = prop.GetValue(someObject, null)
};
dic.Add(t);
}
return dic;
}
}

public class Pair {
public string Name {
get;
set;
}
public object Value {
get;
set;
}
}
}

Monday, January 26, 2009

Logging snippets ready to go

My last post was some simple intro to how you can make your own snippets - here are some easy ones I made to make logging with log4net easier.

This first one is one I use simply to get a logger with the name of the current class. I put this in most every class. Nothing fancy here, just type log and press tab to get a logger named log.

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>Logging
</Title>
<Author>Tomas Ekeli</Author>
<Description>Initializes a logger with the name of the current class</Description>
<HelpUrl />
<SnippetTypes />
<Keywords />
<Shortcut>log</Shortcut>
</Header>
<Snippet>
<References>
<Reference>
<Assembly>log4net.dll</Assembly>
<Url />
</Reference>
</References>
<Imports>
<Import>
<Namespace>log4net</Namespace>
</Import>
</Imports>
<Declarations />
<Code Language="CSharp" Kind="" Delimiter="$"><![CDATA[protected static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); ]]></Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>


Now you've got a logger up and running you'll be doing a lot of calls to log.debug("somestring") and even more with formatting on that string. So here's a snippet for that:


<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>logFormattedString</Title>
<Author>Tomas Ekeli</Author>
<Description>log formatted string</Description>
<HelpUrl></HelpUrl>
<SnippetTypes />
<Keywords />
<Shortcut>lsf</Shortcut>
</Header>
<Snippet>
<References>
<Reference>
<Assembly>log4net</Assembly>
<Url />
</Reference>
</References>
<Imports>
<Import>
<Namespace>log4net</Namespace>
</Import>
</Imports>
<Declarations>
<Literal Editable="true">
<ID>Level</ID>
<Type />
<ToolTip>Log level</ToolTip>
<Default>Debug</Default>
<Function />
</Literal>
<Literal Editable="true">
<ID>Message</ID>
<Type />
<ToolTip />
<Default>field value: {0}</Default>
<Function />
</Literal>
<div class="youtube-video"><object Editable="true">
<ID>Field</ID>
<Type>Object</Type>
<ToolTip>The field to log</ToolTip>
<Default></Default>
<Function />
</object></div>
</Declarations>
<Code Language="csharp" Kind="method body" Delimiter="$"><![CDATA[log.$Level$(string.Format("$Message$", $Field$));]]></Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>


Finally a very nice thing about logging is when you can push the context on a stack for inclusion. I use this all the time. This snippet can be used as a surrounding snippet as well (mark the text you want to surround in a context and right-click -> Surround-with


<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>logFormattedString</Title>
<Author>Tomas Ekeli</Author>
<Description>log formatted string</Description>
<HelpUrl></HelpUrl>
<SnippetTypes />
<Keywords />
<Shortcut>lsf</Shortcut>
</Header>
<Snippet>
<References>
<Reference>
<Assembly>log4net</Assembly>
<Url />
</Reference>
</References>
<Imports>
<Import>
<Namespace>log4net</Namespace>
</Import>
</Imports>
<Declarations>
<Literal Editable="true">
<ID>Level</ID>
<Type />
<ToolTip>Log level</ToolTip>
<Default>Debug</Default>
<Function />
</Literal>
<Literal Editable="true">
<ID>Message</ID>
<Type />
<ToolTip />
<Default>field value: {0}</Default>
<Function />
</Literal>
<div class="youtube-video"><object Editable="true">
<ID>Field</ID>
<Type>Object</Type>
<ToolTip>The field to log</ToolTip>
<Default></Default>
<Function />
</object></div>
</Declarations>
<Code Language="csharp" Kind="method body" Delimiter="$"><![CDATA[log.$Level$(string.Format("$Message$", $Field$));]]></Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>

Code snippets

I like anything which makes my day easier. Code snippets are one of these things. You can make your own code-snippets by editing xml directly, using the code snippet schema reference, or you could use a tool. Snippet Editor is just such a tool. With it making and editing snippets is dead simple!

One thing I've noticed with it, though, is that you cannot make a "SurroundsWith" snippet in this tool. To do this you have to edit the xml yourself. It's easy when you know how - just add the following to the header section of your snippet definition:


<SnippetTypes>
<SnippetType>Expansion</SnippetType>
<SnippetType>SurroundsWith</SnippetType>
</SnippetTypes>


And in your snippet section you add selected and end fields where you would insert the code you want to surround with the snippet (the following example would surround the selected code with curly braces).


<Code Language="csharp" Kind="method body" Delimiter="$">
<![CDATA[{ $selected$ $end$}]]>
</Code>

Tuesday, October 14, 2008

Developing ASP pages with Firefox

I prefer Firefox 3 to IE7 when developing (understatement of the week), particularly because it has Firebug and YSlow (an add-on for an add-on, would that be a quasi-add-on?). IE7 has the developer toolbar, but it's not as good (in my experience).
Naturally I have Firefox as my default browser, so when I launch ASP pages in the development server from Visual Studio they open in Firefox. And it is soooo slooooow. It's ridiculous, it's like being on my old 28.8 modem back in the mid-90s.
How can this be? Firefox isn't slower than IE7. That cannot be, no benchmarks point in that direction (OK, those are just javascript, but you get the gist of it).
It turns out the problem is with Firefox 3 and the development server (Cassini). I haven't dug deeply into it, just observed the problem, found the fix and applied it.
I've no time to check it right now, but disabling IPv6 seems a bit ham-handed. It should be possible to just include localhost in the network.dns.ipv4OnlyDomains preference. This is a string containing a comma-separated list of domains.

Technorati Tags: , , , , , , , ,

Tuesday, October 7, 2008

C# Compiler Could Not Be Created

I was having memory-problems on my dev machine (2 Gigs don't go far these days), and after a few resets I was getting an exception whenever I tried to open a solution containing a C# project in VS2005. It told me the C# compiler could not be loaded, and I should reinstall VS.
Eh? No way. That's an entire day, getting it back to where I want it.
So I open the Help -> About to see if I can get some info about what's wrong. There I'm hit by a Package Load Failure, coming from ReSharper (we loves it). This looks like something.
A quick google for this, and I find Aaron Stebner has a possible solution. The tool he references in that post didn't find any problems, but running through the steps to re-build the native libraries did.
So these are the steps in case that blog ever disappears:
  1. Open a cmd prompt
  2. Run del %windir%\Assembly\NativeImages_v2.0.XXXXX_32\Microsoft.VisualStu#
    (where XXXXX is the build number of the .NET Framework 2.0 that
    you have installed - you can figure that out by looking at the name of
    the folder at %windir%\Microsoft.NET\Framework\v2.0.XXXXX)
  3. Run %windir%\Microsoft.NET\Framework\v2.0.XXXXX\ngen.exe update /queue
That's done it. A restart of VS and we're up and running again. Now I just have to convince my boss I should get 2 more gigs of RAM.

Wednesday, October 1, 2008

Personal Information Numbers

A quick set of Regexes to check for Personal Information Numbers from the nordic countries.
I use these to check the format only, no fancy calculation of checksums etc.

  • Norwegian: DDMMYYXXXXX
    \b[0-4]\d[014]\d{3}\d{5} 

  • Swedish: YYMMDDCXXXX (C == '-' || '+')
    \b\d{2}[01]\d[0-3]\d[-\+]\d{4}

  • Danish: DDMMYY-XXXX
    \b[0-3]\d[01]\d{3}-\d{4}

  • Finnish: DDMMYYCZZZQ (C == '-'||'+'||'A' ; Q == [01234536789ABCDEFHJKLMNPRSTUVWXY])
    \b[0-3]\d[01]\d{3}[-\+A]\d{3}[0-9A-FHJ-NPR-Y]

  • Icelandic: DDMMYY-XXXC (C == 0 || 9)
    \b[0-3]\d[01]\d{3}-\d{3}[09]

Tuesday, September 30, 2008

Podcasts

I listen to a lot of podcasts. What can I say, I bore easily. When I have to do something that's not social and takes more than a few minutes I often pop the earbuds in and start listening. My player, the Zen V 2Gb, is far too small to contain any music, and podcasts are a great way of getting some intelectual topping in the daily drudgery.

I thought I'd post the list of podcasts I enjoy regularly:
  • CBC Radio Quirks and Quarks
    A great show about the stories breaking in science. Definitely a Canadian slant, and they've been running theme shows about Canada's carbon-footprint and such lately (which doesn't interest me that much), but still one of the greatest.

  • FLOSS Weekly
    This show died for a while, but they're back with a vengeance. In-depth interviews with important people in the open-source world. Could it be better?

  • In Our Time With Melvyn Bragg
    Melvyn is a delightfully pretentious British chap interviewing academics on interesting themes. Always interesting, and a really eclectic selection of themes.

  • IT Conversations
    One of the really early podcasts, this is excellent quality. I listen to the entire feed, which I think syndicates a lot of other feeds. Great content from biomedical/genetics to philosophy and CTO-grade stuff. There's always something here you haven't heard about.

  • Math Mutation
    This is a new one for me. Erik Seligman talks very briefly (3-5 minutes) about some interesting topics from the world of Math and Numbers. Good content!

  • StarStuff with Stuart Gary
    I'm a sucker for the starry nights, and this recent discovery seems right up my alley. I've only listened to two yet, but I think it's a keeper!

  • The Java Posse
    My favourite! The posse have weekly commentaries on the tech-world with a heavy java-slant. All the members are world-class developers with strong opinions and the arguments to back them up. It makes my day every time I see a new one pop into my playlist.

  • The Skeptic's Guide to the Universe
    Tackling the idiocy and numb-thinking out there head on, the Skeptical Rogues are not afraid to delve deeply into any theme they find. It's so refreshing to see that there are intelligent people out there, not just nuts.

  • This Week in Science - The Kickass Science Podcast
    Another recent discovery on my part. I'm not sure if this is a keeper yet. The themes seem interesting, but they're just not catching me yet. I'll keep them around for a few weeks, and see if they're worth it.

  • This Week in Tech
    I've a feeling this is the big one. Leo Laporte is a great host, and he gets a lot of interesting punters. Whenever they talk about something I know they always manage to get it dead-wrong though, so I take their opinions with a grain of salt. Still, the show is a gem.
Definitely a Tech/Science slant, eh? Any others I should know about, or comments are much appreciated :)

Thursday, September 25, 2008

CountDistinct - How About SumDistinct - Or Just AggregateDistinct?

Making a report in Microsoft SQL Server 2005 Reporting Services today I needed to sum up several levels in a table. However, the numbers where available only at an intermediate aggregated level, so I needed to sum up only the distinct sublevel-items. Got that? Maybe it's easier if I show an example:


Let's concoct an example here. I'm extracting data in the following groups: Firm, ProductType, Product, Account, Item. Say I'm summing up one measure, BalanceAmount, on products, but my dataset returns other measures on the lowest level, Item.


Here's an example DataSet






















































































Firm ProductType Product Account Item BalanceAmount ItemMeasure
Acme Explosive Nitroglyserin C-001 Bottle $200 $2.14
Acme Explosive Nitroglyserin C-001 Mug $200 $4.87
Acme Explosive TNT C-001 Stick $200 $0.79
Acme Explosive Plastique C-001 Wad $200 $7.64
Acme Prop Fake rock C-001 Crate $200 $4.55
Acme Explosive TNT C-124 Stick $89 $0.79
Acme Explosive TNT C-124 Half-stick $89 $0.40
Acme Explosive Nitroglyserin C-089 Stick $40 $0.79


In the report I need to show this as a drill-down table, with the measures down on the lowest level (Item) summed up on every level, and the aggregated measure (BalanceAmount) summed up on every level where it makes sense.


The problem is that if you use the common SUM function the report would sum up all BalanceAmounts on the Account level resulting in a BalanceAmount of $1000 for Account C-001, not $200 which is the correct value (that would be $200, Einstein).


What you really need is something like CountDistinct, but just for Sum. In fact, this is a specific case of a more general problem where you need to conduct some aggregation on some scope. How do I do something like that?


It's not really hard. What I need is some way to make just the first instance of a value in whatever scope needed be the counting one. To get this I enter the
following into the Code tab of you Report properties:



    Dim scopes As System.Collections.Hashtable
Function getDistinct(ByVal checkMe As Object, ByVal scope As String) As Boolean
Dim firstTimeSeen As Boolean

If (scopes Is Nothing) Then
scopes = New System.Collections.Hashtable
End If

If (Not scopes.Contains(scope)) Then
scopes.Add(scope, New System.Collections.ArrayList)
End If

If (Not CType(scopes(scope), System.Collections.ArrayList).Contains(checkMe)) Then
firstTimeSeen = True
CType(scopes(scope), System.Collections.ArrayList).Add(checkMe)
Else
firstTimeSeen = False
End If

Return firstTimeSeen
End Function

The usage pattern to get SumDistinct in this case is:


=SUM(IIF(Code.getDistinct(Fields!account.Value, "scopeId"), Fields!BalanceAmount.Value, 0))

The "scopeId" string identifies the current scope, so it will change from group to group. E.g. in the Firm group you'd enter "firm", in the ProductType group you'd enter "prodType", etc.


So what am I doing here? We're remembering the values of the checkMe parameter in the context of the scope, and returning True in only the first instance. Thus we can just sum the first of every checkMe value. You could use this to do your own CountDistinct by entering the following expression (but this would be a bit roundabout for that use-case):


=COUNT(IIF(Code.getDistinct(Fields!account.Value, "scopeId"), Fields!BalanceAmount.Value, 0))

I hope you get some use out of this. This kind of code is easy to crank out, but even easier to just copy-paste.

Sunday, August 10, 2008

The 21 Laws of Computer Programming

These had me in stitches. Particularly number 19: "If you automate a mess, you get an automated mess". So true, and taken with another favourite saying of mine, "If it's worth doing once it's worth automating" we've got a true mess on our hands :)

Read 21 laws of computer programming

The Great Office War

Thursday, August 2, 2007

DropDownList with a blank item on top

The DropDownList (DDL) is a nice little databindable control in ASP.Net. The easiest way to use it is to databind it to some value from your database and let the user select.

The problem with straight on databinding is that the first item in the DDL will be the first item in the databound collection (naturally), thus making choosing that item "a little too easy" and "a little too hard". When the user wants to select the top item you might be in for problems, as that selection won't fire off a SelectedIndexChanged -event, which is what you'd normally listen for.

There are ways around this - I've seen several suggestions. None of these are even starters in my opinion:
  • Don't handle changes in the DDL, read from it on some other event (typically a button)
    • This makes it very hard to create responsive UIs, as your user will always have to keep clicking buttons
  • Set whatever you want to happen on changes in the DDL as if the first choice was already chosen (pre-choose for the user)
    • Badness - especially in a stateless media like the web - your user may never correct your initial (possibly wrong) assumption
  • Add an empty record in the databound object (typically your database)
    • Perhaps the worst idea I've seen. The database should not contain filler like this. It's a bit better if you're working with some transient object - but still it's a hack. We don't like hacks.
The way to fix this problem is really not very hard. What you want is an empty item on the top of the list that the user will not expect to work, with a known value you can check for in your SelectedIndexChanged handler.

To do this add a ListItem to the DDL in the page/control's definition with your sentinel value and an empty string as the Text property. Normally this item would disappear on databinding, but wait: set the AppendDataBoundItems property on the DDL to True, and your empty initial value will survive!

Two things to beware: Make sure you check for your sentinel value in the handler, and remember to clear all but the initial item of the DDL's ItemListCollection if you're databinding that control again later (unless you want to keep appending).

Here's your illustrative code:
A standard DDL:

<asp:DropDownList id="ddBasic" runat="server" autopostback="True">
<!-- Selecting the top item will not fire an event (it will be pre-selected) -->
</asp:DropDownList>


A DDL with the empty top-item:

<asp:DropDownList id="ddWithEmptyTop" runat="server" autopostback="True" appenddatabounditems="True">
<asp:ListItem value="-1" text=""> </asp:ListItem>
</asp:DropDownList>


Technorati Tags: , , ,

Thursday, July 26, 2007

Glotton - word glutton


I've launched a new small open-source application, Glotton. Get it at http://code.google.com/p/glotton/

It's my summer holiday, and I've been playing some word-games (in particular Scrabble and BookWorm). I'm not particularly good at these, but I find them fun.

What I can do, however, is to get a computer to help me. So I did - I created Glotton.

It is free, open-source and available for whatever use anyone may put it to. The easiest way to use it is to download the pre-packaged zip, extract this and run the jar-file.

Send me a note if you like it or find any bugs.


Powered by ScribeFire.

Wednesday, June 27, 2007

Error ... error ... whence cometh thou?

I'm working on a page that will allow our users to view a table in a database in ASP.Net. To do this I am writing a UserControl where I am connecting to the database through a SqlDataSource, and I bind a GridView on the UserControl to this datasource. If you are unfamiliar with databinding this simply means that I hook the presenting controller (the GridView) up to an object that can provide it with data. This automagically populates the presenter with the data from the source. But, as with all automagical things can, and do, go horribly wrong.

Small digression: I'm from the Java plains, but I've recently set up shop in the .Net woods where I mostly hang around in the soppy marshes of ASP, the glossy banks of MSSQL-RS and more recently the crags of ADO. Being quite new in these parts I get to do a lot of really stupid things, and I'm building an appreciation of each land's pros and cons. One of my gripes with the marshes of ASP is the incessant focus on doing things declaratively as the first-choice. Sure, it's great if what you want is static, but I don't want static content!

Why this rant? Declarativity just bit me in the rump, like a particularly nasty ass.

So, I was binding my GridView to the SQLDataSource declaratively, like a good ASP-ape. Everything was working as expected until I started testing the page with non-standard inputs. The datasource is setup with the data it needs to connect to the database, and an initial select statement to get at the data you want. If any of this is incorrect the entire page explodes in a messy way. Red goo all over the walls.

My test-case was simply setting up the datasource with an incorrect login. This caused an SQL-exception when the SQLDataSource tried to perform the initial Select (as one would expect). Now, .Net does not have checked exceptions (expect a post on this later), so being from the Java-land I am always a bit surprised and worried to see exceptions being thrown willy-nilly run-time.

Well, this exception I should be able to deal with. It is a real problem, but the application should be able to handle such problems gracefully. I just had to find some place to catch this exception, and handle it.

There are several places you can handle an error in ASP.Net:
  • global.asax (where you can handle application-level errors), overriding one of the following methods
    • Gobal_Error
    • Application_Error
  • Page_Error (where you can handle errors on a page-level)
In addition to this you can define a page to send the user to in case of an error, so the plebs don't have to see the ugly innards of your app. All of these solutions are good, but not for my use-case where I actually wanted to handle the exception, should it occur. These solutions let you do things like log and redirect on errors, but offer little in the way of actually handling the problem.

Yes, on a page I could have used the Page_Error, the problem is that I am building a UserControl, which is part of a page, but not the page itself. And, a UserControl has no onError event to catch, and on Error method to override. In effect the UserControl blew up, killing all bystanding controls and page-elements.

Digging in to where the error originated I saw that it was spawned when the controls on the page where coming to life, in the preRender -stage of the lifecycle. This tells me that I must place some code to handle this before that stage of the lifecycle to avoid this error. In the purely declarative mannar I would declare a handler for some SqlDataSource error event, but that component does not have one. So I am relegated to provoking the error myself, and handling it correctly.

This is not really all that hard, once you've gotten to this level (the realisation that I had to do it this way took much longer than the coding of the fix). What you're forced to do is to not bind the GridView to the SQLDataSource declaratively. Read that again. To handle the error you're forced to step out of the declarative world and do the binding yourself. Now, this isn't hard, but it suddenly seems as if the declarative way of doing things is second-rate and sunshine-days-only. I can live with that, but what I really don't like is how every example focuses on declarative implementation, when it's the less powerful and (to me) less natural way of doing things. Sigh.

Oh, well, on to the fix. Just remove the DataSourceID="someDataSource" element from your GridView decalaration, and add something like this to you Page.Load handler:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
theGridView.DataSource = someDataSource
Try
GridView1.DataBind()
btnSave.Visible = True
lblError.Visible = False
Catch exc As SqlException
lblError.Visible = True
btnSave.Visible = False
lblError.Text = "Could not load table: " & exc.Message
End Try
End Sub

This will let the UserControl come up, even if the datasource experienced an error. Much better, but I'm left feeling it's dumb that I have to do it this way.



Technorati Tags: , , , , , ,

Tuesday, June 26, 2007

Let's Talk About It...

"Let's talk about it..." that's the name of the latest version of Jade, that was released yesterday! That's right, a new version of everyone's favourite FIPA-compliant Java-based multi-agent framework has a new release. And this is just, what, a month or two since the first book about the framework was released (Developing Multi-Agent Systems with JADE).



Exciting times!



So, what's new in this release? Any breaking changes? Let's have a look at the press-release (I haven't had time to try it out yet. I'll do that at my first opportunity and post my impressions).



  • A whole new communication mechanism: Publish/Subscribe!
    • A whole new mechanism where agents can simply register a topic they are interested in, and every message that is broadcast with that topic will be sent to them. This sounds like a good idea to me, uncoupling the broadcaster and subscribers. It is only applicable to general broadcasts, though, and I wonder how they implement "closed" lists.

    • Sounds like they are implemented by making a proxy-AID representing the topic, to ensure compatibility. I wonder if this is a good thing, is breaking the agent-AID binding really something you want to do?

  • A "completely restructured version of the Web Service Integration Gateway"
    • Web app (can be deployed to app servers, like Apache Tomcat)!
    • Web-services are derived from the actions in the ontologies registered to the Directory Facilitator.
    • WSDL document generated for each agent service exposed (how did they do it before?)
    • I haven't used the WSID earlier, so I can't really add any thoughts to this, check out the guide here.

  • Improved detection of main container using UDP Multicast packets

    • Excellent, this makes it easier to set up multi-platform environments!
  • A reflector, finally allowing us to use the Java Collections Framework when working with ontologies
    • Excellent stuff, now just port the entire thing to Java 1.5 to give us generics!

All in all some exciting stuff! Let's hope this one doesn't break anything, eh? I've not touched it yet, and so far on the mailing-lists there's one user who's had problems with the multicasting on his machine, but it looks like a he found a workaround (detect-main option set to false).



Let the bugsearch ensue :-)



If you met God on your commute

Here's a nice little story...



Talking to God...