Splitting up assemblies in Quino using NDepend (Part I)

Published by Marco on

Updated by Marco on

 A lot of work has been put into Quino 2.0[1], with almost no stone left unturned. Almost every subsystem has been refactored and simplified, including but not limited to the data driver, the schema migration, generated code and metadata, model-building, security and authentication, service-application support and, of course, configuration and execution.

Two of the finishing touches before releasing 2.0 are to reorganize all of the code into a more coherent namespace structure and to reduce the size of the two monolithic assemblies: Encodo and Quino.

A Step Back

The first thing to establish is: why are we doing this? Why do we want to reduce dependencies and reduce the size of our assemblies? There are several reasons, but a major reason is to improve the discoverability of patterns and types in Quino. Two giant assemblies are not inviting—they are, in fact, daunting. Replace these assemblies with dozens of smaller ones and users of your framework will be more likely to (A) find what they’re looking for on their own and (B) build their own extensions with the correct dependencies and patterns. Neither of these is guaranteed, but smaller modules are a great start.

Another big reason is portability. The .NET Core was released as open-source software some time ago and more and more .NET source code is added to it each day. There are portable targets, non-Windows targets, Universal-build targets and much more. It makes sense to split code up into highly portable units with as few dependencies as possible. That is, the dependencies should be explicit and intended.

Not only that, but NuGet packaging has come to the fore more than ever. Quino was originally designed to keep third-party boundaries clear, but we wanted to make it as easy as possible to use Quino. Just include Encodo and Quino and off you went. However, with NuGet, you can now say you want to use Quino.Standard and you’ll get Quino.Core, Encodo.Core, Encodo.Services.SimpleInjector, Quino.Services.SimpleInjector and other packages.

With so much interesting code in the Quino framework, we want to make it available as much as possible not only for our internal projects but also for customer projects where appropriate and, also, possibly for open-source distribution.

NDepend

I’ve used NDepend before[2] to clean up dependencies. However, the last analysis I did about a year ago showed quite deep problems[3] that needed to be addressed before any further dependency analysis could bear fruit at all. With that work finally out of the way, I’m ready to re-engage with NDepend and see where we stand with Quino.

As luck would have it, NDepend is in version 6, released at the start of summer 2015. As was the case last year, NDepend has generously provided me with an upgrade license to allow me to test and evaluate the new version with a sizable and real-world project.

Here is some of the feedback I sent to NDepend (Twitter):

I really, really like the depth of insight NDepend gives me into my code. I find myself thinking “SOLID” much more often when I have NDepend shaking its head sadly at me, tsk-tsking at all of the dependency snarls I’ve managed to build.

  • It’s fast and super-reliable. I can work these checks into my workflow relatively easily.
  • I’m using the matrix view a lot more than the graphs because even NDepend recommends I don’t use a graph for the number of namespaces/classes I’m usually looking at
  • Where the graph view is super-useful is for examining *indirect* dependencies, which are harder to decipher with the graph
  • I’ve found so many silly mistakes/lazy decisions that would lead to confusion for developers new to my framework
  • I’m spending so much time with it and documenting my experiences because I want more people at my company to use it
  • I haven’t even scratched the surface of the warnings/errors but want to get to that, as well (the Dashboard tells me of 71 rules violated; 9 critical; I’m afraid to look :-)

Use Cases

Before I get more in-depth with NDepend, please note that there at least two main use cases for this tool[4]:

  1. Clean up a project or solution that has never had a professional dependency checkup
  2. Analyze and maintain separation and architectural layers in a project or solution

These two use cases are vastly different. The first is like cleaning a gas-station bathroom for the first time in years; the second is more like the weekly once-over you give your bathroom at home. The tools you’ll need for the two jobs are similar, but quite different in scope and power. The same goes for NDepend: how you’ll use it to claw your way back to architectural purity is different than how you’ll use it to occasionally clean up an already mostly-clean project.

Quino is much better than it was the last time we peeked under the covers with NDepend, but we’re still going to need a bucket of industrial cleaner before we’re done.[5]

The first step is to make sure that you’re analyzing the correct assemblies. Show the project properties to see which assemblies are included. You should remove all assemblies from consideration that don’t currently interest you (especially if your library is not quite up to snuff, dependency-wise; afterwards, you can leave as many clean assemblies in the list as you like).[6]

Industrial-strength cleaner for Quino

Running an analysis with NDepend 6 generates a nice report, which includes the following initial dependency graph for the assemblies.

 Assembly dependencies for Encodo and Quino

As you can see, Encodo and Quino depend only on system assemblies, but there are components that pull in other references where they might not be needed. The initial dependency matrices for Encodo and Quino both look much better than they did when I last generated one. The images below show what we have to work with in the Encodo and Quino assemblies.

 Initial Dependency Matrix for Encodo Initial Dependency Matrix for Quino

It’s not as terrible as I’ve made out, right? There is far less namespace-nesting, so it’s much easier to see where the bidirectional dependencies are. There are only a handful of cyclic dependencies in each library, with Encodo edging out Quino because of (A) the nature of the code and (B) I’d put more effort into Encodo so far.

I’m not particularly surprised to see that this is relatively clean because we’ve put effort into keeping the external dependencies low. It’s the internal dependencies in Encodo and Quino that we want to reduce.

Small and Focused Assemblies

 (Partial) Architecture Graph for Quino 2.0-beta3 Encodo assemblies for Quino 2.0-beta3 Quino assemblies for Quino 2.0-beta3The goal, as stated in the title of this article, is to split Encodo and Quino into separate assemblies. While removing cyclic dependencies is required for such an operation, it’s not sufficient. Even without cycles, it’s still possible that a given assembly is still too dependent on other assemblies.

Before going any farther, I’m going to list the assemblies we’d like to have. By “like to have”, I mean the list that we’d originally planned plus a few more that we added while doing the actual splitting.[7] The images on the right show the assemblies in Encodo, Quino and a partial overview of the dependency graph (calculated with the ReSharper Architecture overview rather than with NDepend, just for variety).

Of these, the following assemblies and their dependencies are of particular interest[8]:

  • Encodo.Core: System dependencies only
  • Encodo.Application: basic application support[9]
  • Encodo.Application.Standard: configuration methods for non-metadata applications that don’t want to pick and choose packages/assemblies
  • Encodo.Expressions: depends only on Encodo.Core
  • Quino.Meta: depends only on Encodo.Core and Encodo.Expressions
  • Quino.Meta.Standard: Optional, but useful metadata extensions
  • Quino.Application: depends only on Encodo.Application and Quino.Meta
  • Quino.Application.Standard: configuration methods for metadata applications that don’t want to pick and choose packages/assemblies
  • Quino.Data: depends on Quino.Application and some Encodo.* assemblies
  • Quino.Schema: depends on Quino.Data

This seems like a good spot to stop, before getting into the nitty-gritty detail of how we used NDepend in practice. In the next article, I’ll discuss both the high-level and low-level workflows I used with NDepend to efficiently clear up these cycles. Stay tuned!


[3] You can see a lot of the issues associated with these changes in the release notes for Quino 2.0-beta1 (mostly the first point in the “Highlights” section) and Quino 2.0-beta2 (pretty much all of the points in the “Highlights” section).
[4] I’m sure there are more, but those are the ones I can think of that would apply to my project (for now).
[5] …to stretch the gas-station metaphor even further.
[6]

Here I’m going to give you a tip that confused me for a while, but that I think was due to particularly bad luck and is actually quite a rare occurrence.

If you already see the correct assemblies in the list, you should still check that NDepend picked up the right paths. That is, if you haven’t followed the advice in NDepend’s white paper and still have a different bin folder for each assembly, you may see something like the following in the tooltip when you hover over the assembly name:

“Several valid .NET assemblies with the name {Encodo} have been found. They all have the same version. the one with the biggest file has been chosen.”

If NDepend has accidentally found an older copy of your assembly, you must delete that assembly. Even if you add an assembly directly, NDepend will not honor the path from which you added it. This isn’t as bad as it sounds, since it’s a very strange constellation of circumstances that led to this assembly hanging around anyway:

  • The project is no longer included in the latest Quino but lingers in my workspace
  • The version number is unfortunately the same, even though the assembly is wildly out of date

I only noticed because I knew I didn’t have that many dependency cycles left in the Encodo assembly.

[7] Especially for larger libraries like Quino, you’ll find that your expectations about dependencies between modules will be largely correct, but will still have gossamer filaments connecting them that prevent a clean split. In those cases, we just created new assemblies to hold these common dependencies. Once an initial split is complete, we’ll iterate and refactor to reduce some of these ad-hoc assemblies.
[8] Screenshots, names and dependencies are based on a pre-release version of Quino, so while the likelihood is small, everything is subject to change.
[9] Stay tuned for an upcoming post on the details of starting up an application, which is the support provided in Encodo.Application.