我一直在尝试遵循清晰的架构来开发应用程序。
鲍勃叔叔的干净架构所建议的大部分内容在理论上都是有意义的。
如图所示,
系统的所有用例都定义了输出端口,它返回操作的结果。演示者展示结果,
但问题是,
-
输出端口是否有任何实际分组,似乎输出端口是链接的抽象/接口的集合到一个用例。
因此,假设我们有 5 个用例,它们会产生一些输出数据,并且在触发用例和报告结果之间存在一段时间的延迟,所以这意味着我们也必须处理它。这将使输出端口现在有 5*2 接口,由 Presenter 实现。
-
来到Web应用程序中的presenter,Web应用程序有很多页面,这些页面可以称为视图。那么,我们是否创建一个实现所有用例输出接口的单个演示者?演示者决定渲染哪个视图、什么数据。 ?这种方法的问题在于,它使演示者成为一个非常大的类,它实现了所有用例输出。
或者我们为每个 UI 页面创建一个演示者,它仅实现相关的用例输出端口。?
-
同样的问题也适用于控制器,是否每个视图都有一个大控制器和多个小控制器。
I have been trying to follow clear architecture, to develop applications.
most of the stuff suggested by uncle bob's clean architecture theoretically makes sense.
As shown in the picture,
All the use cases of the system define the output port, where it returns the result of the operation. and the presenter presents the result,
but the questions are,
-
Is there are any practical grouping of the output port, it seems that the output port is a collection of abstract / interfaces which is linked to the one use case.
so, say if we have 5 use cases, which produce some output data, and there is a delay for some time between the use case is triggered and the result is reported, so it means we have to also take care of it. this will make the output port have 5*2 interfaces now, to be implemented by the presenter.
-
coming to the presenter in the web application, the web application has a lot of pages, which can be called views. so, do we create a single presenter that implements all the use-case output interfaces? and the presenter decides which view to render, what data. ? the problem with this approach is that it makes the presenter one very big class which implements all the use case output.
Or do we create one presenter for each UI page, which implements only relevant use case output port.?
-
the same question goes to the controller, whether to have one big controller of many small controllers per view.
发布评论
评论(1)
编辑
因此,当您想要报告用例的进度时,您有多种选择。
向输出端口添加进度方法
扩展进度接口
进度响应数据
扩展进度响应数据
我无法在这里讨论所有不同的优缺点,但我想我会使用 2. 或 4. 选项。
演示者的概念并不意味着只有一个。此外,如果只有一个,它将承担很多责任,因此会因不同的原因而发生变化。这将违反单一责任原则。这并不一定意味着您必须为每个输出端口创建一个新的演示器。但通常不同的输出端口会因不同的原因而改变。
我还认为,由于测试原因,将不同的演示者分开会更好。例如,如果您只有一个实现所有输出端口的演示者,那么它还依赖于它可以更新的所有视图模型。这意味着您经常必须模拟某个测试的依赖关系,而该特定测试不需要这些依赖关系。
与我对 2 给出的答案相同。演示者可以应用于控制器。
控制器根据用户交互执行用例。这可以是按钮的点击、触摸设备上的手指手势、移动设备的陀螺仪传感器的状态改变或者任何用户交互。因此,控制器由 ui 发出的事件触发。控制器解释用户交互,并通过将用户输入从视图模型传递到用例来将其转换为用例调用。您经常使用演示器将视图模型的更新与用例的结果封装起来。
编辑
是的,甚至更细粒度。也许一个用例仅适用于页面的一部分,因此您也有该部分的控制器和演示者。
在某些情况下,您可以将演示者和/或控制器合并到一个对象中,但我想这种情况很少见。最后当应用程序发展时你会看到它。有时您合并对象,后来您发现这不是一个好主意,然后您再次拆分它们。也许与合并之前的结构相同,也许您会发现另一个更合适的结构。
EDIT
So when you want to report the progress of the use case you have multiple options.
Add a progress method to the output port
Extend a progress interface
Progress response data
Extend progress response data
I can't go into all the different pros and cons here, but I guess I would use either the 2. or 4. option.
The concept of a presenter doesn't mean that there is only one. Furthermore if there would only be one it would have a lot of responsibilities and thus change for different reasons. This would be a violation of the single responsibility principle. This doesn't necessarily mean that you have to create a new presenter for each output port. But usually the difference output ports change for different reasons.
I also guess it will be better to separate the different presenters, because of testing reasons. E.g. if you only have one presenter that implements all output ports, it also has dependencies to all view models that it can update. This means that you often have to mock dependencies for one test that are not necesarry for that specific test.
The same answer as I gave about 2. the presenter can be applied to controllers.
Controllers execute use cases based on user interaction. This can be a click to a button, a fingure gesture on a touch device, a state change of the gyro sensor of a mobile device or whatever a user interaction can be. Thus controllers are triggered by events that the ui emits. The controllers interprete user interactions and translate them to a use case call by passing user input from view models to the use case. You often encasulate the updating of the view models with the result of the use case by using a presenter.
EDIT
Yes and even more fine grained. Maybe a use case only applies to a part of a page and thus you have a controller for that part and a presenter as well.
There might be situations when you can merge presenters and/or controllers into one object, but I guess this cases are rare. Finally you will see it when the application evolves. Sometimes you merge objects and later you find out that it wasn't a good idea and you split them again. Maybe into the same structure as they were before you merged, maybe you find another structure more suitable.