AOP – Enforce initialization with PostSharp

Yan Cui

I help clients go faster for less using serverless technologies.

This article is brought to you by

Is your CI build step taking too long? Try Depot for free today and experience up to 40x faster build speed!

Unlock faster CI for FREE

Oh, the Pain!

Now and again we all come across an object which requires initialization before it can be used but with nothing there to stop us from trying to use it before it’s initialized.

In most cases you will get a seemingly unrelated exception (e.g. null reference when the object tries to use some resource which it expects to be set during initialization), which aren’t very helpful at all when you try to debug/identify the problem.

If you’re lucky, the object you’re using might have been designed with this in mind and gives you a meaningful exception when you try to call a method which requires the object to be initialized first, in which case it should be a straightforward to fix your code accordingly. For the guys designing the class though, this usually means they have to litter their code with validation logic and often means duplicated validation code or unnecessary inheritance just to encapsulate the validation logic.

Fortunately, with the help of PostSharp, a simple attribute can take care of this chore for you with ease!

Define high level interface

First off, you need a nice and simple interface like this:

/// <summary>
/// Marks a component which needs to be initialized before it can be used
/// </summary>
public interface IInitializable<T>
    bool IsInitialized { get; }

    void Initialize(T initObject);


The attribute will look something like this (in PostSharp 2):

[AttributeUsage(AttributeTargets.Method, Inherited = false)]
public sealed class InitializationRequiredAttribute : MethodInterceptionAspect
    public override bool CompileTimeValidate(MethodBase method)
        // make sure that the attribute is used in a type that implements the 
        // IInitializable interface
        if (!method.DeclaringType.GetInterfaces()
                   .Any(i => i.IsGenericType &&
                             i.GetGenericTypeDefinition() == typeof(IInitializable<>)))
            throw new Exception(string.Format(
                "The [{0}] attribute can only be used in types that implement the [{1}] interface",
                GetType(), typeof(IInitializable<>)));
        return base.CompileTimeValidate(method);

    /// <summary>
    /// Checks the IsInitialized property before allowing the call to proceed
    /// </summary>
    public override void OnInvoke(MethodInterceptionArgs eventArgs)
        var propertyInfo = eventArgs.Instance.GetType().GetProperty("IsInitialized");
        if (propertyInfo != null)
            var propertyValue = propertyInfo.GetValue(eventArgs.Instance, null);
            if (propertyValue != null && propertyValue is bool && (bool) propertyValue)
                // only proceed with the call if the instance has been initialized
        throw new InstanceNotInitializedException(eventArgs.Method.Name);

I have enforced a rule that this attribute can only be used in a class which implements our IInitializable interface, you can relax this rule by removing the overridden CompileTimeValidate method. However, in my experience it’s best to not leave any grey area in your code just in case you run into a situation where you had expected the attribute to do its job only to find out later that something’s gone wrong because you had accidentally/purposely removed the IInitializable interface from your class.


To use the attribute:

public class MyClass : IInitializable<string>
    public bool IsInitialized { get; private set; }
    public void Initialize(string initializationData)
        IsInitialized = true;

    public void DoSomething() { }

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.

1 thought on “AOP – Enforce initialization with PostSharp”

Leave a Comment

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