[#inverting-dependencies]
= Inverting Dependencies

:Notice: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at. http://www.apache.org/licenses/LICENSE-2.0 . Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR  CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
:page-partial:


If we get the dependencies wrong (that is, our initial guess on stability is proven incorrect over time) then there are a couple of techniques we can use:

* use the link:https://en.wikipedia.org/wiki/Dependency_inversion_principle[dependency inversion principle] to introduce an abstraction representing the dependency.

* move functionality, eg by factoring it out into xref:mixins.adoc[mixins] into the other module or into a third module which depends on the other modules that had a bidirectional relationship

Mixins in particular allow dependencies to be inverted, so that the dependencies between modules can be kept acyclic and under control.

For example, suppose that we send out ``Invoice``s to ``Customer``s.
We want the invoices to know about customers, but not vice versa.
We could surface the set of invoices for a customer using a `Customer_invoices` mixin:

[plantuml]
.`invoices` module contributes to `customers`
----
include::docs:resources:partial$plantuml-stereotypes.puml[]

package invoice {
    class Customer_invoices <<mixin>>
    class Invoice <<mi>>
}
Customer_invoices --> "0.*" Invoice: invoices

package customers {
    class Customer <<ppt>>
}
Customer_invoices -.r-> Customer: "contributes to"
----

In the UI, when rendering a `Customer`, we would also be presented with the associated set of ``Invoice``s.

We can also use mixins for dependencies that are in the other direction.
For example, suppose we have a mechanism to attach ``Document``s to arbitrary domain objects.
The documents module does not depend on any other modules, but provides a `DocumentHolder` marker interface.
We can therefore attach documents to a `Customer` by having `Customer` implement this marker interface:

[plantuml]
.`customers` depends upon contributions of `documents`
----
include::docs:resources:partial$plantuml-stereotypes.puml[]

package documents {
    interface DocumentHolder <<role>>
    class DocumentHolder_documents <<mixin>>
    class DocumentHolder_attachDocument <<mixin>>
    class Document <<ppt>>
}
DocumentHolder_documents -d-> "0..*" Document: documents
DocumentHolder_documents -.l-> DocumentHolder: "contributes to"
DocumentHolder_attachDocument -.l-> DocumentHolder: "contributes to"

package customers {
    class Customer <<ppt>>
}
Customer -u.-^ DocumentHolder
----


