Clean Architecture + MVP in iOS
Another article about clean architecture and using presenter as a business-logic handler in presentation layer.
Simple application that using open dota API as a data source. - tifoaudii/ios-heropedia-mvp-clean-arch
I created simple application using Open Dota API as a data source. Feel free to visit my repository on github and just clone it. The project consist of several additional layer such as DI Container for managing dependencies, Router for handling navigation, and of course the network layer that support local cache using UserDefault. I used clean architecture approach for create separation layer between presentation, domain, and data. I also created presenter as a business-logic handler and using protocol-oriented to make it loose coupling between each other.
Inside the view controller, it has dependency which is the presenter that has responsibilities to handle the business-logic. Whenever there are some UI event or interaction between user and the view, it would ask presenter to do something and whenever the presenter has done with the job, it will ask view controller through
HeroListViewPresentationProtocol implementation. The advantages of using this approach is we can implement unit testing easily later because it have a good abstraction between view and the presenter.
In this implementation, the presenter has two dependencies which are the network service and the callback (would be used for handle navigation — check the router layer in the repository). Inside
HeroListPresenter, it has four variable with getter access and six main function that would be used by the view controller. Beside handle the business-logic, presenter also has a responsibility to fetch data from the network layer and prepare the data so the view controller can consume it properly.
Inside network service implementation, it has two dependencies which are the data store that has a job to fetch data from remote API and cache repository that has a job to save the data from remote API and then load the data whenever it needed. Those of two dependencies are protocol-type and of course it would be loose coupling and easy to maintenance.
Implement unit testing become painless because the system was designed with protocol-oriented. We can create spy properties to spying those presenter job from the view controller. Then in each test cases, we can validate the behaviour each of it. Here are the test cases implementation in view controller test.
I think there are no perfect architecture at all. It depend on what the requirement and what problems that we are trying to solve. In this article, I just want to show you how my approach to implement clean architecture with the presenter as a business-logic handler. Of course this implementation is far from perfect word. And I encourage you to give me some feedback :]. Thank you for reading this and see you in another article.