模型视图演示者 - 设置对象集合 [英] Model view presenter - setting object collections

查看:46
本文介绍了模型视图演示者 - 设置对象集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我在上周的一场

会议上了解了模型视图演示者模式,并正在尝试它。它正在工作

很好,但我有一个问题。


我试图用它来重置组合框中的信息。下面是我的视图界面和演示者的一些

示例代码:


公共接口IDevToolView

{

string []表

{set; }

}


公共类DevToolPresenter

{

私有IDevToolView _view = null;


public DevToolPresenter()

{}


public DevToolPresenter(IDevToolView view)

{

_view = view;

}


public void GetTablesForNewDatabase()

{

string [] tables = //获取表格字符串数组的一些代码


if(tables.Length!= 0)

{

_view.Tables = tables;

}

}

}

和这里是我的观点(只是我需要的样本

这个问题:

公共类frmMain:Form,IDevToolView

{

public string []表

{

set

{

cboTable.Items.Clear;

cboTable.Items.AddRange(value);

}

}

}


我的问题是关于Item的调用s.Clear in the Tables属性。

在我看来,如果我理解正确的模式,我应该在演示者中真的这样做。

。然而,

没有将组合框本身传递到演示者中,我不能...... b $ b似乎绕过它,因为我没有任何办法直接访问

cboTable.Items。


我误解了这个吗?有没有办法解决我的问题?

Hi,

I learned a little about the model view presenter pattern at a
conference this last week and am experimenting with it. It''s working
pretty well but I have a question.

I am trying to use it to reset info in a combo box. Below is some
sample code for my view interface and the presenter:

public interface IDevToolView
{
string[] Tables
{ set; }
}

public class DevToolPresenter
{
private IDevToolView _view = null;

public DevToolPresenter()
{}

public DevToolPresenter(IDevToolView view)
{
_view = view;
}

public void GetTablesForNewDatabase()
{
string [] tables = //Some code to get the string array of tables

if (tables.Length != 0)
{
_view.Tables = tables;
}
}
}

and here''s what my view looks like (just the sample for what I need for
this question:
public class frmMain : Form, IDevToolView
{
public string[] Tables
{
set
{
cboTable.Items.Clear;
cboTable.Items.AddRange(value);
}
}
}

My question is around the call to Items.Clear in the Tables property.
This seems to me to violate the pattern, I should really be doing that
within the presenter if I understand the pattern correctly. However,
short of passing in the combo box itself into the presenter, I can''t
seem to get around it, because I don''t have any way to access
cboTable.Items directly.

Am I misunderstanding this? Is there a way around my problem?

推荐答案

嗨道格,


我想你的代码很好。


首先,清除项目并不完全是业务逻辑 - 它是视图

逻辑。换句话说,您可能希望在

未来中创建一个不同的视图,在使用不同的

表更新之前不会清除显示。例如,想想绘画程序,只是想象为什么清除显示应该真正取决于视图本身的原因。一下之后,

控制器可能会发送另一个,你可能只想保留前一个。


你说的原因是好的,我没有任何办法

直接访问cboTable.Items。那是对的,你不是!如果你这样做了,那么你将会向控制器发出视图逻辑。


-

Dave Sexton


" Doug" < dn ****** @ dtgnet.com写信息

新闻:11 ********************* @ e3g2000cwe。 googlegrou ps.com ...
Hi Doug,

I think your code is fine as is.

For one thing, clearing the items is not exactly business logic - it''s view
logic. In other words, you might want to create a different view in the
future that doesn''t clear the display before it''s updated with different
tables. Think painting program, for instance, just to visualize why clearing
the display should really be up to the view itself. After one stroke, the
controller might send another, and you may just want to keep the previous.

And the reason you stated already is a good one, "I don''t have any way to
access cboTable.Items directly". That''s right, you don''t! And if you did
you''d be tasking view-logic to the controller.

--
Dave Sexton

"Doug" <dn******@dtgnet.comwrote in message
news:11*********************@e3g2000cwe.googlegrou ps.com...




我在一个<模型视图演示者模式中学到了一点这是上周的会议,我正在试验它。它正在工作

很好,但我有一个问题。


我试图用它来重置组合框中的信息。下面是我的视图界面和演示者的一些

示例代码:


公共接口IDevToolView

{

string []表

{set; }

}


公共类DevToolPresenter

{

私有IDevToolView _view = null;


public DevToolPresenter()

{}


public DevToolPresenter(IDevToolView view)

{

_view = view;

}


public void GetTablesForNewDatabase()

{

string [] tables = //获取表格字符串数组的一些代码


if(tables.Length!= 0)

{

_view.Tables = tables;

}

}

}

这里是我的观点(只是我需要的样本

这个问题:


公共类frmMain :Form,IDevToolView

{

public string [] Tables

{

set

{

cboTable.Items.Clear;

cboTable.Items.AddRange(value);

}

}

}


我的问题是在Tables属性中对Items.Clear的调用。

这在我看来是违反了模式的,如果我理解正确的模式,我应该在演示者中做那个

。然而,

没有将组合框本身传递到演示者中,我不能...... b $ b似乎绕过它,因为我没有任何办法直接访问

cboTable.Items。


我误解了这个吗?有没有办法解决我的问题?
Hi,

I learned a little about the model view presenter pattern at a
conference this last week and am experimenting with it. It''s working
pretty well but I have a question.

I am trying to use it to reset info in a combo box. Below is some
sample code for my view interface and the presenter:

public interface IDevToolView
{
string[] Tables
{ set; }
}

public class DevToolPresenter
{
private IDevToolView _view = null;

public DevToolPresenter()
{}

public DevToolPresenter(IDevToolView view)
{
_view = view;
}

public void GetTablesForNewDatabase()
{
string [] tables = //Some code to get the string array of tables

if (tables.Length != 0)
{
_view.Tables = tables;
}
}
}

and here''s what my view looks like (just the sample for what I need for
this question:
public class frmMain : Form, IDevToolView
{
public string[] Tables
{
set
{
cboTable.Items.Clear;
cboTable.Items.AddRange(value);
}
}
}

My question is around the call to Items.Clear in the Tables property.
This seems to me to violate the pattern, I should really be doing that
within the presenter if I understand the pattern correctly. However,
short of passing in the combo box itself into the presenter, I can''t
seem to get around it, because I don''t have any way to access
cboTable.Items directly.

Am I misunderstanding this? Is there a way around my problem?



嗨Dave,


我上课用于重置组合框中的数据并不是真正的控制器,因为它是一个演示者类。我对演示者模式相对较新,但据我所知,演示者

实际上是在填充/重新设置视图吗?在那种情况下,

我不希望以某种方式在演示者课程中重置组合框吗?


我得到了演示者的印象class非常类似于
a控制器类,但它实际上只是另一层

抽象,可以更好地测试你的代码和更多

整体轻量级视图。

Hi Dave,

The class I have for resetting that data in the combo box isn''t really
a controller so much as it is a presenter class. I''m relatively new to
the presenter pattern, but from what I understand, the presenter
actually does the work of filling/reseting the view? In that case,
wouldn''t I want the combo box reset in the presenter class somehow?

I''ve gotten the impression that the presenter class is very similiar to
a controller class but that its really just another layer of
abstraction that allows for better testing of your code and a more
lightweight view overall.


嗨Doug,


"模型视图Presenter设计模式真的只是许多开发人员已经熟悉的模型视图控制器模式的全新内容;

关键的区别在于MVP真正将UI与域/服务区分开来/>
应用程序层

[模型视图MSDN上的Presenter模式:
http://msdn.microsoft.com/msdnmag/is...signPatterns/]


看来是他是对的 - MVP并不完全是MVC,但是这个小的

片段表明MVP意味着真正的将UI与

域/服务层分开。说实话,我不知道真正的是什么在这里意味着

,但是我又没有读过这篇文章了。


只是一个初步猜测,但我认为MVP实际上是不同的从一个方面来说
MVC的
- 控制器独立于视图和模型,但是我怀疑对于你在MVP设计中编码的每个视图,你会必须编写一个

对应的演示者。看起来对我来说还有更多的工作 - 我可能会因为错误而感到错误。


但是我会建议你接受我原来的建议。尽量不要使用
来定制你的程序以适应设计模式,除非它绝对需要
。如果你喜欢你已经拥有的东西而且这只是最后一个概念

,这会让你烦恼,我的解决方案非常简单 - 别管它了!


如果你不打算再写一些观点那么MVP和MVC真的不会那么有用了。在WinForms中,视图和控制器通常是相同的,并且模型是业务对象或DataSet。这种简单的

范例通常适用于一般应用程序开发。如果你将
正确地封装在每个Form之外的业务逻辑并且放入它自己的

对象模型中,那么你可以真正减少表单中的代码量,

通常让表格出现。


-

Dave Sexton


"道格" < dn ****** @ dtgnet.com写信息

新闻:11 ********************** @ e64g2000cwd .googlegr oups.com ...
Hi Doug,

"The Model View Presenter design pattern is really just a fresh take on the
Model View Controller pattern that many developers are already familiar with;
the key distinction is that MVP truly separates the UI from the domain/service
layer of the application"
[Model View Presenter Pattern on MSDN:
http://msdn.microsoft.com/msdnmag/is...signPatterns/]

It appears the you are correct - MVP is not exactly MVC, however this small
snippet states that MVP is meant to "truly" separate the UI from the
domain/service layer. To be perfectly honest I don''t know what "truly" means
here, but then again I haven''t read the article yet myself.

Just an initial guess, but I take it that MVP actually differs from one aspect
of MVC - The controller is independent of the view and model, however I
suspect that for each view you code in the MVP design, you''ll have to code a
corresponding presenter as well. Seems like more work to me - I could be
wrong though :)

But I''m going to recommend that you take my original advice anyway. Try not
to tailor your program to fit a design pattern unless it''s absolutely
required. If you like what you have already and it''s just this last concept
that''s bugging you, my solution was quite easy - leave it alone!

If you aren''t going to be writing any more views then both MVP and MVC really
won''t be that useful at all. In WinForms, the view and controller are usually
the same thing and the model is a business object or a DataSet. This simple
paradigm usually works quite well for general application development. If you
properly encapsulate the business logic outside of each Form and into its own
object model, then you can really reduce the amount of code in the Forms,
normally letting the Form present itself.

--
Dave Sexton

"Doug" <dn******@dtgnet.comwrote in message
news:11**********************@e64g2000cwd.googlegr oups.com...

嗨Dave,


用于在组合中重置数据的类盒子并不是真正的

a控制器,因为它是一个演示者类。我对演示者模式相对较新,但据我所知,演示者

实际上是在填充/重新设置视图吗?在那种情况下,

我不希望以某种方式在演示者课程中重置组合框吗?


我得到了演示者的印象class非常类似于
a控制器类,但它实际上只是另一层

抽象,可以更好地测试你的代码和更多

整体轻量级视图。
Hi Dave,

The class I have for resetting that data in the combo box isn''t really
a controller so much as it is a presenter class. I''m relatively new to
the presenter pattern, but from what I understand, the presenter
actually does the work of filling/reseting the view? In that case,
wouldn''t I want the combo box reset in the presenter class somehow?

I''ve gotten the impression that the presenter class is very similiar to
a controller class but that its really just another layer of
abstraction that allows for better testing of your code and a more
lightweight view overall.



这篇关于模型视图演示者 - 设置对象集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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