title: Clean architecture cascaded deletion of resources
body: <p>Currently, I have been trying to convert a codebase using &quot;clean architecture&quot; to try to separate many things, create better defined interfaces, and make code more easily testable.</p>
  <p>Right now, we have resources which are related in a hierarchical manner. We can think of this as something like:</p>
  <pre><code>A
  |-B
  | |
  | C
  |
  |-D
    |-F
    |-G
      |-H
      |-I
  </code></pre>
  <p>This is a very rough example and meant more to show than a description of precisely what I am working with.</p>
  <p>In a case like this, I might have some &quot;UseCase&quot; where I want to &quot;delete A&quot;. In our case, when we delete a type in this hierarchy we want to remove all associated data including data of child type. This is where I am not sure how to approach things exactly. There are a few approaches I have considered and am looking for better options.</p>
  <p>The first would be to have a &quot;UseCase&quot; which has all this functionality (delete for all types) and can therefore reference itself to call other deletion methods. I really didn't like this as it seems to me to violate the &quot;open/closed principle&quot; and seems like it would be very hard to maintain.</p>
  <p>The second approach was to have the &quot;UseCase&quot; &quot;DeleteA&quot; depend on &quot;DeleteB&quot; and &quot;DeleteD&quot; (for example). &quot;DeleteA&quot; would then have dependencies of &quot;DeleteA &amp; DeleteB &amp; DeleteD&quot;. This seems like it makes it difficult to test since now it actually depends on interfaces of every child type &quot;Delete&quot; &quot;UseCase&quot;.</p>
  <p>I am struggling with how this should be approached. I have considered a service locator pattern which I also don't really like much. It seems like it makes dependencies unclear and they will only get resolved at runtime (no compile time check).</p>
  <p>Any suggestions are welcome. I have found it difficult to find information on cases like this. Examples I have found seem to be fairly isolated to something like &quot;DoSomethingWithX&quot; where the result doesn't cascade across many types.</p>