Architecture

A use case that you can apply composition root to make your app more flexible.

Hi everyone, I hope you’re doing really well. Today we’re going to talk about how to apply composition root. What is it ? How does it affect to our apps. Let’s figure all it out together in this post.


The Problem & Solution

Take a look into your code base. How do you push from a view controller to another view controller ? For example, view controller A to view controller B by tapping a button on view controller A. Is this how you are doing it as I’ve seen in many codebases?

@IBAction func buttonTapped(_ sender: UIButton) {
      let viewControllerB = storyboard!.instantiateViewController(identifier: "ViewControllerB")
      navigationController?.pushViewController(viewControllerB, animated: true)
}

What’s problem with that approach ? The problems are by doing so you’re making view controller A knows too much about view controller B. View controller A knows how view controller B’s dependencies (if any) knows how to instantiate view controller B and knows how view controller B is presented. The consequences are view controller A coupled to view controller B and then in some point of your development if you want to make some change about the app’s follow, view controller A to view controller C instead of B, for example you have to go into view controller A’s class and change there which is violate open-closed principle.

I think that’s enough reason for us to find a better way, a more flexible way to solve the problems above. And it’s not hard to find we’re not the only one who face the problems, legendaries’s already found a great way to deal with and it called composition root. So let’s talk about composition root.

Definition

In this article I just want to show you a brief introduction of what is composition root and how it works. I highly recommend you to read a book named Dependency Injection Principles, Practices, and Patterns by Mark Seemann , Steven van Deursen to get a fascinating into it.

A Composition Root is a single, logical location in an application where modules are composed together.

Dependency Injection – Principles, Practices, and Patterns

We can understand that a composition root is something or somewhere we initiate everything and then compose them together as needed. Does it make sense to you ? Does it ring any bells ? So now we know that instead of instantiate view controller B inside view controller A we should move all the instantiations to a new place called composition root. But what’s composition root in an iOS app or do we need to create it ? By reading the book I’ve learnt that:

The Composition Root isn’t a method or a class, it’s a concept. It can be a part of the Main method, or it can span multiple classes, as long as they all reside a single module.

and

We should compose object graphs as close as possible to the application’s entry point.

From this sentence and from the problem we’ve had above I can consider AppDelegate/SceneDelegate as a composition root. So let’s see how we can implement the concept to solve our problem and then we will see how it make the app better.

The solution

Instead of creating view controller B inside view controller A we’re going to delegate the button tapping message to the composition root and then instantiate view controller B from there. In the code bellow I’m using closure but you can also use protocol as well.

Here’s an example of the composition root’s implementation – the SceneDelegate :

So now, our SceneDelegate will take the responsibility of instantiating and composing and navigating view controllers. So you can have more control on how things are compose and in composite view controllers aren’t know about each others, they’re totally free !! They’re just push message and let composition root do what it wants afterward. If I want to push to another view controller instead of view controller B it’s very simple to do, right ?

Conclusion

Well, I just showed you a use case that you can apply composition root pattern to make you app more flexible, of course the concept has much more than that. Again, I highly recommend you to read the book I mentioned above.

OK, it’s time to say goodbye. I hope you’ve got something in your bag 😉 If you have any questions feel free to comment down bellow.

References

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *