{
"$type": "site.standard.document",
"canonicalUrl": "https://johnnyreilly.com/posts/using-reflection-to-identify-unwanted-dependencies",
"description": "Learn how to identify unwelcome dependencies in complex web apps by walking a dependency tree using reflection-based tests.",
"path": "/posts/using-reflection-to-identify-unwanted-dependencies",
"publishedAt": "2018-04-28T00:00:00.000Z",
"site": "at://did:plc:yy3apqjlms24kso7ahn7lbmb/site.standard.publication/3mova7c4nho2b",
"tags": [
"asp.net"
],
"textContent": "I having a web app which is fairly complex. It's made up of services, controllers and all sorts of things. So far, so unremarkable. However, I needed to ensure that the controllers did not attempt to access the database via any of their dependencies. Or their dependencies, dependencies. Or their dependencies. You get my point.\n\n\n\nThe why is not important here. What's significant is the idea of walking a dependency tree and identifying, via a reflection based test, when such unwelcome dependencies occur, and where.\n\nWhen they do occur the test should fail, like this:\n\nWhat follows is an example of how you can accomplish this. It is exceedingly far from the most beautiful code I've ever written. But it works. One reservation I have about it is that it doesn't use the Dependency Injection mechanism used at runtime (AutoFac). If I had more time I would amend the code to use that instead; it would become an easier test to read if I did. Also it would better get round the limitations of the code below. Essentially the approach relies on the assumption of there being 1 interface and 1 implementation. That's often not true in complex systems. But this is good enough to roll with for now.",
"title": "Using Reflection to Identify Unwanted Dependencies"
}