Over a million developers have joined DZone.

Structurizr: System Context Diagram as Code

DZone's Guide to

Structurizr: System Context Diagram as Code

· Java Zone ·
Free Resource

Get the Edge with a Professional Java IDE. 30-day free trial.

As I said in Resolving the conflict between software architecture and code, my focus for this year is representing a software architecture model as code. In Simple Sketches for Diagramming Your Software Architecture, I showed an example System Context diagram for my techtribes.je website.

techtribes.je System Context diagram

It's a simple diagram that shows techtribes.je in the middle, surrounded by the key types of users and system dependencies. It's your typical "big picture" view. This diagram was created using OmniGraffle (think Microsoft Visio for Mac OS X) and it's exactly that - a static diagram that needs to be manually kept up to date. Instead, wouldn't it be great if this diagram was based upon a model that we could better version control, collaborate on and visualize? If you're not sure what I mean by a "model", take a look at Models, sketches and everything in between.

This is basically what the aim of Structurizr is. It's a way to describe a software architecture model as code, and then visualize it in a simple way. The Structurizr Java library is available on GitHub and you can download a prebuilt binary. Just as a warning, this is very much a work in progress and so don't be surprised if things change! Here's some Java code to recreate the techtribes.je System Context diagram.

package com.structurizr.example;
import com.structurizr.io.json.JsonWriter;
import com.structurizr.model.Location;
import com.structurizr.model.Model;
import com.structurizr.model.Person;
import com.structurizr.model.SoftwareSystem;
import com.structurizr.view.SystemContextView;
import com.structurizr.view.ViewSet;
import java.io.StringWriter;
* This is a model of the system context for the techtribes.je system,
* the code for which can be found at https://github.com/techtribesje/techtribesje
public class TechTribesSystemContext {
public static void main(String[] args) throws Exception {
// create a model and the software system we want to describe
Model model = new Model("techtribes.je", "This is a model of the system context for the techtribes.je system, the code for which can be found at https://github.com/techtribesje/techtribesje");
SoftwareSystem techTribes = model.addSoftwareSystem(Location.Internal, "techtribes.je", "techtribes.je is the only way to keep up to date with the IT, tech and digital sector in Jersey and Guernsey, Channel Islands");
// create the various types of people (roles) that use the software system
Person anonymousUser = model.addPerson(Location.External, "Anonymous User", "Anybody on the web.");
anonymousUser.uses(techTribes, "View people, tribes (businesses, communities and interest groups), content, events, jobs, etc from the local tech, digital and IT sector.");
Person authenticatedUser = model.addPerson(Location.External, "Aggregated User", "A user or business with content that is aggregated into the website.");
authenticatedUser.uses(techTribes, "Manage user profile and tribe membership.");
Person adminUser = model.addPerson(Location.External, "Administration User", "A system administration user.");
adminUser.uses(techTribes, "Add people, add tribes and manage tribe membership.");
// create the various software systems that techtribes.je has a dependency on
SoftwareSystem twitter = model.addSoftwareSystem(Location.External, "Twitter", "twitter.com");
techTribes.uses(twitter, "Gets profile information and tweets from.");
SoftwareSystem gitHub = model.addSoftwareSystem(Location.External, "GitHub", "github.com");
techTribes.uses(gitHub, "Gets information about public code repositories from.");
SoftwareSystem blogs = model.addSoftwareSystem(Location.External, "Blogs", "RSS and Atom feeds");
techTribes.uses(blogs, "Gets content using RSS and Atom feeds from.");
// now create the system context view based upon the model
ViewSet viewSet = new ViewSet(model);
SystemContextView contextView = viewSet.createContextView(techTribes);
// and output the model and view to JSON (so that we can render it using structurizr.com)
JsonWriter jsonWriter = new JsonWriter(true);
StringWriter stringWriter = new StringWriter();
jsonWriter.write(viewSet, stringWriter);

Executing this code creates this JSON, which you can then copy and paste into the try it page of Structurizr. The result (if you move the boxes around) is something like this.

techtribes.je System Context diagram

Don't worry, there will eventually be an API for uploading software architecture models and the diagrams will get some styling, but it proves the concept. What we have then is an API that implements the various levels in my C4 software architecture model, with a simple browser-based rendering tool. Hopefully that's a nice simple introduction of how to represent a software architecture model as code, and gives you a flavour for the sort of direction I'm taking it. Having the software architecture as code provides some interesting opportunities that you don't get with static diagrams from Visio, etc and the ability to keep the models up to date automatically by scanning the codebase is what I find particularly exciting. If you have any thoughts on this, please do drop me a note.

Get the Java IDE that understands code & makes developing enjoyable. Level up your code with IntelliJ IDEA. Download the free trial.


Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}