在 csla 和手动 transactionscope 中使用事务

发布于 2024-09-08 02:30:09 字数 1049 浏览 2 评论 0原文

因此,希望掌握一些 CSLA 技能可以帮助我找到更好的方法。我有一种情况,我想从对象外部手动创建事务并将 transactionAttribute 设置为手动。

所以我有一些如下所示的代码:

using (SqlConnection conn ConnectionManager<SqlConnection>.GetManager("Db").Connection)
{
    connectionTransaction = conn.BeginTransaction();

    objectRef = objectRef.Save();

    //other logic here

    objectRef = objectRef.Save();
    connectionTransaction.Commit();
}

然后在 save 方法中,有类似的数据访问:

using (var conn = ConnectionManager<SqlConnection>.GetManager("Db").Connection)
{
    using (var cm = conn.CreateCommand())
    {
        cm.CommandType = CommandType.StoredProcedure;
        cm.CommandText = "Proc_Name";

        // param definitions

        cm.ExecuteNonQuery();
    }
}

当我这样做时,我收到以下错误:

ExecuteNonQuery 要求命令在连接分配给命令时有一个事务处于待处理的本地事务中。命令的 Transaction 属性尚未初始化。

好吧,这是有道理的,我可以通过将事务分配给命令来轻松修复。但我想知道这是否真的根据 CSLA 最佳实践正确构建。我似乎无法在我最近得到的书中找到一个很好的例子。

我只是无法想象告诉我的域对象代码表现得像在事务中一样是一种很好的做法,而有时可能并非如此。

谁能告诉我我需要做什么才能正确解决这个问题?

So hopefully with some CSLA skills out there can help me see a better way with this one. I have a situation where I want to manually create my transaction from outside the object and set my transactionAttribute to manual.

So I have some code that looks like this:

using (SqlConnection conn ConnectionManager<SqlConnection>.GetManager("Db").Connection)
{
    connectionTransaction = conn.BeginTransaction();

    objectRef = objectRef.Save();

    //other logic here

    objectRef = objectRef.Save();
    connectionTransaction.Commit();
}

Then inside the save method there is something like this for the data access:

using (var conn = ConnectionManager<SqlConnection>.GetManager("Db").Connection)
{
    using (var cm = conn.CreateCommand())
    {
        cm.CommandType = CommandType.StoredProcedure;
        cm.CommandText = "Proc_Name";

        // param definitions

        cm.ExecuteNonQuery();
    }
}

When I do this I receive the following error:

ExecuteNonQuery requires the command to have a transaction when the connection assigned to the command is in a pending local transaction. The Transaction property of the command has not been initialized.

Ok so that makes sense what it is saying and I could easily fix by assigning the transaction to the command. But I'm wondering if this is actually structured correctly according to CSLA best practices. I can't seem to find a good example in the book I recently got.

I just can't imagine that it is good practice to tell my domain object code to behave like it is in a transaction when there could be times when it isn't.

Can anyone show me the light on what I need to do to fix this correctly?

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

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

发布评论

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

评论(1

我的鱼塘能养鲲 2024-09-15 02:30:09

好吧,我需要改变很多事情。但这种情况的一个明显问题是我在 using 语句中使用了连接。一旦它退出,它就会被处理掉。由于该事务经历了几个这样的方法,所以我的连接和事务在第一次调用后就被释放了。

我现在刚刚经历过这样的时刻之一,我意识到了这一点。我讨厌有时答案如此明显。

Ok so there were a lot of things I needed to change with this. But one of the glaring problems with this situation is that I have the connection being used in the using statement. Which as soon as it exits out it is being disposed of. Since this transaction goes through several of these methods like this it turned out my connection and transaction were being disposed of after the first call.

I just had one of those duh moments right now and I realized it. I hate how sometimes the answer can be so obvious.

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