简体   繁体   English

我应该从客户端还是通过存储的proc删除子对象

[英]Should I delete child objects from the client, or via a stored proc

I am working on both parts of a data layer. 我正在数据层的两个部分上工作。 I get to create the procs, and the business object layer that will consume the procs. 我创建了proc,并创建了将使用proc的业务对象层。

Is there a best practice for determining where a delete should occur? 是否有最佳实践来确定应该在哪里删除?

Here's the scenario. 这是场景。 I have a table Book with foreign key to a child table Pages. 我有一个带有外键的子表的子表。 If I delete the book, I also want to delete the pages. 如果我删除这本书,我也想删除这些页面。 If the deletion of a page fails, I want the whole book to stay (transaction). 如果删除页面失败,我希望整本书保留(事务)。

Should the delete occur in managed code (by creating a transaction, deleting all child objects and finally the book), or in the proc (again in a transaction)? 删除应该发生在托管代码中(通过创建事务,删除所有子对象并最终删除书籍)还是在proc中(再次在事务中)?

Or does it matter? 还是有关系吗?

well there are 3 ways for this: 那么有3种方法可以实现:

  1. do it in the app with TransactionScope 使用TransactionScope在应用程式中完成
  2. do it in the proc 在过程中做
  3. create a cascade delete on the PK-FK relationship. 在PK-FK关系上创建级联删除。

depending on what your DAL looks like you'll have to make a choice. 根据您的DAL外观,您必须做出选择。 if it's a ORM then go with 1. else go with 3. 如果是ORM,则选择1.,否则选择3.。

I would say it depends where you store your "logic" for the data. 我会说这取决于您将数据的“逻辑”存储在何处。

1) If your stored procedures are mostly "dumb" ones with simple Inserts/Update/Delete. 1)如果您的存储过程大多是“哑”的,则带有简单的插入/更新/删除操作。 I would put the deletes in the data layer object where you would have more complex code. 我会将删除内容放在数据层对象中,在该对象中您将拥有更复杂的代码。

2)IF you have written many complex stored procedure, where you need to check biz rules. 2)如果您编写了许多复杂的存储过程,则需要检查biz规则。 Keep the logic all in the SPs and keep the data layer simple. 将逻辑全部保留在SP中,并使数据层保持简单。

Really it where you want the complexity to lie. 确实是您想要复杂性所在的地方。 Don't put too many rules in both the objects and the SPs, or else maintenance will be a bitch. 不要在对象和SP中都放置太多规则,否则维护会很麻烦。

Call me old fashioned but I would always let a database do what a database does; 称我为老式,但我总是让数据库执行数据库的工作; let it take care of the deletion and return a value back to the calling code to indicate the success or failure of the operation. 让它负责删除操作,并向调用代码返回一个值,以指示操作是成功还是失败。

I know that ADO.NET does a good job of managing SQL operations but I normally limit this to configuring the connection, adding the parameters and then running a stored procedure. 我知道ADO.NET在管理SQL操作方面做得很好,但是我通常将其限制为配置连接,添加参数然后运行存储过程。

My preference is to perform child deletes from managed code. 我的首选是从托管代码执行子级删除。 It is easier to ensure that all deletes are performed identically if done from here and it gives one easy place for another developer to come and understand how this entity works. 如果从此处进行删除,则确保所有删除操作均相同会更容易,并且这为另一位开发人员提供了一个轻松的地方来了解此实体的工作方式。

If child objects should always be deleted when the main object is deleted the best place to do that is in the database. 如果在删除主对象时始终应删除子对象,则最好的位置是在数据库中。 There are deltes that may happen from imports or queries that are not done through the GUI. 有些导入可能会因未通过GUI完成导入或查询而发生变化。 Think about what would happen if you needed to deelte all the books from a publisher that went out of business. 想一想,如果您需要从停业的出版商那里删除所有书籍,那会发生什么。 No one would do that one at a time through the GUI. 没有人会一次通过GUI做到这一点。 Therefore if cascading delete is an option in your database, then set it. 因此,如果级联删除是数据库中的一个选项,则进行设置。 If not do it through a trigger. 如果不是,则通过触发器进行操作。

Before doing this though, be very very sure that you want to delete the child objects. 但是,在执行此操作之前,请务必确保要删除子对象。 Will you lose historical data that you need? 您会丢失所需的历史数据吗? Often it is better to mark the parent record as inactive than to delete it. 通常,将父记录标记为不活动比删除它更好。 This way you don't lose the history on the book orders because you delted all the child records when the book was no longer available. 这样,您就不会丢失图书订单上的历史记录,因为当图书不再可用时,您会删除所有子记录。 You almost never want to delete the record if your system involves, ordering, warehousing, or financial systems or any other system where child tables would hold records that have a date history and which may need to be called in reports or when researching a customer call. 如果您的系统涉及订购,仓储或财务系统,或者子表将保存具有日期历史记录且可能需要在报告中或在研究客户呼叫时调用的任何其他系统,则几乎永远不希望删除记录。

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

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