简体   繁体   English

如何在ASP.NET中的UI上组织我的方法[设计问题]

[英]How to organize my methods on the UI in ASP.NET [Design Problem]

First of all, I know it's a little long .. But I just wanted to make a clear case here .. Thank you all for your advice in advance :) 首先,我知道它有点长..但是我只是想在这里做一个明确的案例..谢谢大家的建议:)

I've been trying for 3 days to figure out a good design pattern to organize the methods I use at my UI layer in my web application .. and I couldn't get it right! 我已经尝试了3天,以找出一种好的设计模式来组织我在Web应用程序的UI层中使用的方法..但我做对了!

What I Have : 我有的 :

  • 19 Tables in my database 我数据库中的19个表

  • 19 Classes each class is corresponding to a table and has properties that corresponds to this given table's columns. 19个类每个类均对应一个表,并具有与该给定表的列相对应的属性。

  • 19x2 Pages .. the first 19 pages have the proper input controls that allows me to enter information to create a new record in the table by sending it via an object of the class's type. 19x2页..前19页具有适当的输入控件,使我可以通过类类型的对象将其发送来输入信息以在表中创建新记录。

  • the second 19 pages are for editing the existing records in the tables 后19页用于编辑表中的现有记录

What I Need : 我需要的 :

  • The first type of pages (the first 19 pages) need some helping methods (Ex: Reset the controls, AddToTheDb) 第一种类型的页面(前19页)需要一些帮助方法(例如:重置控件,AddToTheDb)

  • The second type (the other 19 pages) need some helping methods (Ex: Determine what's the id of the item to edit, Update the database) 第二种类型(其他19页)需要一些帮助方法(例如:确定要编辑的项目的ID,更新数据库)

  • And finally another set of methods that's going to be common between both the two types of pages. 最后是这两种类型的页面之间通用的另一套方法。

...I've been using interfaces to force the pages to define those methods, and user this method in the answer to give the flexibility in parameters for the specific purpose methods that are sued by the pair of my 19 pages. ...我一直在使用界面来强制页面定义这些方法,然后在答案中使用该方法来为我的19个页面对使用的特定用途方法提供灵活的参数。

PS: I'm not that good, so if you any "in the first place you should have .." or such kinda advices feel free to say what you see. PS:我不是很好,所以如果您“首先应该拥有..”或类似的建议,请随便说说您所看到的。 I'd be glad to learn from you. 我很高兴向您学习。

"In the first place you should have" created a single page for each class that does both the create and the update. “首先应该具有”为每个同时创建和更新的类创建了一个页面。 Whatever fields are different between the two operations can be made different when you are populating the page. 填充页面时,两个操作之间的任何字段都可以不同。 That will cut down signficantly on the amount of code you have to maintain. 这将大大减少您必须维护的代码量。 It will also make it easier when new things are added to the classes, because the changes will need to be made in only one place. 当将新事物添加到类中时,这也将变得更加容易,因为更改只需在一个地方进行。

Doing this will also solve the problem about where to put the helping methods. 这样做还将解决有关将帮助方法放在何处的问题。

UPDATE UPDATE

Creating a user interface with a lot of complex state is something that a lot of people have problems with and do badly. 创建具有很多复杂状态的用户界面是很多人遇到的问题并且做得不好。 However, there is a simple way to do it that will give good results. 但是,有一种简单的方法可以产生良好的效果。

The trick is to put all of your UI-state code in one place, and be very explicit about what logic controls the behavior. 诀窍是将所有UI状态代码放在一个位置,并明确说明哪种逻辑控制行为。 Never set a Visible or Enabled property (or even a Text property) in more than one place. 切勿在一个以上的地方设置Visible或Enabled属性(甚至Text属性)。 That way the logic is clear and can be easily changed. 这样一来,逻辑就很清楚,并且可以轻松更改。

Here is an example: 这是一个例子:

private void UpdateUI()
{
    bool isNewRecord = (contact.ContactId == 0);

    statusLabel.Text = isNewRecord ? "Create New Contact" : "Edit " + contact.Name;
    nameTextBox.Visible = isNewRecord;

    bool isBusiness = contactTypeBusinessRadioButton.IsChecked;

    spouseCheckBox.Visible = !isBusiness;
    bool hasSpouse = !isBusiness & spouseCheckBox.IsChecked;

    spouseNameTextBox.Visible = hasSpouse;
}

Then all of your UI event code looks like the following: 然后,您所有的UI事件代码如下所示:

protected void spouseCheckBox_Click(object sender, EventArgs args)
{
    UpdateUI();
}

Or with some explicit state control like this: 或像这样的一些显式状态控制:

protected void addPhoneButton_Click(object sender, EventArgs args)
{
    contact.Phones.Add(new Phone());
    UpdateUI();
}

Similarly, it is very important that all data reads/writes to the UI happen in a single place, regardless of the state: 同样,非常重要的是,无论状态如何,所有对UI的数据读/写都在一个位置进行:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
         int contactId;
         if (int.TryParse(Request.QueryString["contactid"], out contactId))
             contact = Contact.Load(contactId);
         else
             contact = new ContactId();
         DisplayContact(contact);  // Only one method to display new or existing record.
    }
}

protected void saveButton_Click(object sender, EventArgs args)
{
    ReadContactFromPage(contact); // Only one method to read the screen.
    contact.Save();
}

Clearly, for complex pieces of the UI, such as open-ended lists of things, the display and input code can be factored into many method calls, but the key thing is that all of that complexity be hidden from the higher levels that interact with the page events. 显然,对于UI的复杂部分(例如,开放式的事物列表),可以将显示和输入代码分解为许多方法调用,但是关键是要从与之交互的更高层次中隐藏所有这些复杂性。页面事件。

You could create a base page class that other will inherit from. 您可以创建其他将从其继承的基页类。 If there is common functionality there that can be reused, that's probably a good starting point. 如果存在可以重复使用的通用功能,那可能是一个很好的起点。 No if you are direct editing of tables, since you have like a table per class aproach, then maybe you could use some framework or create a generic edit feature for simplier tables. 不,如果您是直接编辑表,因为每个类都有一个类似的表,那么您可以使用某些框架或为更简单的表创建通用的编辑功能。

Now as an advice, don't overdo the less code is better approach. 现在作为建议,不要过度使用代码越少越好。 Nice clean easy to understand code is much more valuable. 简洁易懂的代码更有价值。

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

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