Skip to content
This repository has been archived by the owner on Jan 10, 2024. It is now read-only.

Commit

Permalink
Added a .NET example.
Browse files Browse the repository at this point in the history
  • Loading branch information
simonbrowndotje committed Jan 10, 2022
1 parent e71acea commit 1af58e8
Showing 1 changed file with 40 additions and 3 deletions.
43 changes: 40 additions & 3 deletions docs/cookbook/dsl-and-code/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# DSL and code

It's possible to use both the DSL and the Structurizr for Java library together - perhaps you want to define a basic model via the DSL, and use automatic extraction to add components to the model. To do this, define your workspace using the DSL as normal, for example:
It's possible to use both the DSL and a code-based library together - perhaps you want to define a basic model via the DSL, and use automatic extraction to add components to the model. To do this, define your workspace using the DSL as normal, for example:

```
workspace {
Expand All @@ -24,7 +24,14 @@ workspace {
}
```

You can then write a Java program that parses this definition, and extends the workspace further; for example:
## Java

Since the DSL parser is written in Java, you can use it directly by adding a dependency on the `structurizr-dsl` library, which is available on [Maven Central](https://search.maven.org/artifact/com.structurizr/structurizr-dsl):

- groupId: `com.structurizr`
- artifactId: `structurizr-dsl`

You can then write a Java program that parses your DSL definition, and extends the workspace further; for example:

```
StructurizrDslParser parser = new StructurizrDslParser();
Expand All @@ -42,4 +49,34 @@ componentView.addDefaultElements();
componentView.enableAutomaticLayout();
```

You'll need to include a dependency on the `structurizr-dsl` library, which is available on [Maven Central](https://search.maven.org/artifact/com.structurizr/structurizr-dsl). Please note that the Structurizr for Java library is designed to be *append only*, so it's not possible to remove/modify elements/relationships that already exist in the model.
Please note that the Structurizr for Java library is designed to be *append only*, so it's not possible to remove/modify elements/relationships that already exist in the model.

## Other languages

Ports of the "Structurizr for Java" library are available for a number of other programming languages - see [https://structurizr.org/#authoring](https://structurizr.org/#authoring) for links. Although you can't use the DSL parser directly, you can achieve the same effect with the following steps:

### 1. Convert your DSL workspace to the JSON format

This can be done using the [Structurizr CLI export command](https://github.com/structurizr/cli/blob/master/docs/export.md):

```
structurizr export -workspace workspace.dsl -format json
```

### 2. Load the JSON file, and add to the workspace

A Structurizr for .NET version of the above example is as follows:

```
FileInfo fileInfo = new FileInfo("workspace.json");
Workspace workspace = WorkspaceUtils.LoadWorkspaceFromJson(fileInfo);
Container webApplication = workspace.Model.GetSoftwareSystemWithName("Software System").GetContainerWithName("Web Application");
// add components manually or via automatic extraction
...
// add a component view
ComponentView componentView = workspace.Views.CreateComponentView(webApplication, "Components", "Description");
componentView.AddDefaultElements();
componentView.EnableAutomaticLayout();
```

0 comments on commit 1af58e8

Please sign in to comment.