在Dagger/MVP应用程序中放置业务逻辑的位置 [英] Where to place business logic in a Dagger/MVP app

查看:107
本文介绍了在Dagger/MVP应用程序中放置业务逻辑的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看过许多Dagger演示应用程序后,我不清楚在哪里放置业务对象.在典型的三层应用程序中,您具有ui,业务层和数据访问层. MVP本质上是一个三层体系结构.

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处理组件和模块,我已经看到演示应用程序将业务逻辑放置在模块中.但是根据MVP架构,业务逻辑属于Presenter层,因为该层假定充当ui和模型之间的桥梁.这些演示应用程序中的许多应用程序都具有仅由带有公共字段的类组成的模型,这些类用于存储和检索数据.

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.

推荐答案

在遵循Bob叔叔描述的简洁架构之后,所有包含业务(域)逻辑(规则)的代码都应位于业务层内部.
例如,我们正在开发用于在线订购食物/衣服的移动应用程序.不要紧.

演示层:(由视图演示者组成)

-演示者-处理视图意图(单击按钮,渲染视图等),在收到来自交互器的结果后,致电业务交互器,表示要渲染屏幕/布局的当前状态.
-视图-只是呈现UI,保持视图愚蠢,所有视图逻辑都应在Presenter中.

示例案例:在这一层中,您可以检查例如:用户选择购物车屏幕,您的表示层向交互器发出请求,该交互器​​返回购物车项目.如果列表为空,则视图显示标题为您的卡为空"的布局,否则显示项目列表.

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.

业务/域层 :(由 interactor ,助手类等组成)

第一条规则是保持域层纯净.如果使用gradle,则可以使用具有空依赖项的多项目.仅语言,rxjava导致它几乎是我们时代的标准.

示例案例:您需要验证用户订单信息(送货地址,首字母缩写).您所有的逻辑都应该在这里.长度检查,正则表达式验证等.

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.

数据层:

知道如何保存,获取,更新,删除信息.关于持久性.在android系统中:数据层可以通过retrofit2,room orm等发出HTTP请求.缓存从此处开始.

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.

使用SOLID原理也很重要.这将使您的体系结构易于理解,灵活且可维护.在此处中了解更多.

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

这篇关于在Dagger/MVP应用程序中放置业务逻辑的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆