有效地更新对象的子对象 [英] Updating an Object's child objects efficiently

查看:79
本文介绍了有效地更新对象的子对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个代表工作"的表格.在该工作之下是与该工作相关联的项目清单.用户可以在作业中添加或删除项目.

作业有其自己的SQL表,但也有一个Item表.创建作业时,作业列表中的任何项目都将添加到具有JobID的Item表中,以将它们链接到该作业.

一切正常,就可以了-我可以创建,查看或删除作业.我的问题是如何进行更新过程.简单(但不是很优雅)的方法如下:

在加载作业表格并编辑了作业列表后,单击更新":
-删除Item SQL表中与作业有关的所有项目.
-将作业列表中的所有项目添加到表格中.

换句话说,每次更新时,我都会有效地删除与作业相关的所有项目,并将它们重新写回到数据库中.这在没有太多项目的情况下没有问题,而且代码很简单.但是,我正在寻找一种更好"的方法,并且愿意接受建议!

谢谢:)

Hi,

I have a form representing a "job". Under that job is a list of items I''ve associated with the job. The user can add or delete items from the job.

A job has its own SQL table but there''s also an Item table. When a job is created, any items in the job list are added to the Item table with a JobID linking them to the job.

Everything works just fine as it is - I can create, view or delete a job. The problem I have is how to approach the Update process. The simple (but not very elegant) way is as follows:

On loading the job form, and having edited the job list, and clicking "Update":
- Delete all items in the Item SQL table relating to the job.
- Add all the items from the job list to the table.

So in other words, each time I update, I effectively remove all items associated with the job and rewrite them back to the database. This is no problem where there are not too many items plus the code is simple. However, I''m looking for a *better* approach and I''m open to suggestions!

Thanks :)

推荐答案

一个更优雅的解决方案是使用一些客户端脚本并跟踪您在作业上呈现的作业的哪些元素客户.

考虑一下您在Facebook中的供稿.当您将鼠标悬停在一行上时,会看到不同的修改选项(更新,删除,隐藏等).通常,当您单击这些按钮时,UI会更改以反映您执行的意图(某些东西会逐渐淡出,重新绘制,改写或以其他方式显示).这是使用服务器端代码的客户端Ajax.

考虑您的用户想要做什么.

用户可能想离开物品.所以让他们;)

他们可能希望删除一个项目.在该行旁边呈现一个"x"图像,然后单击该图像时,调用从表中删除该ID的Web方法.

他们可能想更新(或修改)单个元素.在该行旁边绘制一个铅笔,然后单击该铅笔,以显示适合您的数据行的各种类型的更新界面,并通过Ajax发送更新以保留更改.

这里有一些示例 [
干杯,
-jc
A more elegant solution would be to use some client-side script and to keep track of what elements of the job you''ve rendered on the client.

Think about your feed in Facebook. When you hover over a row, you''re presented with different modification options (update, delete, hide, like, etc). Often, when you click on these, the UI changes to reflect your executed intent (something fades out, is redrawn, reworded or otherwise). This is client-side Ajax working with server-side code.

Consider what you users would like to do.

It''s likely the users would want to leave items. So let them ;)

It''s likely they may wish to delete an item. Render an ''x'' image next to the line and, when clicked, call a web method that removes that ID from the table.

It''s likely that they would want to update (or modify) a single element. Render a pencil beside the row and, when clicked, reveal an update interface of whatever sort is appropriate for your row of data, and send the update via Ajax to persist the changes.

There are some samples here[^] using ASP.NET and jQuery/jQuery UI together to make things like this happen.

Another thing to consider...if you do leave it as is, why not add a CreateDate field and a ''deleted'' field. This would allow you to keep a history of Job iterations/modifications and you wouldn''t need to delete the records, just mark them as ''deleted=true'' and not render them on the page.

Cheers,
-jc


这篇关于有效地更新对象的子对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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