Castle Windsor Tips – say NO to private setter

Yan Cui

I help clients go faster for less using serverless technologies.

This article is brought to you by

Hookdeck: The Serverless Event Gateway

Hookdeck is a reliable and scalable serverless event gateway for sending, receiving, authenticating, transforming, filtering, and routing events between services in your event-driven architecture.

Learn more

Now, this is something that has stung me and every single one of my colleagues time and time again..

Imagine you have an interface called IMyConfiguration which has a get-only property:

public interface IMyConfiguration
{
    int Port { get; }
}

And the implementing class takes in a parameter called port in the constructor and sets the value of the property using a private setter on the property:

public class MyConfiguration : IMyConfiguration
{
    public MyConfiguration(int port)
    {
        Port = port;
    }

    public int Port { get; private set; }
}

All looks fine, you set up your castle config to pass in an integer called port:

<component id="MyConfiguration"
           service="MyAssembly.IMyConfiguration, MyAssembly"
           type="MyAssembly.MyConfiguration, MyAssembly"
           lifestyle="singleton">
    <parameters>
        <port>1234</port>
    </parameters>
</component>

And you hit Run, and immediately you run into a Castle exception, the exception message is less than useful as it only says ”Object reference not set to an instance of an object.” and the stack trace (see below) doesn’t reveal anything useful either..

image

If this sounds familiar to you then you should make a note to self as I have done and remember to not use a private setter on a property defined in a class that’s going to be injected into your application by Castle!

To fix this error, you will need to change your implementation to something like this instead:

public class MyConfiguration : IMyConfiguration
{
    private readonly int _port;

    public MyConfiguration(int port)
    {
        _port = port;
    }

    public int Port { get { return _port; } }
}

Whenever you’re ready, here are 4 ways I can help you:

  1. Production-Ready Serverless: Join 20+ AWS Heroes & Community Builders and 1000+ other students in levelling up your serverless game. This is your one-stop shop for quickly levelling up your serverless skills.
  2. Do you want to know how to test serverless architectures with a fast dev & test loop? Check out my latest course, Testing Serverless Architectures and learn the smart way to test serverless.
  3. I help clients launch product ideas, improve their development processes and upskill their teams. If you’d like to work together, then let’s get in touch.
  4. Join my community on Discord, ask questions, and join the discussion on all things AWS and Serverless.

2 thoughts on “Castle Windsor Tips – say NO to private setter”

  1. Hi Krzysztof, that’s excellent news! I believe we’re still running v2.1, let me try to reproduce the same exception with v2.5 beta2 and update accordingly.

Leave a Comment

Your email address will not be published. Required fields are marked *