dotnetcore / SmartSql

SmartSql = MyBatis in C# + .NET Core+ Cache(Memory | Redis) + R/W Splitting + PropertyChangedTrack +Dynamic Repository + InvokeSync + Diagnostics
https://smartsql.net/
Apache License 2.0
1.1k stars 222 forks source link

接口上的 Transaction 特性无效 #62

Closed siegrainwong closed 5 years ago

siegrainwong commented 5 years ago

在虚方法上这个特性有效,在接口方法上无效: 请问是不支持接口吗?

有效示例:

[Transaction]
public virtual async Task<bool> UpdateAsync(ArticleUpdateParameter parameter)
{
     var externalTask = SetArticleTagsAndCategories(parameter.Id, parameter.Tags, parameter.Categories);
     var updateTask = ArticleRepository.UpdateAsync(parameter);
     await Task.WhenAll(externalTask, updateTask);
     return true;
}

image

无效示例:

[Transaction]
Task<int> DeleteAsync(int id);

image

Ahoo-Wang commented 5 years ago
  1. 代理仓储接口从使用上应该是不需要加事务AOP注解的,因为这个是单一操作,不需要加事务。
  2. 代理仓储接口是通过SmartSql的动态代理库动态生成的实现类,而SmartSql.AOP提供的事务AOP注解是通过 AspectCore-Framework 来提供的,这就相当于 动态代理 + 动态代理。
siegrainwong commented 5 years ago

明白了 ,谢谢解答 不过我的Delete里有五条语句,所以才想这样操作的。 现在声明了个虚方法加上Transaction实现了。

Ahoo-Wang commented 5 years ago

@Seanwong933 下个小版本将支持 image

siegrainwong commented 5 years ago

点赞~

Ahoo-Wang commented 5 years ago

Ref https://github.com/dotnetcore/SmartSql/releases/tag/v4.0.29

可以通过一下几种方式开启事物:

  1. 在动态仓储接口函数上加上 UseTransaction 注解
        [UseTransaction]
        long DoByAnnotationTransaction(AllPrimitive entity);
  2. 在 Statement 加上 Transaction 属性
    <Statement Id="DoByTransaction" Transaction="Unspecified">
    // do-something-0 
    // do-something-1
    // do-something-2 
    // do-something-3 
    </Statement>
  3. 在 RequestContext 传入 Transaction 属性
            var id = DbSession.ExecuteScalar<long>(new RequestContext
            {
                Scope = nameof(User),
                SqlId = "Insert",
                Transaction = IsolationLevel.Unspecified,
                Request = new User
                {
                   UserName= "SmartSql",
                }
            });
siegrainwong commented 5 years ago

谢谢,用上了~