Subclassable Enums

So today, I found myself needing to map exception types to http status codes for the purpose of looking up which status code to report back from any service endpoint invocation that has been interrupted by an unhandled exception.  Now, I could have simply setup a lazy instantiated static instance of a  Dictionary<Type, HttpStatusCode> somewhere and referred to it.  Or I could have setup a function with a switch statement on an exception parameter’s type and casing on typeof() calls on various exception types to return HttpStatusCodes.  Each of these has their drawbacks though.  The switch statement would only apply in this one case of translating the types.  The dictionary would only provide a map to translate from the exception type to an http status code.  If we ever decided that we wanted to make other decisions or take other actions based on an one of the exception types, we would either have to write more switch statements or expand the value type of the dictionary.

So I discussed with my colleague about using a subclassable enum.  I built a reference implementation using the Subclassable Enum implementation from AtomicStack.  Its class signature looked something like this:

public class ExceptionTypeEnum : SubclassableEnum<ExceptionTypeEnum, Type>

and its constructor looked like the following:

protected ExceptionTypeEnum(Type exceptionType, HttpStatusCode statusCode) : base(exceptionType) {}

This has led me write this post about what exactly a Subclassable Enum is and some of the ways it can be useful.  First, let’s start with some of the problems that the Subclassable Enum helps to solve.

One of the things that some people have wished that they could do in .Net is create enums based on string values.  With the standard enum class, the set of constants defined by the enum must have an underlying type that is an integral type.  If no type is specified, then the underlying type defaults to Int32.  The following is not a valid .Net enum:

public enum Status { active = "active", inactive = "inactive" }

However, using the Atomic.Net StringEnum class enables the following:

public class Status : StringEnum<Status>
{
    public static readonly Status Active   = new Status("active");
    public static readonly Status Inactive = new Status("inactive");

    protected Status(string status) : base(status) {}
}

For example, which can then be used in a parameter definition like in the SetStatus method in the following class:

public class Person
{
    public Status AccountStatus { get; private set; }
    public void SetStatus(Status status){ ... }
}

Another useful feature of subclassable enums is the ability to controllably allow others to extend the list of enum values.  As long as you don’t mark the enum class as sealed, then it is open to extension.  For example, consider the following extensions to the Status enum from above:

public class ExtendedStatus : Status
{
    public static readonly Status Pending = new ExtendedStatus("pending");
    public static readonly Status Locked  = new ExtendedStatus("locked");

    protected ExtendedStatus(string status) : base(status) {}
}

Now we can call the SetStatus method from above with any of the following calls:

public void CallSetStatus()
{
    Person person = new Person();
    // The original Status values work
    person.SetStatus(Status.Active);
    person.SetStatus(Status.Inactive);
    // The original Status values are available via ExtendedStatus too
    person.SetStatus(ExtendedStatus.Active);
    person.SetStatus(ExtendedStatus.Inactive);

    // The new Status values also work
    person.SetStatus(ExtendedStatus.Pending);
    person.SetStatus(ExtendedStatus.Locked);
}

With Subclassable Enums, iterating over the list of registered enums is simple.  For example consider the following iteration over the Status enum from above:

foreach(Status status in Status.AllValues) { ... }

Or you can iterate over their underlying values with the following:

foreach(String status in Status.AllNaturalValues) { ... }

Subclassable enums also benefit from being subclasses like any other.  You can define enums that are abstract and require subclass implementations that override abstract functionality.  For example, consider the following change to the Status enum:

public abstract class Status : StringEnum<Status>
{
    private class ActiveStatus : Status
    {
        public static readonly ActiveStatus instance = new ActiveStatus();
        private ActiveStatus() : ("active"){}

        public override List<Permission> GetAppPermissions() { ... }
    }

    private class InactiveStatus : Status
    {
        public static readonly InactiveStatus instance = new InactiveStatus();
        private InactiveStatus() : ("inactive"){}

        public override List<Permission> GetAppPermissions() { ... }
    }
    public static readonly Status Active = ActiveStatus.instance;
    public static readonly Status Inactive = InactiveStatus.instance;

    protected Status(string status) : base(status) {}

    public abstract List<Permissions> GetAppPermissions();
}

With a classical enum, you very likely would have had to define a helper utility method such as the following:

public static List<Permission> GetStatusPermissions(Status status)
{
    switch(status)
    {
        case Status.Active:
            ...
        case Status.Inactive:
            ...
    }
}

Now consider that you want to make another decision based on a status.  For example, let’s say that you want to optionally log user activity based on status.  With a classical enum you might write a helper utility method like the following:

public void LogActivityMessage(string message, Status status)
{
    switch(status)
    {
        case Status.Active
            // do nothing
            break;
        case Status.Inactive
            ...
    }
}

With the Status subclass of StringEnum you would simply add a new abstract method called LogActivity and call it the following way:

public void TestLogActivity()
{
    Person person = new Person();
    person.SetStatus(Status.Inactive);
    person.AccountStatus.LogActivity("...");
}

Since the LogActivity method is abstract, all enumerated values are now required to at least implement the method.  With the classical enums, the LogActivity utility method may have been defined far from the GetStatusPermissions method.  There is no guarantee that new statuses that are added to the classical enum actually get cases defined for them across the various related switch statements.

And finally, another benefit of subclassable enums is that you are not restricted to a single type of underlying value for the enum entry.  There may be times when an you would like an enum to represent two or more different types of values for a given entry.  Consider the following change to the Status enum for example:

public abstract class Status : StringIntegerEnum<Status>
{
    private class ActiveStatus : Status
    {
        public static readonly ActiveStatus instance = new ActiveStatus();
        private ActiveStatus() : ("active", 1){}

        public override List<Permission> GetAppPermissions() { ... }
    }

    private class InactiveStatus : Status
    {
        public static readonly InactiveStatus instance = new InactiveStatus();
        private InactiveStatus() : ("inactive", 0){}

        public override List<Permission> GetAppPermissions() { ... }
    }
    public static readonly Status Active = ActiveStatus.instance;
    public static readonly Status Inactive = InactiveStatus.instance;

    protected Status(string status, int statusCode) : base(status, statusCode) {}

    public abstract List<Permissions> GetAppPermissions();
}

Now each status presents both a string constant and an integer constant.  The StringIntegerEnum base class provides the ability to obtain both unique lists of underlying values as well as being able to substitute the enum entry for either a string or an integer.  So for example, the status might be stored in the database using its integer value, but may be operated on mostly by its string value in the middleware code.  Just as StringEnums are able to be converted to and from their string values, StringIntegerEnums are able to be converted to and from either their string values or their integer values.  This provides the ability to use subclassable enums as an enumerable mapping structure.

As you can see subclassable enums provide a greater degree of flexibility and versatility than classical enums.  There is a cost of course to subclassing enums.  In order to be derivable, these types are class instances and will not perform the same as classical enums.  But I think we can see that the trade off is likely worth it if you have any of the above requirements.  Additionally, avoiding the proliferation of switch statements based on classical enums all by itself may be justification enough.

Check out the subclassable enum implementation on Github in the AtomicStack project: SubclassableEnum.cs

Atomic Stack

So, I’ve been thinking about how I wanted to try to jump start my blog for a very long time.  The problem is, that for as much as I like technology, I’ve never really taken to the typed word.  I’ve always preferred to make a phone call rather than write up an email or send a text. For as fast as I can type, it seems that I can never really type fast enough to get my ideas recorded, except generally in perhaps the case of programming which is good, given that I’m a programmer.  But when it comes to free-flowing thoughts like those generally relayed via speech, I would definitely prefer to dictate than to actually type.  So, I’m going to attempt to use the Voice Memos iOS application as a way to stage the content for my blog.

Recently I decided to start an open source project, based on some opinions I had received at the St. Louis Days of .Net which echoed similar sentiments from the previous year’s conference.  You see, over the years I’ve been fortunate to have been tasked with solving some of the most difficult challenges faced by the various teams that I have been a part of.  I’ve also been fortunate to have worked with some very talented and skilled individuals on those teams.  Some I collaborated with and others I was literally schooled by.  I’ve somehow managed to hold on to the practices that have proven useful and advantageous in the various projects that I’ve worked on and assimilated them as recurring patterns.  I’ve described some of these patterns to certain individuals over the last few years, and nearly every time, I’ve been asked if any of it was embodied in an open source implementation.  The unfortunate answer has always been nothing that I’m involved with and nothing that I was aware of.

So the purpose of this open source project is to embody the set of tools that I will be implementing and leveraging during the course of the development of a personal closed source project of my own.  As such, requirements will flow from my personal project to the tool-set project.  The tool-set will be comprised of a stack of technologies that I will leverage to build an n-tier web application.  These technologies will be new implementations based upon the patterns that I have successfully leveraged over the years.  These will include things in the following list, which I plan to go into further detail of in future posts:

  • Configurable tier implementations across 8 tiers (data storage, data access/io, business enforcement, application services, service hosting, client side server access/io, client side controllers and client side views)
  • Three independent storage/transport schemas (data storage, entity model, and use case schemas)
  • Entity Model in memory indexing
  • Entity Model domain specific querying language built as a fluent api on top of .Net classes
  • Singleton/Multiton supporting services with lifetimes managed by an Abstract Factory implementation
  • Vertically integrated independent entity tiers relationally bound in the business tier with optimized distributed data access/io execution
  • Expansive configuration
  • Web server host abstraction (with IIS integration implementation, future implementations may include integration on top of OWIN)
  • Built in user account management and security authentication/authorization (including access challenge/negotiation)
  • Built in/expandable service hosting options (default web service implementation with multiple/expandable content negotiation options)
  • Subclassable Enums (and conversely enumerated subclasses, an excellent alternative to using switch statements)
  • Additional supporting data types
  • Advanced .net generics tricks (including subclass constraints/contexts and generic namespaces via nested classes)
  • General object based data storage services for supporting the practice of high fidelity functional prototyping
  • Interchangeable client side server proxies (supporting functional prototyping via an elaborate proxy implementation simulating future remote services)
  • Classical inheritance implementation on top of ECMAScript 5/JavaScript 1.8.5 with base class method call dispatching, public and protected access modifiers, instance and static scopes with constructors
  • Pure client side MVC solution implemented using JSON/HTML/JavaScript
  • and of course more…

The name of this set of tools is Atomic Stack.  The server side tiers (Atomic.Net) are being implemented in .Net using C# due its amazing generics support.  The client side tiers (AtomicWeb/AtomicJS) are being built upon HTML5/ECMAScript 5/CSS 3.  The project goals will including adherence to development practices including:

  • Separation of Concerns (including Unobtrusive JavaScript)
  • Clean Coding Principles
  • Tier/Down Design and Development (with wide client side development cycles with narrow vertical server side development sprints)

In addition I will be looking at employing additional practices not currently in use including the following:

  • Design by Contract (at least for application hosted services)
  • Test Driven Development

I’m sure that if anyone actually comes across this blog, that some may point out that there are a ton of frameworks and libraries out there.  They may question, do we really need yet another framework or library, much less a stack of them?  Frankly, I’m not sure.  I do know however that I have ideas and I would like to contribute those ideas in a tangible way to the community.  And due to my past experience, there is a certain degree of independent yet cohesiveness among these ideas which therefore are compelling me to attempt to start from scratch and create these new tools with minimal constraints solely upon the raw platforms that they are to be built upon (.Net, JavasScript, HTML, CSS, etc.).  Ofcourse I will very likely be incorporating additional dependencies upon things that are already well written and tested (like Mike Woodring’s DevelopMentor ThreadPool, HtmlAgilityPack and jQuery).  But it is very likely that I will avoid some tools whose implementations I find lacking (for example the MS Entity Framework).

Anyway, I droned on long enough and I’m not quite sure how to end this post.  If you are interested in checking out the project, please visit http://atomicstack.com.