Skip to content
This repository was archived by the owner on May 8, 2021. It is now read-only.
This repository was archived by the owner on May 8, 2021. It is now read-only.

沙发,关于 Cap 接入问题建议 #1

Description

@2881099

大哥好,你现在这样封装肯定可以。

问题初衷:

如何在外部控制事务提交?

因为提交事务的操作中可能还有其他操作,不是简单 tran.Commit() 就完事。

如果把事务提交权交给 Cap,整个事务就终断。

伪代码如下:

using (var uow = ....())
{
    _capPublisher.Transaction.Value.Begin(uow.Transaction);
    //...
    uow.Commit(); // 这样提交 _capPublisher.Flush 没有被执行
}
using (var uow = ....())
{
    _capPublisher.Transaction.Value.Begin(uow.Transaction);
    //...
    capPublisher.Commit(); // 这样提交 uow.Commit 内部其他逻辑没有被执行
}

建议:开放 Flush 方法给外部使用

Cap 作者否决了我的意图,不给支持,其实很简单的一个改动。如果每个数据库自己 copy 代码来改比较复杂,建议代码如下:

public static class CapUnitOfWorkExtenssions
{
    public static void SetCapPublisher(this IUnitOfWork fsql, ICapPublisher capPublisher)
    {
        capPublisher.Transaction.Value.Begin(uow.GetOrBeginTransaction());
    }
    public static void Flush(this ICapPublisher capPublisher)
    {
        capPublisher?.GetType().GetMethod("Flush", BindingFlags.Instance | BindingFlags.NonPublic)
            .Invoke(capPublisher, null);
    }
}

using (var uow = fsql.CreateUnitOfWork())
{
    uow.SetCapPublisher(_capPublisher);
    //...
    uow.Commit();
    capPublisher.Flush(); //执行 mq 推送
}

这样一来就不用为每个数据库去做这个了

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions