Archive for the 'Uncategorized' Category

05
Aug
10

This blog has moved

To http://blog.hitchman.net. See you there

Advertisements
27
Jul
09

Dependency Injection for NHibernate

Sly has asked about the DependencyInjectionInterceptor class I referred to in a previous post.

Here is the class:

    ///<summary>
    /// An NHibernate interceptor that instantiates objects from a DI container.
    ///</summary>
    public class DependencyInjectionInterceptor : EmptyInterceptor
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(DependencyInjectionInterceptor));
        private readonly IKernel container;
        private readonly Check chk = new Check();
        ///<summary>
        /// Construct the <see cref="DependencyInjectionInterceptor"/>.
        /// Entities found by name in the container will be resolved from there.
        /// All other entities will use default NHibernate instantiation.
        ///</summary>
        ///<param name="container">The IoC container</param>
        public DependencyInjectionInterceptor(IKernel container)
        {
            this.container = container;
        }
        /// <summary>
        ///             Instantiate the entity class. Return <see langword="null" /> to indicate that Hibernate should use the default
        ///             constructor of the class
        /// </summary>
        /// <param name="entityName">the name of the entity </param>
        /// <param name="entityMode">The type of entity instance to be returned. </param>
        /// <param name="id">the identifier of the new instance </param>
        /// <returns>
        /// An instance of the class, or <see langword="null" /> to choose default behaviour
        /// </returns>
        /// <remarks>
        ///             The identifier property of the returned instance
        ///             should be initialized with the given identifier.
        /// </remarks>
        public override object Instantiate(string entityName, EntityMode entityMode, object id)
        {
            try
            {
                return container.Resolve(entityName, new {Id = id});
            }
            catch(ComponentNotFoundException)
            {
                chk.Assert(!container.HasComponent(entityName),
                           string.Format(
                                   "Entity {0}, registered in the container, could not be constructed with an 'Id' " +
                                   "dynamic argument. The DependencyInjectionInterceptor expects to find a component " +
                                   "with a contructor having all dependencies satisfied and with a property named 'Id' to " +
                                   "take the object's id (key).",
                                   entityName));
                log.DebugFormat(
                        "Instantiate: Entity {0} was not registered in the container. Using default NHibernate contruction.",
                        entityName);
                return null;
            }
        }
    }

IKernel is the Castle Micro Kernel (within Windsor). Each entity (mapped class) needs to be registered in the container and must have an Id property.

One important ‘gotcha’ is that Castle will inject into public properties by default, so a class with a many-to-one will be given a default instance of the ‘one’ type by Castle, which you probably don’t want. The simplest way to stop this is to decorate the property with the DoNoWireAttribute in Castle.Core. There are other smarter ways.

As I mentioned in my reply to Sly, alternatives based on the latest NH release are described on NHForge.

04
Jun
09

Aptitude testing software engineers

I sat a three-hour aptitude test yesterday that is designed to test my ability to concentrate, understand complex requirements and accurately implement (follow) instructions.

It was a mentally draining experience. Afterwards I was trying to find a way of explaining the test to my wife (who is a lawyer and mediator, not a techie). She shivered in horror as I explained it to her.

On further reflection I think the following analogies fairly represent the test:

  • The requirements (questions) were written by a psychopathic school principal.
  • The hardware platform was a 1970’s Russian copy of a 1960’s Apollo mission computer.
  • The development was outsourced to a South American software house (at which point the requirements were translated to Spanish). They subcontracted to a developer found on RentACoder who wrote the code in his bedroom.
  • I’ve been asked to debug and prove the programs correctness with only a fan-fold dot-matrix source code listing.

I think I did fairly well. It certainly makes you think hard. The questions are perfectly fair, but are designed to lead you towards false assumptions and dangerous short-cuts.

The things you go through to get a good job!

10
May
09

Firefox: Always restore your previous session without the prompt

I often shutdown my machine without closing Firefox. Next time I restart the browser it asks me if I want to resume the previous session.

I always want to restore as the first 5 or 6 tabs are my ‘always open’ apps (GMail, Google Reader, etc,). Clicking ‘Resume Previous Session’ all the time really hacks me off.

Now a bit of Googling turns up innumerable posts about disabling session restore so you always start a new session, but finding information about the opposite behaviour, i.e. always restoring the previous session (without asking) is much harder to find.

In order to raise the profile of this feature, I’m linking to the only other post I know about that talks about this. Ironically I only found this after browsing the source and ‘reverse engineering’ my Google query.

The solution is a bit of a hack as the behaviour I require isn’t directly supported. However Firefox allows extensions to override the default prompt and we can exploit this.

Head to about:config and create a string key ‘browser.sessionstore.restore_prompt_uri‘ and give it a value of ‘javascript:window.close();‘. This tell Firefox that a custom session restore prompt has been provided… which is immediately terminated.

Luckily the session restore code assumes that we want to restore unless the prompt returns otherwise and the net result is no prompt and automatic session restoration.

I hope this takes a little bit of friction out of your day.

07
Apr
09

Australia’s National Broadband Network

Kevin Rudd, the Australian PM has just announced a National Broadband Network providing fibre to the home at speeds of 100Mb/s within the next eight years.

That’s pretty exciting! Except that broadband plans in Oz tend to be capped. I just worked out it would take me 34.1 minutes to use my current monthly quota at this speed.

14
Jan
09

Hi!

Hi, just a quick introduction…

I’m a .NET developer, based in Melbourne, Australia.

I’ve decided it’s time to stop lurking in the ALT.NET forums and start contributing! Here’s where I plan to do it.