Finding deep assembly dependencies
Published by marco on
Quino contains a Sandbox in the main solution that lets us test a lot of the Quino subsystems in real-world conditions. The Sandbox has several application targets:
- Remote Data Server
- WebAPI Server
The targets that connect directly to a database (e.g. WPF, Winform) were using the PostgreSql driver by default. I wanted to configure all Sandbox applications to be easily configurable to run with SqlServer.
Just add the driver, right?
This is pretty straightforward for a Quino application. The driver can be selected directly in the application (directly linking the corresponding assembly) or it can be configured externally.
Naturally, if the Sandbox loads the driver from configuration, some mechanism still has to make sure that the required data-driver assemblies are available.
The PostgreSql driver was in the output folder. This was expected, since that driver works. The SqlServer was not in the output folder. This was also expected, since that driver had never been used.
I checked the direct dependencies of the Sandbox Winform application, but it didn’t include the PostgreSql driver. That’s not really good, as I would like both SqlServer and PostgreSql to be configured in the same way. As it stood, though, I would be referencing SqlServer directly and PostgreSql would continue to show up by magic.
Before doing anything else, I was going to have to find out why PostgreSql was included in the output folder.
I needed to figure out assembly dependencies.
My natural inclination was to reach for NDepend, but I thought maybe I’d see what the other tools have to offer first.
Does Visual Studio include anything that might help? The “Project Dependencies” shows only assemblies on which a project is dependent. I wanted to find assemblies that were dependent on PostgreSql. I have the Enterprise version of Visual Studio and I seem to recall an “Architecture” menu, but I discovered that these tools are no longer installed by default.
According to the VS support team in that link, you have to install the “Visual Studio extension development” workload in the Visual Studio installer. In this package, the “Architecture and analysis tools” feature is available, but not included by default.
Hovering this feature shows a tooltip indicating that it contains “Code Map, Live Dependency Validation and Code Clone detection”. The “Live Dependency Validation” sounds like it might do what I want, but it also sounds quite heavyweight and somewhat intrusive, as described in this blog from the end of 2016 (MSDN). Instead of further modifying my VS installation (and possibly slowing it down), I decided to try another tool.
What about ReSharper? For a while now, it’s included project-dependency graphs and hierarchies. Try as I might, I couldn’t get the tools to show me the transitive dependency on PostgreSql that Sandbox Winform was pulling in from somewhere. The hierarchy view is live and quick, but it doesn’t show all transitive usages.
The graph view is nicely rendered, but shows dependencies by default instead of dependencies and usages. At any rate, the Sandbox wasn’t showing up as a transitive user of PostgreSql.
I didn’t believe ReSharper at this point because something was causing the data driver to be copied to the output folder.
NDepend to the rescue
So, as expected, I turned to NDepend. I took a few seconds to run an analysis and then right-clicked the PostgreSql data-driver project to select
NDepend => Select Assemblies… => That are Using Me (Directly or Indirectly) to show the following query and results.
Sandbox.Model is indirectly referencing the PostgreSql data driver, via a transitive-dependency chain of 4 assemblies. Can I see which assemblies they are? Of course I can: this kind of information is best shown on a graph, so you can show a graph of any query results by clicking
Export to Graph to show the graph below.
Now I can finally see that the
SandboxModel pulls in the
Quino.Testing.Models.Generated (to use the
BaseTypes module) which, in turn, has a reference to
Quino.Tests.Base which, of course, includes the PostgreSql driver because that’s the default testing driver for Quino tests.
Now that I know how the reference is coming in, I can fix the problem. Here I’m on my own: I have to solve this problem without NDepend. But at least NDepend was able to show me exactly what I have to fix (unlike VS or ReSharper).
I ended up moving the test-fixture base classes from
Quino.Testing.Models.Generated into a new assembly called
Quino.Testing.Models.Fixtures. The latter assembly still depends on
Quino.Tests.Base and thus the PostgreSql data driver, but it’s now possible to reference the Quino testing models without transitively referencing the PostgreSql data driver.
A quick re-analysis with NDepend and I can see that the same query now shows a clean view: only testing code and testing assemblies reference the PostgreSql driver.
And now to finish my original task! I ran the Winform Sandbox application with the PostgreSql driver configured and was greeted with an error message that the driver could not be loaded. I now had parity between PostgreSql and SqlServer.
The fix? Obviously, make sure that the drivers are available by referencing them directly from any Sandbox application that needs to connect to a database. This was the obvious solution from the beginning, but we had to quickly fix a problem with dependencies first. Why? Because we hate hacking. :-)
Two quick references added, one build and I was able to connect to both SQL Server and PostgreSql.