structurizr / dsl

Structurizr DSL
https://docs.structurizr.com/dsl
Apache License 2.0
1.41k stars 266 forks source link

Using !ref within enterprise #129

Closed gp-mlezotte closed 2 years ago

gp-mlezotte commented 2 years ago

I've been experimenting with using !ref with !include to make my DSL files cleaner and more focused. I had a lot of trouble at first getting it working, and was always receiving the following error in Structurizr Lite:

workspace.dsl: Unexpected tokens at line X: !ref test { at line Y....

I eventually tried simplifying my DSL and removing the enterprise wrapper. When I did this, the !refs started working.

Is it possible to use !ref within enterprise? If not, could this functionality be added or are there any suggestions for working around it? If it helps, I've been using non-hierarchical identifiers, but plan to switch to hierarchical identifiers in the future.

simonbrowndotje commented 2 years ago

!ref is designed to be used with the workspace extension feature, so any elements should have already been defined inside an enterprise in the base workspace. Can you provide an example of what you're trying to do?

gp-mlezotte commented 2 years ago

My original thought was that I might be able to declutter some of my DSL files by migrating the perspectives into separate files themselves. After thinking it through a little more, I came to the conclusion that maybe I was trying to separate it out a little too much, and that it might not be worth the effort given that it seems like I would need to add an identifier for everything, including relationships. I would love it if I could just "layer" a perspective file on top of a structure file without having to do anything special in the structure file. I may revisit this in the future... it was mainly an exercise to help me understand some functionality in Structurizr.

Maybe a couple of examples will help to illustrate where I was getting the error.

This DSL will work fine:

workspace "Test" "Description" {
    model {
        testSystem = softwareSystem "Test System"

        !ref testSystem {
            container "Test Container"
        }
    }
}

This will throw the error I mentioned above:

workspace "Test" "Description" {
    model {
        enterprise "Test Company" {
            testSystem = softwareSystem "Test System"

            !ref testSystem {
                container "Test Container"
            }
        }
    }
}

The only difference is that in the second example, the definitions and !ref are wrapped in an enterprise.

simonbrowndotje commented 2 years ago

This will work though:

workspace "Test" "Description" {
    model {
        enterprise "Test Company" {
            testSystem = softwareSystem "Test System"
        }

        !ref testSystem {
            container "Test Container"
        }
    }
}

And the software system is still rendered inside the enterprise.

Screenshot 2022-04-18 at 16 38 42

simonbrowndotje commented 2 years ago

I would love it if I could just "layer" a perspective file on top of a structure file without having to do anything special in the structure file.

You could do this via the plugin mechanism:

  1. Create yourself a file detailing all of the perspectives.
  2. Write a plugin that parses this file, and applies the perspectives to the relevant element(s).

The DSL is a just a wrapper for the Structurizr for Java library, so you have the full power of that at your disposal.

gp-mlezotte commented 2 years ago

I didn't think to move the !ref outside of the enterprise. This is exactly what I was looking for. That's a great suggestion for using the Java library as well. I'm a .NET developer by trade, but it's looking like I'll have to get into Java a little more to take full advantage of Structurizr. Thanks so much!

simonbrowndotje commented 2 years ago

You can also do something similar with the .NET library, although you'd need to convert the DSL to JSON (with the Structurizr CLI) as a first step -> https://github.com/structurizr/dsl/tree/master/docs/cookbook/dsl-and-code#other-languages

gp-mlezotte commented 2 years ago

Thanks again! That looks like it could be very useful as well. I really appreciate all the extensibility options!