I love nHibernate but…

There are just to many assemblies to include to get it running. In my little fantasy idealistic world the main features of nHibernate would be contained in one assembly that would not clog up the references if the project and make it dependent upon to many things.

Before you will burn me on the stake for taking a swing on your favorite OR-mapper hear me out. In the old days where people/software were happy if they could replace their 50+ lines of ADO.NET code with 5 XML files nHibernate was the bomb. All that you needed was one assembly and you were done. Today the situation is somewhat different. I personally expect an OR mapper to deliver:

  • Basic OR mapping functionality
  • Fluent configuration
  • A Linq provider
The situation now is that each of this is a separate assembly. And I do not like this. This means that I must have three assemblies where only one should be, making my projects “bloated” and my bin folder looking like a dump.
I do not see a reason why this can not be included in the main distribution of nHibernate and my hopes are high that this will be the case in the near future.
Advertisements

Photography…my new passion

Not blogging that much anymore. The reason for that is that I have been taken up by a new hobby which is actually consuming most of my free time.

I am still looking for the right amount of photography and development, but I am not there yet 🙂

Anyway if you are interested in my little pictures then you are all invited to take a look at my flickr photostream 🙂

Log4Net custom LayoutPattern

A few days ago I had a nice little task at my workplace: To add a custom “key” to the log4net pattern layout.

I like log4net and was quite happy to be given time to dive into the belly of the beast. But I quickly noticed that most of the information on the web is outdated and simply wrong with the current version.

The most useful information I got was from a blog post from Scot Hanselman http://www.hanselman.com/blog/CreatingYourOwnCustomPatternLayoutPatternParserAndPatternConvertorWithLog4net.aspx. Sadly log4net has moved along since that blog was written.

Where I failed in the current version was the fact that the PatternParser class is sealed thus making any inheritance attempts void.

After some Googleing and reading the log4net SDK I found the solution to be simple and elegant at the same time. All you have to do is to create two classes:

  • PatternConverter
  • PatternLayout

And you are done.

The first class you have to implement is a PatternConverter which will actually handle the new key you want to add. The implementation is straight forward and easy to do. An example would look like this:

using System.IO;
using log4net.Util;
namespace MyCustomLog4NetPattern
{
  public class CustomPatternConverter : PatternConverter
  {
    protected override void Convert(TextWriter writer, object state)
    {
      writer.Write("Message to add");
    }
  }
}

The object that gets passed in is actually the LoggingEvent that has triggered the log operation. Sou if you need additional resources from there they are available. Additionally to this you can access all the environment variables that you thing should be placed in the log.

A little word of advice: Do not apply any formatting to the string you write. So no newlines or any other fancy formating.

After that is implemented we can move on to the PatternLayout. This is again an almost empty inheritance story. Which will look like this:

using log4net.Layout;
namespace MyCustomLog4NetPattern
{
  public class CustomPatternLayout : PatternLayout
  {
    public CustomPatternLayout()
    {
      AddConverter(new ConverterInfo{Name = "your_key", Type = typeof(CustomPatternConverter )});
    }
  }
}

And that is it! You are done. Every time that the string %your_key appears in the appender layout your little converter will be called and the placeholder replaced.

If now you want to use your custom “key” on a rolling file appender the configuration would look like this:

<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
  <file value="Log/log.txt" />
  <appendToFile value="true" />
  <rollingStyle value="Composite" />
  <datePattern value="yyyyMMdd" />
  <maxSizeRollBackups value="10" />
  <maximumFileSize value="1MB" />
  <layout type="MyCustomLog4NetPattern.CustomPatternLayout, MyCustomLog4NetPattern">
    <conversionPattern value="%date [%thread] %-5level %logger - %message%newline - My stuff: %your_key%newline" />
  </layout>
</appender>

Ant that is all that there is to this. easy and simple solution.

Hope that this will save somebody some time in the future 🙂

Localization! If I tell you English then give me English

I know that the next logical step in web pages is to deliver more and more localized content. But i think that some companies and developers are taking to many shortcuts to achieve this! The most aggravating ones is defining the users preferred language based on his location! This I just wrong! And in this post I will explain you why I think that this is wrong and how to do it correctly!

Before we start a little word of explanation: I prefer my web sites in English (regardless of my mother tongue) but sadly my IP address belongs to an Austria ISP. So naturally all “geo-aware” websites know where my computer sits. OK, this is not bad but what drives me mad is what they do with that information.

Now that that is done lets start. So I fire up my browser and navigate to a geo-aware website, like IGN.com. so what do I see on my screen when the page has loaded? I see:

  • Local news…OK
  • Local release dates…OK
  • The adds are also local…OK
  • Everything on the freking page is in German…NOT OK

I am quite happy with the location related information but does my location really tell anything about my language preferences? The simple answer is NO IT DOES NOT! But how should the web page know your language preferences if it does not know you? Someone could ask at this time. The answer to this is so simple that 80% of web pages tend to ignore it:

The web page does not but my browser knows!

Each browser on the face of this earth send the desired language preferences to the server when making an request! Why this simple fact is ignored i do not understand! Here is the screen that proves it!

Language settings dialog in Firefox

Firefox language settings

So this “preference” gets send to the web server with every request I do! Take this and give me the page in the language I would like to see! I do not care about the adds and the offers but the actual page content could be displayed in the language I desire (If it exists). I do not expect this to be true for small local sites that are only published with one language. But for big multilingual sites that have all the content in English to show it in my location language just because my IP says so is just mocking the user!

If the content that I want is there then please give it to me!

Making it the right way is not that hard! I will take .NET with C# to make my point, but it is easily possible in all other languages.

If you are in a web application you can simply query the HttpContext for the data you need:

string[] languages = HttpContext.Current.Request.UserLanguages;

And that is it! Now you have the names of the users preferred cultures (this includes the language he wants). Just pick the first one and use that to resolve all your multi language resources.

Is that really that hard?

My little VS theme

For a lack of a better option I sat down and started surfing the new for something interesting when I stumbled upon bespin. Not only is it a nice little browser but the default color scheme immediately took me over. I must say, that I like it a lot.

So after playing with it for some time I decided to do a little color theme for visual studio and see if I can get the same feel there. To achieve this I used the fine services of studiostyles. It is not a perfect tool, there are some problems with using it outside of the IE, but still it is good.

Now that it is done I must say that I somehow like the new theme.

If you would like to get the new VS them you can download it here

VS theme sample

Bespin inspired VS theme

Generic section handler – HowTo

After being on vacation for the last two weeks and therefore suffering a little from internet abstinence syndrome,  I am back with a vengeance. No not really but I do feel refreshed and ready to take on anything.

So to get me a little in to the “flow” here is a post that I was planning on publishing before vacation time struck.

It is nothing special just a little testing helper I wrote to get some much needed tests done.

If you are developing a .NET component that is going to be part of a bigger application (or solution), then chances are high that you will provide a so called “custom configuration section”. There are other ways to configure a component but a custom configuration section is quite elegant.

So youz start to develop your custom configuration section. I hope that I do not have to cover the nuts and bolts of how to get this done. What I want to convey here is the aswer to the question how to test this.

After you implement you object structure you are faced with quite a nice little problem of how to verify that your configuration is “bullet prof”? There are two answers to this question:

  1. You write the XML configuration for all the possible permutations and combination’s of how your configuration section could be used
  2. You test each configuration “element” on its own and verify that each functions ass desired.

No. 2 is definitely the better option and therefore should be used. And there is another argument for option no. 2: If the configuration section is relatively complex then the number of combination goes through the roof. If you ever put the effort into testing custom configuration section then you know that this is not easily achieved.

But neither the less lets get to explaining how this is done. My testing framework of choice is nUnit so all code samples will be done using it.

I take that you have your configuration elements already defined. So now all you need to do is to write a simple generic configuration section handler

using System.Configuration;
using NUnit.Framework;

namespace Tests.Helpers
{
 public class GenericSectionHandler : ConfigurationSection where T : ConfigurationElement
 {
 private const string ELEMENT_NAME = "TestElement";

 [ConfigurationProperty(ELEMENT_NAME, IsRequired = true)]
 public T GetConfiguration
 {
 get
 {
 return (T) this[ELEMENT_NAME];
 }
 }

 public static T Get(string sectionName)
 {
 var sectionHandler = ConfigurationManager.GetSection(sectionName) as GenericSectionHandler;
 if(sectionHandler == null)
 Assert.Fail("section [{0}] does not exist", sectionName);

 return sectionHandler.GetConfiguration;
 }
 }
}

After you have that in place inside your code you have to add the configuration group to your test project configuration file. This could look like the following:

<sectionGroup name="SectionTests">
 MyTest" type="Tests.Helpers.GenericSectionHandler`1[[MyApplication.Configuration.MySection, MyApplication]], Test"/>
</sectionGroup>

Now you can just add your configuration scenario.

<SectionTests>
  <MyTest>
    <TestElement>
      <!-- YOUR CONFIGURATION ELEMENT -->
    </TestElement>
  </MyTest>
</SectionTests>

Now you can use your configuration section scenario in your actual unit tests.

public void MySectionTests()
{
  var mySection = GenericSectionHandler.Get("SectionTests/MyTest");
  // do your assertions
}

And that is that.

Not the best post I ever had I know but since I was not blogging for quite some time I hope that this will be a nice start of getting into the habit of blogging 🙂

Resharper 5.1 first look’s

About a year ago I started using JetBrains reSharper or R# for shorts and have been hooked ever since.

So it came naturally that I followed the upgrade wave and installed R# 5.1. And I must say that I blew me away on more than one level. The refactoring and navigation support is as good as ever and the integration with VS 2010 is flawless. But there was a dark side to the upgrade.

While my PC at home had no problem running VS2008 with R# 4.* and VS2010 also did not impress my little dual-core machine, but at soon as R# 5.1 was installed the computer just stopped.

I hist major performance problems. Especially when the background “compiler” kicked in. The feature set is impressive but still I find it hard to use because writing “return foo;” takes 30 sec+.

I will come back with another post describing all the cool pictures. But just had to get the little performance problem out of the door.

And now off to my well deserved vacation 🙂