通过BLL问题更新Linq to SQL-最佳实践 [英] Linq to SQL Updating through BLL issues - Best Practices

查看:93
本文介绍了通过BLL问题更新Linq to SQL-最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

设置:

我有一个很大的表格,其中包含许多用于更新Product对象的字段.因此,在ASPX页面中,用户更改了需要更新的字段,然后单击了Submit.在后面的代码中,我要做类似的事情;

I have a large form with many fields that are collected to update a Product object. So in the ASPX page the user changes the fields that need updating and they hit submit. In the code behind I do something like this;

Dim p as New MyCompany.Product()
p = p.GetProductById(ProductID)

我将Linq的Product子类扩展到SQL,以将该方法(GetProductById)添加到对象中

I extend the Product partial class of Linq to SQL to add this method (GetProductById) to the object

p.Name = txtName.Text
p.SKU = txtSKU.Text
p.Price = txtPrice.Text
...    
p.Update()

这是扩展的Product子类中的Update方法.我更新数据库,发送电子邮件和更新历史记录表,因此我希望此方法可以完成所有这些事情.

This is an Update method in the extended Product partial class. I update the database, send emails and update history tables so i want this method to do all those things.

该项目还有50个字段,因此显然有一种方法可以收集所有50个字段(这很可笑(而且我还是不想走那条路线,因为很难调试IMO)

There are 50 more fields for the project so obviously it would be ridiculous to have a method that collects all 50 fields (and I don't want to go that route anyway bc it's harder to debug IMO)

问题:

如果我使用DataContext通过Linq to SQL获得产品,那么我再也无法更新它,因为它会出现无法附加和已附加到另一个DataContext的实体的错误.

If I get the Product via Linq to SQL using a DataContext then I can never update it again because it errors about not being able to attach and entity that's already attached to another DataContext.

问题:

那么,如果我通过BLL中的方法获取对象,请在ASPX页面中对其进行更新,然后尝试再次通过BLL发送更新以更新数据库,那么该怎么做呢?

SO if I get an object through a method in my BLL, update it in the ASPX page and then try to send the updates through the BLL again to update the database, how should I go about doing this?

推荐答案

不管是否使用LINQ-to-SQL,这都是我的工作.提交后,我将搜索项目(如果使用PK是单个项目,则应该很快),我的DAL返回数据对象,然后使用反射将页面中的每个元素与数据对象中的相应属性进行映射.我的DAL仅更新已更改的项目.

Regardless of LINQ-to-SQL or not, here's what I do. Upon submission, I search for the item (it should be quick if it is a single item using the PK), my DAL returns a data object, and I use reflection to map each element in the page with corresponding properties in the data object. My DAL only updates items that changed.

我认为您要做的是相同的,收集所有值并提交它们.如果LinqToSql不够聪明,无法确定更改了什么,那么它可能不是最佳选择.

I think what you have to do is the same, gathering all values and submitting them. If LinqToSql is not smart enough to determine what changed then it may not be the best alternative.

这篇关于通过BLL问题更新Linq to SQL-最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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