简体   繁体   English

模型视图演示者被动视图实现

[英]Model View Presenter Passive View implementation

I'm currently using Model View Presenter Passive View on my .NET Compact Framework project using C#. 我目前正在使用C#在.NET Compact Framework项目上使用Model View Presenter被动视图。 Now, in my model, I have lots of Pinvoke from a C/C++ DLL. 现在,在我的模型中,我从C / C ++ DLL中获得了许多Pinvoke。 My project is a hardware testing equipment, typically with buttons and large LCD touchscreen. 我的项目是硬件测试设备,通常带有按钮和大型LCD触摸屏。 It then collects data (uses some database) and transfer to PC. 然后收集数据(使用一些数据库)并传输到PC。

I created a model interface, and the class that implements it invokes those Pinvoke methods. 我创建了一个模型接口,实现该模型的类调用了这些Pinvoke方法。 One reason is I would like to encapsulate the Pinvoke and marshalling,interop inside the Model. 原因之一是我想将Pinvoke和编组,互操作封装在模型中。

Now I have a presenter. 现在我有一个主持人。 An example scenario: a user presses a button then the click event on the view will call the method on presenter (via an interface), then finally call the model's method (via an interface again). 一个示例场景:用户按下一个按钮,然后视图上的click事件将(通过接口)调用presenter上的方法,然后最终(再次通过接口)调用模型的方法。

Now, it seems to me that the presenter is mostly becoming a wrapper of the model's business logic. 现在,在我看来,演示者几乎正在成为模型业务逻辑的包装。 If I add some methods to the model, I also need to add that method via an interface because the view's button's need to invoke some of the methods in the model. 如果我向模型添加一些方法,则还需要通过接口添加该方法,因为视图的按钮需要调用模型中的某些方法。 I feel that there is too many indirection. 我觉得有太多的间接。 One example is that, in the model, I have a thread to wait for the events being pushed by a C/C++ DLL. 一个示例是,在模型中,我有一个线程来等待C / C ++ DLL推送的事件。 Now, I have a thread on the Presenter which uses an observer pattern to queue and process that events coming from the model (changing of screen views and telling the user what is happening). 现在,我在Presenter上有一个线程,该线程使用观察者模式对来自模型的事件进行排队和处理(更改屏幕视图并告诉用户正在发生的事情)。

pseudo code from the interface of view: void viewChangeTestResultsText(string Text); 视图界面中的伪代码:void viewChangeTestResultsText(string Text);

from the interface of presenter:
void PerformTest();

on the concrete class the implements the interface of presenter:
void PerformTest()
{
interfaceView.viewChangeTestResultsText("Test Started");
interceModel.PerformTest();
}

on the interface of Model:
void PerformTest();

on the concrete class of the Model:
PerformTest()
{
ModelPinvokeMethods.PerformTest();
}

in this code, the button click handler calls the performtest in the presenter, then presenter calls the performtest in the model. 在此代码中,按钮单击处理程序在presenter中调用performtest,然后presenter在模型中调用performtest。 then model calls the pinvoke performtest. 然后模型调用pinvoke performtest。 the indirection quite causing some pain already because I have lots of method calls to implement and the project is in a very tight deadline. 间接操作已经造成了一定的痛苦,因为我要实现很多方法调用,并且该项目的期限很紧。

For my project, there is another variant and I know that I will be needing a changeable presenter and with this, I also need a changeable model because the business logic is somehow different although there are lots of similarities. 对于我的项目,还有另一个变体,我知道我将需要一个可更改的演示者,与此同时,我还需要一个可更改的模型,因为尽管有很多相似之处,但是业务逻辑还是有所不同。 Right now, I am thinking of pushing all the logic in the model to the presenter so that I will just be maintaining only the logic on the presenter view and use the model only for data handling (database, configurations, settings), which I think will be simpler in terms of development and code maintenance but I am not sure of the impact in terms of flexibility. 现在,我正在考虑将模型中的所有逻辑推给演示者,这样我将只维护演示者视图上的逻辑并将模型仅用于数据处理(数据库,配置,设置),我认为在开发和代码维护方面将更加简单,但是我不确定灵活性方面的影响。

This is my first time to use MVP in passive view. 这是我第一次在被动视图中使用MVP。 I am not sure if I am missing something regarding the correct implementation of MVP. 我不确定我是否缺少有关正确实施MVP的内容。 Any thoughts or suggestions on this? 有什么想法或建议吗?

Your understanding of MVP seems fine; 您对MVP的理解似乎不错; you have correctly distinguished between presentation logic (perform test, synchronize view) and domain logic (PInvoke). 您已经正确区分了表示逻辑(执行测试,同步视图)和域逻辑(PInvoke)。 With the interfaces you have set up, you can easily unit test the presenter (which is one of the main advantages of using MVP). 使用已设置的接口,您可以轻松地对演示者进行单元测试(这是使用MVP的主要优势之一)。

I would advise against putting all of the logic in the presenter as that can lead to a God Object . 我建议不要将所有逻辑放到演示者中,因为这可能导致出现“ 上帝对象”

Regarding your changeable presenter problem, I'm not sure what you mean. 关于您的演示者问题,我不确定您的意思。 Do you mean that you need a different presenter/model for each type of device? 您是说每种设备都需要不同的演示者/模型吗? If so it seems perfectly reasonable to have a MVP-triad for each type of device if they are sufficiently distinct from one another. 如果是这样,那么如果每种类型的设备彼此之间有足够的区别,那么为它们配备一个MVP三元组似乎是完全合理的。 If you identify common traits between them, you can either use inheritance or utility classes to provide common code. 如果确定它们之间的共同特征,则可以使用继承或实用程序类来提供共同的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM