处理屏幕抓取对象的更好方法

发布于 2024-08-07 17:04:30 字数 1381 浏览 2 评论 0原文

在我的应用程序中,我总是最终实现模型-视图-演示者模式,并且通常最终会使用 get 属性从屏幕上废弃我的视图对象。

例如

Person IBasicRegistration.Person
{
 get
 {
  if (ViewState["View.Person"] == null)
   ViewState["View.Person"] = new Person();

  var Person = (Person) ViewState["View.Person"];

  Person.Email = txtEmail.Text.Trim();
  Person.FirstName = txtFirstName.Text.Trim();
  Person.LastName = txtLastName.Text.Trim();
  Person.Zip = txtZip.Text.Trim();
  Person.Phone = txtPhone.Text.Trim();
  Person.ResidentPersonLicenseState = 
        EnumerationParser.Parse<States?>(ddState.SelectedValue);

  return Person;
 }
}

,但是在调试过程中,我注意到当我在 Presenter/Model 中访问 IBasicRegistration.Person 时,我对 get { } 属性进行了相当多的遍历。

我开始认为这种模式似乎与 INotifyPropertyChanged 模式非常相似,并且我开始考虑实现类似的模式并让每个文本字段实现一个 OnChanged 事件,该事件将更新位于视图状态的 person 对象中的相关值,但是我越进一步考虑到每次用户将鼠标移出字段时都需要大量服务器请求,并且似乎在某些时候可能会导致可扩展性问题。

我的下一个想法是创建一个 IsDirty 标志并将我的代码包装在它触及类似以下字段的位置是否有意义:

Person IBasicRegistration.Person
{
 get
 {
  if (ViewState["View.Person"] == null)
   ViewState["View.Person"] = new Person();

  var Person = (Person) ViewState["View.Person"];

  if(IsDirty)
  {
      Person.Email = txtEmail.Text.Trim();
      ...others

      IsDirty = false;
  }

  return Person;
 }
}

并设置调用回发的任何方法(即通常只是提交按钮)以设置 IsDirty = true 和那么它就会无缘无故地跳过大量重复工作。

有没有人为这项任务想出更优雅的东西?

In my applications I always end up implementing a Model-View-Presenter pattern and usually end up scrapping my View object from the screen with a get property.

For example

Person IBasicRegistration.Person
{
 get
 {
  if (ViewState["View.Person"] == null)
   ViewState["View.Person"] = new Person();

  var Person = (Person) ViewState["View.Person"];

  Person.Email = txtEmail.Text.Trim();
  Person.FirstName = txtFirstName.Text.Trim();
  Person.LastName = txtLastName.Text.Trim();
  Person.Zip = txtZip.Text.Trim();
  Person.Phone = txtPhone.Text.Trim();
  Person.ResidentPersonLicenseState = 
        EnumerationParser.Parse<States?>(ddState.SelectedValue);

  return Person;
 }
}

However during debugging I've noticed when I access IBasicRegistration.Person in my Presenter/Model that I get quite a few traversals of my get { } property.

I started thinking this pattern seems to very similar to the INotifyPropertyChanged pattern and I started thinking about implementing a similar pattern and having each textfield implement an OnChanged event that would update it's related value in the person object that sits in the view state however the further I thought on that it require numerous server requests every time a person mouses out of a field and seems like it could lead to scalability issues at some point.

My next thought would it just make sense to create an IsDirty flag and wrap my code where it touches the fields similar to:

Person IBasicRegistration.Person
{
 get
 {
  if (ViewState["View.Person"] == null)
   ViewState["View.Person"] = new Person();

  var Person = (Person) ViewState["View.Person"];

  if(IsDirty)
  {
      Person.Email = txtEmail.Text.Trim();
      ...others

      IsDirty = false;
  }

  return Person;
 }
}

And set on any method that invokes a post back, (ie usually just the submit button) to set IsDirty = true and then it will skip repeating alot of work for no reason.

Has anyone come up with something more elegant for this task?

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

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

发布评论

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

评论(1

紫瑟鸿黎 2024-08-14 17:04:30

我认为发生脱节的地方是,您通常在获取视图时实现视图,同时创建视图。典型的场景是表单实现视图接口,然后在应用程序对象中注册自身(或已注册)。然后,每当您需要了解某人输入的内容时,您都可以使用视图的各种属性。

例如,要找出您为名字输入的内容,您可以

 myVariable = PersonView.FirstName();

在您的示例中,每次访问人员时都会拉取视图的状态。即使您只想要电子邮件,您也会提取所有内容并将其放入状态变量中。

现在,如果数据结构 Person 是您模型的一部分。那么这就可以了。换了你就把人拉出来一次。修改或将其添加到模型中,然后其他所有内容(报告等)都将查看模型。

然而,你的问题表明它正在一次又一次地受到打击。这让我认为出于多种原因直接从视图访问 Person。在这种情况下,我会将属性放在视图上,以允许访问各个成员。

或者我会重构设计,以便所有内容都使用数据模型,并且每次视图更改时都会更新模型。我怀疑您此时可能不想这样做,因此最好的方法是使用单独的属性。

I think where the disconnect is happening is that you typically implement a view while in your get you are create the view. A typical scenario is that a form implements a view interface and then register itself (or is registered) within the application object. Then anytime you need to find out what has been entered for a person you use the various properties of the view.

For example to find out what you entered for the first name you would go

 myVariable = PersonView.FirstName();

In your example you are pulling the STATE of the view every time you access person. Even if you just wanted the email you are pulling everything and putting it in a state variable.

Now this if the data structure Person was part of your model. Then this could be OK. You pull the person out once when it is changed. Modify or add it to the model then everything else (reports ,etc) would be looking at the model.

However your question suggests that that it is being hit over and over again. Which leads me to think that Person is being accessed directly from the view for a variety of reasons. In this case I would put properties on the view allowing access to the individual members.

Or I would refactor the design so that everything uses the model for the data with the model being updated everytime the view changes. I suspect you probably don't want to do this at this point and hence the best way is to use individual properties.

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