[NoBrainer] Simplifying Sparx Enterprise Architect Development with C# Interactive (CSI)

Developing an AddIn for Sparx Enterprise Architect is tedious, even with the existence of support such as the great work of Geert Bellekens. However, since Visual Studio 2015 we can use the C# Interactive Console which allows us to fire up an instance of Enterprise Architect and test our code interactively before putting it into classes and methods.

Note: I am not advising not to use Unit and Integration tests and to only rely on interactive tests. Not at all! But I found for myself that “playing around” with some API calls or figuring out how various elements in the EA API belong together went much faster than running unit tests and having to restart EA for every code/compile/test run.

So what do we need to start working in Visual Studio interactively with EA? It does not take much:

  1. Start a C# Interactive Console
  2. Load all required assemblies via the #r syntax
  3. Add some using statements you will need.
  4. Instantiate an EA.Repository class and open a repository.
  5. (optional) Show the main EA window

To facilitate the process of instantiating an EA.Reporitory class I use a RepositorySingleton class which you find below. It accepts a path to an EA reporitory file and also implements IDisposable so we can correctly terminate a running EA instance.

So this is really all it takes to speed up your C# development with Sparx Enterprise Architect.

Note: I tested with VS 2017 v15.8.7 and EA 14 v14.0.1422.

CSharpInteractiveConsole

C# Interactive Console (CSI)

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.Contracts;
using System.IO;
using System.Threading;
namespace biz.dfch.CS.EA.ProductModeler.Tests.Repository
{
public class RepositorySingleton : IDisposable
{
private static readonly object _syncRoot = new object();
private static readonly List<Action> _cleanupActions = new List<Action>();
private static int _instances;
private static string _repoPathAndFileName;
private static readonly Lazy<global::EA.Repository> _repository = new Lazy<global::EA.Repository>(() =>
{
var repository = new global::EA.Repository();
Contract.Assert(null != repository);
_cleanupActions.Add(() => repository.Exit());
var isOpen = repository.OpenFile(_repoPathAndFileName);
Contract.Assert(isOpen);
return repository;
});
// ReSharper disable once InconsistentlySynchronizedField
public global::EA.Repository Repository => _repository.Value;
public RepositorySingleton(string path)
{
Contract.Requires(File.Exists(path));
Interlocked.Increment(ref _instances);
if (_repository.IsValueCreated) return;
lock (_syncRoot)
{
if (_repository.IsValueCreated) return;
_repoPathAndFileName = path;
var result = _repository.Value;
}
}
private static void ReleaseUnmanagedResources()
{
if (0 != Interlocked.Decrement(ref _instances)) return;
_cleanupActions.Reverse();
foreach (var action in _cleanupActions)
{
try
{
action.Invoke();
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
}
}
public void Dispose()
{
ReleaseUnmanagedResources();
GC.SuppressFinalize(this);
}
~RepositorySingleton()
{
ReleaseUnmanagedResources();
}
}
}
/**
* Copyright 2018 d-fens GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#r "C:\src\biz.dfch.CS.EA.ProductModeler\src\biz.dfch.CS.EA.ProductModeler.Tests\bin\Debug\biz.dfch.CS.EA.ProductModeler.Tests.dll"
#r "C:\Program Files (x86)\Sparx Systems\EA\Interop.EA.dll"
using System;
using System.Diagnostics.Contracts;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using biz.dfch.CS.EA.ProductModeler.Tests.AddIn;
var path = @"C:\src\biz.dfch.CS.EA.ProductModeler\src\biz.dfch.PS.EA.ProductModeler.Scripts\Resources\Test.eapx";
var i = new biz.dfch.CS.EA.ProductModeler.Tests.Repository.RepositorySingleton(path);
// show EA UI, so you can interact with it manually
i.Repository.ShowWindow(1);
// get number of models in current EA repository
i.Repository.Models.OfType<EA.Package>().Count();
// get all top level models
i.Repository.Models.OfType<EA.Package>().Select(e => e.Name);
// clean up
i.Dispose();
/**
* Copyright 2018 d-fens GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: