适当的编程? (事件) [英] proper programming? (events)

查看:64
本文介绍了适当的编程? (事件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




好​​吧,我有编程背景,但我是C#的新手。我也是自我

这样教:


i有一个datagridview,根据用户签署的
用户的不同行为在


现在是否更有效/正确如果我这样做:


if(user ==" user1"){

this.dataGridView2.CellBeginEdit + = new

System.Windows.Forms.DataGridViewCellCancelEventHa ndler(this.dataGridView2_CellBeginEdit_User1);

} else {

this.dataGridView2.CellBeginEdit + = new

System.Windows.Forms.DataGridViewCellCancelEventHa ndler(this.dataGridView2_CellBeginEdit_User2);

}


而不是检查一个cellBegin事件中的哪个用户?


谢谢

Gideon

hi ,

ok , i have a programming background but i''m new to C# . i''m also self
taught so :

i have a datagridview that should act differently depending on which
user has signed in

now is it more effecient/correct if i do this:

if(user=="user1") {
this.dataGridView2.CellBeginEdit += new
System.Windows.Forms.DataGridViewCellCancelEventHa ndler(this.dataGridView2_CellBeginEdit_User1);
}else {
this.dataGridView2.CellBeginEdit += new
System.Windows.Forms.DataGridViewCellCancelEventHa ndler(this.dataGridView2_CellBeginEdit_User2);
}

as opposed to check for which user in ONE cellBegin event??

Thanks
Gideon

推荐答案



giddy写道:

giddy wrote:

hi,


好​​吧,我有编程背景,但我是C#的新手。我也是自我

这样教:


i有一个datagridview,根据用户签署的
用户的不同行为在


现在是否更有效/正确如果我这样做:


if(user ==" user1"){

this.dataGridView2.CellBeginEdit + = new

System.Windows.Forms.DataGridViewCellCancelEventHa ndler(this.dataGridView2_CellBeginEdit_User1);

} else {

this.dataGridView2.CellBeginEdit + = new

System.Windows.Forms.DataGridViewCellCancelEventHa ndler(this.dataGridView2_CellBeginEdit_User2);

}


而不是检查一个cellBegin事件中的哪个用户?


谢谢

Gideon
hi ,

ok , i have a programming background but i''m new to C# . i''m also self
taught so :

i have a datagridview that should act differently depending on which
user has signed in

now is it more effecient/correct if i do this:

if(user=="user1") {
this.dataGridView2.CellBeginEdit += new
System.Windows.Forms.DataGridViewCellCancelEventHa ndler(this.dataGridView2_CellBeginEdit_User1);
}else {
this.dataGridView2.CellBeginEdit += new
System.Windows.Forms.DataGridViewCellCancelEventHa ndler(this.dataGridView2_CellBeginEdit_User2);
}

as opposed to check for which user in ONE cellBegin event??

Thanks
Gideon



我不确定我喜欢哪种方法。检查实际用户

似乎很难维护。什么样的东西

你想要做些什么?几乎可以肯定有更好的方式来实现你想要的东西......


-

Tom Shelton


您好基甸,


我更喜欢将该逻辑放在事件处理程序中,或者逻辑是否复杂

和动态然后是业务对象。也就是说,如果可以为每个用户配置事件处理程序中的逻辑
,并在以后基于数据库

属性进行更改,那么这种复杂性可能会保证使用更复杂的
复杂系统以防止事件处理程序方法中的代码膨胀,并且封装逻辑以便可以在其他UI组件中重用 br />
系统。


这里有一个简单的BO模型,如果你有动态需要采用它,需要b / b
需要,比如我上面提到的那个:


this.dataGridView2.CellBeginEdit + = dataGridView2_CellBeginEdit;


void dataGridView2_CellBeginEdit(object sender,SomeEventArgs e )

{

//你应该确保你的应用程序在这里查询之前分配User.Current

//属性,或者你可以使该属性为
//单身字段并在首次请求时自动初始化

使用

//类型构造函数(静态)。


User.Current.BeginCellEdit(sender,e);

}

class用户

{

private static用户当前;

public static用户当前{get {return current; } set {current =

value;公共UserUICapabilities上限{get {return caps;} $

public UserUICapabilities上限} $

private readonly UserUICapabilities上限;

private readonly int id;


public User(int id)

{

this.id = id;

caps = new UserUICapabilities(this);

}


public void BeginCellEdit(object sender,SomeEventArgs e)

{

caps.BeginCellEdit(sender,e);

}

}


class UserUICapabilities

{

private readonly用户用户;

私人布尔初始化; //延迟初始化


public UserUICapabilities(用户用户)

{

this.user = user;

}


void EnsureInitialized()

{

if(initialized)

return ;


// TODO:从数据库加载用户上限


initialized = true;

}


public void BeginCellEdit(object sender,SomeEventArgs e)

{

EnsureInitialized();


//具有不同id的每个User对象将有一个

// UserCapabilities对象的实例,可以在这里提供一个

//不同的实现。


// TODO:基于用户开始进行单元格编辑。本课程中的字段

}

}

如果BeginCellEdit方法需要有关UI元素的更多信息

它可以操作,然​​后你可以创建一个数据上下文类。您的数据上下文类的实例可以传递给BeginCellEdit而不是发送者

和事件参数:


类BeginCellEditContext

{

私有DataGridView网格;

私有DataGridView网格{get {return grid; } $

私有标签;

私有标签标签{get {return label; } $

public BeginCellEditContext(DataGridView网格,标签标签)

{

this.grid = grid;

this.label = label;

}

}


该类可能是BeginCellEdit的唯一参数方法:


public void BeginCellEdit(BeginCellEditContext context){...}


方法可以这样调用:


BeginCellEditContext context = new BeginCellEditContext(theGrid,theLabel);


User.Current.BeginCellEdit(context);

另一种选择是使用MVP架构,但这将增加甚至更多的复杂性。你将不得不为BeginCellEdit方法可能执行的每个可能的

任务创建命令对象。


满足您所有需求的最简单方法将是最好的;在一些

案例中,这只是一个简单的事件处理程序例程,所以明智地选择:)


-

Dave Sexton


" giddy" < gi ******* @ gmail.com写信息

新闻:11 ********************** @ j44g2000cwa.googlegr oups.com ...
Hi Gideon,

I prefer to place that logic in the event handler, or if the logic is complex
and dynamic then a business object. i.e., if the logic in the event handler
may be configured for each user and changed at a later time based on database
properties, for example, then that complexity may warrant the use of a more
complex system as well to prevent code bloat in the event handler method and
to encapsulate the logic so it can be reused in other UI components and
systems.

Here''s a simple BO model that you might want to adopt if you have "dynamic"
needs, such as the one I mentioned above:

this.dataGridView2.CellBeginEdit += dataGridView2_CellBeginEdit;

void dataGridView2_CellBeginEdit(object sender, SomeEventArgs e)
{
// you should ensure that your application assigns the User.Current
// property before it is queried here, or you can make the property a
// singleton field and initialize it automatically upon first request
using a
// type constructor (static).

User.Current.BeginCellEdit(sender, e);
}
class User
{
private static User current;
public static User Current { get { return current; } set { current=
value; } }

public UserUICapabilities Caps { get { return caps; } }

private readonly UserUICapabilities caps;
private readonly int id;

public User(int id)
{
this.id = id;
caps = new UserUICapabilities(this);
}

public void BeginCellEdit(object sender, SomeEventArgs e)
{
caps.BeginCellEdit(sender, e);
}
}

class UserUICapabilities
{
private readonly User user;
private bool initialized; // lazy initialization

public UserUICapabilities(User user)
{
this.user = user;
}

void EnsureInitialized()
{
if (initialized)
return;

// TODO: load user caps from database

initialized = true;
}

public void BeginCellEdit(object sender, SomeEventArgs e)
{
EnsureInitialized();

// Each User object with a distinct "id" will have an
// instance of a UserCapabilities object that can provide a
// different implementation here.

// TODO: begin cell edit based on the "user" field in this class
}
}
If the BeginCellEdit method needs more information about the UI elements that
it can manipulate, then you can create a data context class. An instance of
your data context class may be passed to BeginCellEdit instead of the sender
and event args:

class BeginCellEditContext
{
private DataGridView grid;
private DataGridView Grid { get { return grid; } }

private Label label;
private Label Label { get { return label; } }

public BeginCellEditContext(DataGridView grid, Label label)
{
this.grid = grid;
this.label = label;
}
}

That class may be the only argument to the BeginCellEdit method:

public void BeginCellEdit(BeginCellEditContext context) { ... }

The method may be called as such:

BeginCellEditContext context = new BeginCellEditContext(theGrid, theLabel);

User.Current.BeginCellEdit(context);
Another option is to use an MVP architecture, but that''s going to add even
more complexity. You''ll have to create command objects for every possible
task that the BeginCellEdit method may perform.

The simplest approach that meets all your needs will be the best one; in some
cases that is just a simple event handler routine, so choose wisely :)

--
Dave Sexton

"giddy" <gi*******@gmail.comwrote in message
news:11**********************@j44g2000cwa.googlegr oups.com...




好​​吧,我有编程背景但我''我是C#的新手。我也是自我

这样教:


i有一个datagridview,根据用户签署的
用户的不同行为在


现在是否更有效/正确如果我这样做:


if(user ==" user1"){

this.dataGridView2.CellBeginEdit + = new

System.Windows.Forms.DataGridViewCellCancelEventHa ndler(this.dataGridView2_CellBeginEdit_User1);

} else {

this.dataGridView2.CellBeginEdit + = new

System.Windows.Forms.DataGridViewCellCancelEventHa ndler(this.dataGridView2_CellBeginEdit_User2);

}


而不是检查一个cellBegin事件中的哪个用户?


谢谢

Gideon
hi ,

ok , i have a programming background but i''m new to C# . i''m also self
taught so :

i have a datagridview that should act differently depending on which
user has signed in

now is it more effecient/correct if i do this:

if(user=="user1") {
this.dataGridView2.CellBeginEdit += new
System.Windows.Forms.DataGridViewCellCancelEventHa ndler(this.dataGridView2_CellBeginEdit_User1);
}else {
this.dataGridView2.CellBeginEdit += new
System.Windows.Forms.DataGridViewCellCancelEventHa ndler(this.dataGridView2_CellBeginEdit_User2);
}

as opposed to check for which user in ONE cellBegin event??

Thanks
Gideon



oops。对不起,用户我的意思是我的应用程序的用户,我的应用程序

mantains用户的密码,我不是在谈论Windows用户

accounts <基本上我不想让它变得混乱但是,有级别......

每个用户都有一些特权并以某种方式受到限制,例如。

一些用户可能不被允许编辑,但只允许制作新的

条目,一些用户可以做相反的事情......等等。

celBeginedit是'我唯一要检查的事件......


我只是问它是否正确给它一个不同的代表

不同的功能取决于使用的privelages ...


因为在一个事件中检查它可能会变得麻烦并减少

效率??


也是Program.cs文件,带有class program,main()和

有完整的全局变量吗?可以通过所有

表单和对话框访问??? (我是ex vb!,所以我问的是program.cs喜欢

a模块文件?)


Gideon


Tom Shelton写道:
oops. .i''m sorry , by user i meant user for my application , my app
mantains users wth passwords , i''m not talking about windows user
accounts

basically i did''nt want to make it confusing but , there are levels ...
each user is given some privilege and retricted in some way , eg .
some users might not be allowed to edit , but only allowed make new
entries , some users would be allowed do the opposite.. etc... .the
celBeginedit is''nt the only event i have to check for this...

i''m just asking if its CORRECT to give it a different delegate to
different funciton depending on what privelages the use has...

becuase checking for it in ONE event might get cumbersome and reduce
efficency??

also is Program.cs file , with "class program , and the main()" the
place to have complete global variables? that can be accessed by all
forms and dialogs??? (i''m an ex vb! , so i''m asking is program.cs like
a module file?)

Gideon

Tom Shelton wrote:

我不确定我喜欢哪种方法。检查实际用户

似乎很难维护。什么样的东西

你想要做些什么?几乎可以肯定有更好的方法来实现你想要的...
I''m not sure I like either method. Checking for the actual user would
seem to make things pretty hard to maintain. What kinds of things are
you looking to do different? There is almost certainly a better way to
accomplish what you want...


这篇关于适当的编程? (事件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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