简单数据工作单元实施

发布于 2024-11-29 06:30:15 字数 76 浏览 1 评论 0原文

我试图在 Simple.Data 中找到工作单元模式的示例实现。有人有吗?我目前正在使用非通用存储库,并被告知实施 UoW 是要做的事情。

I'm trying to find an example implementation of the Unit Of Work pattern in Simple.Data. Does anybody have one? I'm currently using non generic repositories and have been told that implementing UoW is something to do.

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

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

发布评论

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

评论(1

一绘本一梦想 2024-12-06 06:30:15

如果您想要从工作单元中得到的是事务涵盖的一组插入/更新/删除操作,那么这是受支持的:(

var db = Database.Open();
var tx = db.BeginTransaction(); // Internal IDbConnection opened by this call
try
{
    order = tx.Orders.Insert(order); // Returned record will have new IDENTITY value
    foreach (var item in items)
    {
        item.OrderId = order.Id;
        tx.Items.Insert(item);
    }
    tx.Commit(); // Internal IDbConnection closed by this call...
}
catch
{
    tx.Rollback(); // ...or this call :)
}

注意:此代码假设您使用的是 Ado 适配器,IDENTITY 指的是 SQL Server,但代码可以在任何 Ado 提供程序和任何支持事务的适配器上运行。)

如果您希望能够创建一批操作并一次性运行它们,那么目前不直接支持,但是我愿意接受功能请求或补丁。

如果您要跟踪对象的更改,可能需要了解的一件事是,从 Simple.Data 0.9 开始,SimpleRecord 实现了 ICloneable,因此您可以在选择记录后立即获取记录的副本,并在保存时使用它进行比较后退。我将很快推出一个版本,支持 Update(current, Original) 方法,该方法将执行乐观并发更新。

If what you want from the Unit of Work is a set of insert/update/delete operations covered by a transaction, then that is supported:

var db = Database.Open();
var tx = db.BeginTransaction(); // Internal IDbConnection opened by this call
try
{
    order = tx.Orders.Insert(order); // Returned record will have new IDENTITY value
    foreach (var item in items)
    {
        item.OrderId = order.Id;
        tx.Items.Insert(item);
    }
    tx.Commit(); // Internal IDbConnection closed by this call...
}
catch
{
    tx.Rollback(); // ...or this call :)
}

(Note: this code assumes you're using the Ado adapter, and IDENTITY refers to SQL Server, but the code will work on any of the Ado providers and on any adapter which supports transactions.)

If you want to be able to create a batch of operations and run them all in one go, then that's not directly supported at the moment, but I'm open to feature requests or patches.

If you're after change tracking on objects, one thing that might help to know is that as of Simple.Data 0.9, SimpleRecord implements ICloneable, so you can take a copy of a record just after selecting it and use it for comparison when saving back. I'm going to push a release soon with support for an Update(current, original) method which will do optimistic-concurrency updates.

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