ASP.NET 中的业务/域对象

发布于 2024-07-17 22:27:14 字数 1234 浏览 4 评论 0原文

只是试图收集有关通过 ASP.NET (2.0+) UI/表示层操作业务/域对象的有效/无效的想法。 特别是在经典的 ASP.NET LOB 应用程序情况下,ASP.NET 代码直接与业务层对话。 我经常遇到这种类型的设计,想知道什么是理想的解决方案(即实现特定的模式),什么是最好的实用解决方案,不需要在没有实现“模式”的情况下完全重写。

这是一个示例场景。

单个 ASP.NET 页面是特定业务/域对象的“编辑/新建”页面,让我们使用“Person”作为示例。 我们想要在此页面中编辑姓名和地址信息。 当用户进行编辑或输入数据时,在某些情况下表单应该回发以刷新自身。 例如,在编辑地址时,他们选择“国家/地区”。 之后,“州/地区”下拉列表将启用并刷新为所选国家/地区的相关信息。 这本质上是业务逻辑(根据某些依赖字段限制可用选择),并且该逻辑由业务层处理(请记住,这只是一个示例,在很多业务情况下,回发期间的逻辑更加复杂 - 例如例如保险业在选择某些事物时决定了需要/需要哪些其他数据)。

理想情况下,此逻辑仅存储在 Business/Domain 对象中(即,在 ASP.NET 代码中不重复该逻辑)。 为了实现这一点,我认为业务/域对象需要重新初始化,并根据每次回发的当前 UI 值设置其状态。

例如:

private Person person = null;

protected void Page_Load()
{
    person = PersonRepository.Load(Request.QueryString["id"]);

    if (Page.IsPostBack)
        SetPersonStateFromUI(person);
    else
        SetUIStateFromPerson(person);
}

protected void CountryDropDownList_OnChange()
{
    this.StateRegionDropDownList.Enabled = true;
    this.StateRegionDropDownList.Items.Clear();
    this.StateRegionDropDownList.DataSource = person.AvailableStateRegions;
    this.StateRegionDropDownList.DataBind();
}

我看到的其他选项是将业务对象存储在 SessionState 中,而不是每次页面加载备份时从存储库(也称为数据库)加载它。

想法?

Just trying to gather thoughts on what works/doesn't work for manipulating Business/Domain objects through an ASP.NET (2.0+) UI/Presentation layer. Specifically in classic ASP.NET LOB application situations where the ASP.NET code talks directly to the business layer. I come across this type of design quite often and wondering what is the ideal solution (i.e. implementing a specific pattern) and what is the best pragmatic solution that won't require a complete rewrite where no "pattern" is implemented.

Here is a sample scenario.

A single ASP.NET page that is the "Edit/New" page for a particular Business/Domain object, let's use "Person" as an example. We want to edit Name and Address information from within this page. As the user is making edits or entering data, there are some situations where the form should postback to refresh itself. For example, when editing their Address, they select a "Country". After which a State/Region dropdown becomes enabled and refreshed with relevant information for the selected country. This is essentially business logic (restricting available selections based on some dependent field) and this logic is handled by the business layer (remember this is just one example, there are lots of business situations where the logic is more complex during the post back - for example insurance industry when selecting certain things dictates what other data is needed/required).

Ideally this logic is stored only in the Business/Domain object (i.e. not having the logic duplicated in the ASP.NET code). To accomplish this, I believe the Business/Domain object would need to be reinitialized and have it's state set based on current UI values on each postback.

For example:

private Person person = null;

protected void Page_Load()
{
    person = PersonRepository.Load(Request.QueryString["id"]);

    if (Page.IsPostBack)
        SetPersonStateFromUI(person);
    else
        SetUIStateFromPerson(person);
}

protected void CountryDropDownList_OnChange()
{
    this.StateRegionDropDownList.Enabled = true;
    this.StateRegionDropDownList.Items.Clear();
    this.StateRegionDropDownList.DataSource = person.AvailableStateRegions;
    this.StateRegionDropDownList.DataBind();
}

Other options I have seen are storing the Business object in SessionState rather than loading it from the repository (aka database) each time the page loads back up.

Thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

╰◇生如夏花灿烂 2024-07-24 22:27:15

这是我稍微不同意的一句话:

this.StateRegionDropDownList.DataSource = person.AvailableStateRegions;

Person 是一个业务/域对象,但它不是应该处理状态/区域映射(例如)的对象,即使这是做出决策的信息所在的地方。

在需要多个变量来做出决定的更复杂的示例中,您通常要做的是从您想要最终得到的域对象开始,并在该对象上调用一个函数,该函数可以给出所有必需的做出商业决策的信息。

因此,也许(在 State 类上使用静态函数):

this.StateRegionDropDownList.DataSource = State.GetAvailableStateRegions(person, ipAddress);

由于将 UI 帮助器关注点与 Person 域对象分离,这种编程风格往往“更容易测试”。

This is the line I disagree with slightly:

this.StateRegionDropDownList.DataSource = person.AvailableStateRegions;

Person is a business/domain object, but it's not the object that should be handling state/region mapping (for example), even if that's where the information to make the decision lives.

In more complicated examples where multiple variables are needed to make a decision, what you want to do in general is start from the domain object you're trying to end up with, and call a function on that object that can be given all the required information to make a business decision.

So maybe (using a static function on the State class):

this.StateRegionDropDownList.DataSource = State.GetAvailableStateRegions(person, ipAddress);

As a consequence of separating out UI helper concerns from the Person domain object, this style of programming tends to be much "more testable".

錯遇了你 2024-07-24 22:27:14

我会将你的示例放在我的“UI 增强”存储桶中,而不是 BL,验证条目是否正确是 BL,但在我看来,简化数据输入是 UI。

I'd put your example in my 'UI Enhancement' bucket rather than BL, verifying that the entries are correct is BL but easing data entry is UI in my opinion.

凌乱心跳 2024-07-24 22:27:14

对于非常简单的事情,我不会费心定期回发,而是使用 ajax 方法。 例如,如果我需要获取城市列表,我可能有一个页面方法(或网络服务),给定一个州给我一个城市列表。

如果你的选择取决于各种各样的参数,那么你所做的就会很有效。 至于在 Session 中存储东西是有好处的。 您的实体是否同时对多个人可见? 如果是这样,当用户 A 和用户 B 都编辑相同内容时会发生什么。 另外,如果您每次加载时都会保存到数据库吗? 如果我正在编辑我的姓名,然后选择国家/地区,但现在我的浏览器崩溃了,会发生什么情况? 您是否更新了数据库中的名称?

For very simple things I wouldn't bother with a regular post back but would use an ajax approach. For example if I need to get a list of Cities, I might have a Page Method (Or web service) that given a state gives me a list of cities.

If your options depends on a wide variety of parameters, what your doing would work well. As for storing things in Session there are benefits. Are your entities visible to multiple at the same time? If so what happens when User A and User B both edit the same. Also if your loading each time are you savign to the database each time? What happens if I am editing my name, and then select country, but now my browser crashes. Did you update the name in the DB?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文