0

I have a couple classes in a project I'm working on that only register event handlers.

public class EventLogger
{
    public EventLogger(EventRaiser someObject)
    {
        someObject.EventRaised += logThatEvent;
    }
}

The EventLogger is either running or not based on some run time status checks at program startup, so I initialize it in a factory class:

public class EventLoggerFactory
{
    public EventLogger GenerateEventLogger(StatusChecker status, EventRaiser objectToListenTo)
    {
        if (status.EventLoggerNeeded)
        {
            return new EventLogger(objectToListenTo);
        }
    return null; // Spring.NET doesn't let you return null
}

and in Spring.NET config:

<object id="EventLogger" type="MyProject.EventLogger" factory-object="EventLoggerFactory" factory-method="GenerateEventLogger">
    <constructor-arg name="status" ref="StatusChecker"/>
    <constructor-arg name="objectToListenTo" ref="EventRaiser"/>
</object>

Since I can't return null for my factory method, I've tried the following:

public class EventLogger
{
    // Actual implementation elided
    private EventLogger(){}
    internal static EventLogger DummyInstance
    {
        get { return new EventLogger(); }
    }
}

public class EventLoggerFactory
{
    public EventLogger GenerateEventLogger(StatusChecker status, EventRaiser objectToListenTo)
    {
        if (status.EventLoggerNeeded)
        {
            return new EventLogger(objectToListenTo);
        }
    return EventLogger.DummyInstance; 
}

Is this a valid use of the Dummy Object pattern? It feels weird to me to have to adjust the internals of my class to fit the Dependency Injection container I'm using, but I'm unable to find another way.

(Also I'm tagging this spring because I can't create a spring.net tag)

1

Yes, this is a very valid use of the Null Object (or Dummy Object) pattern.

I'd agree that it feels a lot like you are actually catering to your framework, but a lot of logging frameworks actually advocate this approach. It limits the amount of NULL-checks you need on the logging object itself. The .NET DI container Castle actually recommends this pattern for property-based injection of a logger.

  • This is similar to a Sentinal Value en.wikipedia.org/wiki/Sentinel_value It's a special value that signifies that your code should behave differently. I think the only requirement for using one is that it has the same type as an actual value, but it will not be realistically returned. So -1 could be a sentinal value for an algorithm that counts things but not for an algorithm that solves equations. – user2023861 Mar 7 '16 at 16:53
  • It's not really a sentinel value... Sentinel values have something to do with lists or sequences and work as a value that indicates the end of said list or sequence. Null Objects are specific objects that are designed to act as an object that has no specific behaviour. – JDT Mar 7 '16 at 18:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.