Featurewise

Featurewise organisation

Featurewise organisation is the name I use for the way I've come to write software: building a service around its features, the things it does. I've arrived at it over several years, from conversations I've had and things I've seen and read.

It's influenced by Jimmy Bogard's Vertical slice architecture and his MediatR package. One practice I take from them is to use classes to represent the inputs for a feature, like the command objects in MediatR.

Take SubmitApplication, a command that a user issues to a service. When it runs, it finds an existing stored application, modifies its state, and probably does other things like sending a confirmation email or raising an event.

There will be a class that's used to invoke SubmitApplication - it could be called SubmitApplicationArgs and it will contain the information that represents the request e.g. the application's id.

class SubmitApplicationArgs
{
    public string ApplicationId { get; set; }
}

Representing the args like that, we can write the SubmitApplication feature something like this:

class SubmitApplication
{
    void Invoke(SubmitApplicationArgs args)
    {
        // look up the application by args.ApplicationId
        // update its state
        // send out a notification
    }
}

Another influence is Alistair Cockburn's Ports and adapters, also called Hexagonal architecture. I don't follow all of its practices all of the time, but one I always do follow is being careful about which parts of the code belong in the service's Core, and which belong elsewhere.

In Core are the classes that implement the service's operations (Hexagonal calls them use cases; I prefer to call them features), the types that represent the service's domain, and the contracts the features depend on, such as somewhere to store applications and a way to send a notification.

Outside Core are the entrypoints: the things that call into it, like an API or a scheduler, which Hexagonal calls driving adapters. Also outside are the implementations of those contracts, such as a particular database or email provider, which are Hexagonal's driven adapters. The entrypoints and implementations know about Core; Core doesn't know about them.

Core holds features (driving ports, implemented in Core), contracts (driven ports) and domain types. Outside it on one side are entrypoints, an API and a scheduler, with arrows into the features; on the other are implementations, a database and an email provider, with arrows into the contracts. Features depend on the contracts.
Every arrow points into Core: the entrypoints call the features, the implementations fulfil the contracts, and Core knows about neither.

Putting vertical slices together with Hexagonal architecture seems very natural, and it's done often. I prefer the word "feature", because I think it travels better to stakeholders beyond developers than "use case" or "vertical slice". Product owners, testers, marketers, customer support people can all talk about the service in terms of the features it contains.

All this is consistent with modern orthodoxy in software development. Lots of software projects are arranged broadly this way. But to this, featurewise organisation adds one more idea, which I don't think is so commonly used. This is to treat the objects that you use to invoke a feature as links.

Looking again at SubmitApplicationArgs, you can see that if you have an instance of this type, you have what you need to invoke the SubmitApplication feature. The type clearly relates to the feature it's intended to invoke, and its one property is the information needed to invoke it. That makes it exactly what a link is.

Links (of course) are very commonly used, but they are more often expressed in terms of a particular entrypoint to a service - like the URL of an API endpoint. Because of this, they don't belong naturally in Core, which has no knowledge of the entrypoints.

Using features' arguments as links brings the links right into Core. Links can be generated inside the features' Invoke methods. Here's SubmitApplication again, this time returning a result. Once an application is submitted, the applicant can withdraw it, so the result carries a link to WithdrawApplication:

record Submitted(WithdrawApplicationArgs LinkToWithdraw);

class SubmitApplication
{
    Submitted Invoke(SubmitApplicationArgs args)
    {
        // look up the application, update its state, send out a notification
        return new Submitted(new WithdrawApplicationArgs { ApplicationId = args.ApplicationId });
    }
}

Nothing in there says how the link will be followed, and that's what lets it go anywhere. A web UI can show it as a Withdraw button beside the application. And the notification email can carry it too, as a URL to follow if the applicant changes their mind: the feature makes the link, and whatever sends the email turns it into a URL. The register-for-updates service on this site works like that. The links in its emails, to confirm and to unregister, are made in Core.

I'm writing about featurewise organisation now because the "links in Core" idea keeps turning out to matter more than I expected. Links become easier and cheaper to generate, and that means we can have more of them. Links do a lot for the usability and accessibility of software - the World Wide Web is built on them - and putting them in Core brings this usability into Core too. Personifying them, I like to think of featurewise, link-emitting services as being like deeply pleasant individuals with delightful manners. They want to help you.

I hope to write more about these ideas soon, starting with a way of drawing a service's features and the links between them. If you find them interesting too, please leave your reactions, comments and follow-on ideas in the discussion.

Comments

Comments are GitHub Discussions: sign in with GitHub to join in.