Where to place business logic in a Dagger/MVP app

廉价感情. 提交于 2019-12-11 07:53:01

问题


Having looked at a lot of Dagger demo apps, it isn't clear to me where business objects are placed. In a typical three tier app you have ui, business layer and data access layer. MVP is essentially a three tier architecture.

Dagger deals with components and modules and I've seen demo apps place business logic in modules. But according to the MVP architecture, business logic belongs in the Presenter layer as this layers is suppose to act as bridge between the ui and model. Many of these demo apps have models that consist of nothing more than a class with public fields to store and retrieve data from.

Can someone clarify the proper way that this should be done.


回答1:


Following clean architecture which was describe by Uncle Bob, all your code that contains business (domain) logic (rules) should be inside business layer.
For example we are developing mobile application for online ordering food / clothes. Does not matter.

Presentation Layer: (Consists of view and presenter)

- Presenter - handle view intents (button clicks, view rendered and etc), call business interactors, after received result from interactors, says to view to render current state of screen / layout.
- View - nothing more than render UI, keep view stupid, all your view logic should be in presenter.

Example case: In this layer you could check for example: user oped cart screen, your presentation layer makes request to interactor which returns cart items. If list is empty your view shows layout with title "Your card is empty", otherwise shows items list.

Business / Domain Layer: (Consists of interactor, helper classes and etc.)

Rule number one is keep your domain layer pure. If you using gradle you can use multiproject with empty dependencies. Only language, rxjava cause it is almost standard of our time.

Example case: You need to validate user order information (delivery address, initial). All your logic should be here. Length check, regex validate and etc.

Data Layer:

Knows how to save, fetch, update, delete informations. All about persistence. In android cases: data layer could make http request via retrofit2, room orm and etc. Cache starts here.

If your app doesn't contain a lot of business rules, you could avoid business layer. It depends on the project.

Also important to use SOLID principles. It will make your architecture understandable, flexible and maintainable. Read more here.



来源:https://stackoverflow.com/questions/53887553/where-to-place-business-logic-in-a-dagger-mvp-app

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!