VSTA Layer Diagram and MEF– Does my Design Smell?

Did some playing with MEF and a Smelling Layer Diagram today… it turned out to be an interesting scenario.


Some background.
There are many anti-patterns, patterns which have been proven not to work well. Anti-patterns for buildings, for collaboration, for businesses, for code and for sure for design. Some are common, like cyclomatic dependencies between components, some are more specific for a company and even every designer has some anti-patterns made for himself. Every designer, application architect or anybody else involved in designing something knows them or should know them to make a better design and finally application. So, it would be interesting to get a notification when your design SMELLS according to those generic or specific Anti-Patterns, these kind of notifications would help the design, application, business, developers, testers, operations and even better would help the learning curve for new designers/ architects.
The problem, there are to many anti-patterns as a designer you want to pick your own selection or even make your own anti-pattern notification mechanism, giving notifications, warnings at design time not at build time but at design time so you can evolve your design to a really good one.

Now MEF comes insight,  actually it came insight by me this morning on the metro to the office [have to go by public transportation since I did my car away last month]. During the ride I watched a webcast about MEF and decided… that’s really cool for this Anti-Pattern scenario. Great an Add-in based on MEF which validates, analyses your design according to several Anti-Patterns and with MEF in place people can add them at will.

The User Story
I created an Add-in, see image “LayerDiagramSmell”


This Add-in has adds a Menu-Item “does my design smell..?” to the diagram [in my scenario the Layer Diagram]


When you execute this command it checks your design at several Anti-Patterns… and shows his findings in the design. I only change the name in this implementation, but it’s very easy to change colors of lines, shapes and give notifications in the “Error List” [it’s just a pilot].


Now the designer/ architect can think, maybe I have to change this implementation….

The Implementation
I already explained the making of Add-in’s for diagrams in a previous post and MEF is really easy, here is some code…

1. add a reference to System.ComponentModel.Composition.

2. get the Smells binairies

            var catalog = new AggregatingComposablePartCatalog();
catalog.Catalogs.Add(new DirectoryPartCatalog(@"C:\temp\Smells"));
var container = new CompositionContainer(catalog.CreateResolver());

3. Create the Smell Classes.


Smell1 and Smell2 put there DLL’s in the “C:\temp\smells” directory which are loaded in the container and are executed [used for analyzing the diagram].I created them in the same solution but anyone who makes a class which implements the SmellInterface and puts the assembly in the Smells directory has his own Anti-Patterns analyzer.

4. implement some logic for analyzing the diagram, this is Smell2 and a really really basic implementation [It should be better for me not to post this kind of code, anyway… it works ]

public class GetSmell : ISmell
public List<Layer> DoesThisSmell(LayerModel layerModel)
List<Layer> LayersThatSmell = new List<Layer>();
foreach (Layer item in layerModel.Layers)
foreach (var dependencyFrom in item.DependencyFromLayers)
if (item.DependencyToLayers.Contains(dependencyFrom))
return LayersThatSmell;

5. And finally put the Anti-Pattern information back on the diagram.

 List<Layer> LayersThatSmells = ModelElementsSmell.DoesThisSmell(loadedModel);
using (Transaction t = loadedModel.Store.TransactionManager.BeginTransaction("anaylis diagram"))
foreach (var item in LayersThatSmells)
item.Name = "THIS  STINKS" + item.Name;
SerializationResult result = new SerializationResult();
LayerDesignerSerializationHelper.Instance.SaveModelAndDiagram(result, loadedModel, _applicationObject.ActiveDocument.FullName, diagram, _applicationObject.ActiveDocument.FullName + ".DIAGRAM");

I know not enough information to reproduce this example, but I'm curious what you think of this scenario and for sure what are your Anti-Patterns…
More on this in the future….

Comments (4) -

Hi Clemens,

very Nice ! This has a lot of potential. Note to self, check out MEF.

i love to  read articles like yours that is posted in your blogsite thanks for sharing i really
like it.

i love to read articles like yours that is posted in your blogsite thanks for sharing i really
like it.

i love to read articles like yours that is posted in your blogsite thanks for sharing i really
like it.

Add comment