瀏覽代碼

refactor extension methods

master
Savorboard 6 年之前
父節點
當前提交
60c028f11c
共有 6 個文件被更改,包括 48 次插入89 次删除
  1. +2
    -8
      src/DotNetCore.CAP.MongoDB/ICapTransaction.MongoDB.cs
  2. +23
    -1
      src/DotNetCore.CAP.MySql/ICapTransaction.MySql.cs
  3. +22
    -1
      src/DotNetCore.CAP.PostgreSql/ICapTransaction.PostgreSql.cs
  4. +1
    -1
      src/DotNetCore.CAP/ICapTransaction.Base.cs
  5. +0
    -38
      src/DotNetCore.CAP/Internal/RelationDbTransaction.cs
  6. +0
    -40
      src/DotNetCore.CAP/TransactionExtensions.cs

+ 2
- 8
src/DotNetCore.CAP.MongoDB/ICapTransaction.MongoDB.cs 查看文件

@@ -58,18 +58,12 @@ namespace DotNetCore.CAP
return transaction;
}

public static IClientSessionHandle BeginAndJoinToTransaction(this IClientSessionHandle clientSessionHandle,
public static IClientSessionHandle StartTransaction(this IMongoClient client,
ICapPublisher publisher, bool autoCommit = false)
{
var clientSessionHandle = client.StartSession();
var capTrans = publisher.Transaction.Begin(clientSessionHandle, autoCommit);
return new CapMongoDbClientSessionHandle(capTrans);
}

public static IClientSessionHandle StartAndJoinToTransaction(this IMongoClient client,
ICapPublisher publisher, bool autoCommit = false)
{
var clientSessionHandle = client.StartSession();
return BeginAndJoinToTransaction(clientSessionHandle, publisher, autoCommit);
}
}
}

+ 23
- 1
src/DotNetCore.CAP.MySql/ICapTransaction.MySql.cs 查看文件

@@ -64,12 +64,34 @@ namespace DotNetCore.CAP
return transaction;
}

public static IDbContextTransaction BeginAndJoinToTransaction(this DatabaseFacade database,
public static ICapTransaction Begin(this ICapTransaction transaction,
IDbTransaction dbTransaction, bool autoCommit = false)
{

transaction.DbTransaction = dbTransaction;
transaction.AutoCommit = autoCommit;

return transaction;
}

public static IDbContextTransaction BeginTransaction(this DatabaseFacade database,
ICapPublisher publisher, bool autoCommit = false)
{
var trans = database.BeginTransaction();
var capTrans = publisher.Transaction.Begin(trans, autoCommit);
return new CapEFDbTransaction(capTrans);
}

public static ICapTransaction BeginTransaction(this IDbConnection dbConnection,
ICapPublisher publisher, bool autoCommit = false)
{
if (dbConnection.State == ConnectionState.Closed)
{
dbConnection.Open();
}

var dbTransaction = dbConnection.BeginTransaction();
return publisher.Transaction.Begin(dbTransaction, autoCommit);
}
}
}

+ 22
- 1
src/DotNetCore.CAP.PostgreSql/ICapTransaction.PostgreSql.cs 查看文件

@@ -55,6 +55,15 @@ namespace DotNetCore.CAP

public static class CapTransactionExtensions
{
public static ICapTransaction Begin(this ICapTransaction transaction,
IDbTransaction dbTransaction, bool autoCommit = false)
{
transaction.DbTransaction = dbTransaction;
transaction.AutoCommit = autoCommit;

return transaction;
}

public static ICapTransaction Begin(this ICapTransaction transaction,
IDbContextTransaction dbTransaction, bool autoCommit = false)
{
@@ -64,7 +73,19 @@ namespace DotNetCore.CAP
return transaction;
}

public static IDbContextTransaction BeginAndJoinToTransaction(this DatabaseFacade database,
public static ICapTransaction BeginTransaction(this IDbConnection dbConnection,
ICapPublisher publisher, bool autoCommit = false)
{
if (dbConnection.State == ConnectionState.Closed)
{
dbConnection.Open();
}

var dbTransaction = dbConnection.BeginTransaction();
return publisher.Transaction.Begin(dbTransaction, autoCommit);
}

public static IDbContextTransaction BeginTransaction(this DatabaseFacade database,
ICapPublisher publisher, bool autoCommit = false)
{
var trans = database.BeginTransaction();


+ 1
- 1
src/DotNetCore.CAP/ICapTransaction.Base.cs 查看文件

@@ -19,7 +19,7 @@ namespace DotNetCore.CAP

public object DbTransaction { get; set; }

protected internal void AddToSent(CapPublishedMessage msg)
protected internal virtual void AddToSent(CapPublishedMessage msg)
{
_bufferList.Add(msg);
}


+ 0
- 38
src/DotNetCore.CAP/Internal/RelationDbTransaction.cs 查看文件

@@ -1,38 +0,0 @@
// Copyright (c) .NET Core Community. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System.Data;

namespace DotNetCore.CAP.Internal
{
internal class RelationDbTransaction : IDbTransaction
{
private readonly ICapTransaction _capTransaction;

public RelationDbTransaction(ICapTransaction capTransaction)
{
_capTransaction = capTransaction;
var dbTransaction = (IDbTransaction) capTransaction.DbTransaction;
Connection = dbTransaction.Connection;
IsolationLevel = dbTransaction.IsolationLevel;
}

public void Dispose()
{
_capTransaction.Dispose();
}

public void Commit()
{
_capTransaction.Commit();
}

public void Rollback()
{
_capTransaction.Rollback();
}

public IDbConnection Connection { get; }
public IsolationLevel IsolationLevel { get; }
}
}

+ 0
- 40
src/DotNetCore.CAP/TransactionExtensions.cs 查看文件

@@ -1,40 +0,0 @@
// Copyright (c) .NET Core Community. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System.Data;
using DotNetCore.CAP.Internal;

namespace DotNetCore.CAP
{
public static class TransactionExtensions
{
public static ICapTransaction Begin(this ICapTransaction transaction,
IDbTransaction dbTransaction, bool autoCommit = false)
{

transaction.DbTransaction = dbTransaction;
transaction.AutoCommit = autoCommit;

return transaction;
}

public static IDbTransaction JoinToTransaction(this IDbTransaction dbTransaction,
ICapPublisher publisher, bool autoCommit = false)
{
dbTransaction = new RelationDbTransaction(publisher.Transaction.Begin(dbTransaction, autoCommit));
return dbTransaction;
}

public static IDbTransaction BeginAndJoinToTransaction(this IDbConnection dbConnection,
ICapPublisher publisher, bool autoCommit = false)
{
if (dbConnection.State == ConnectionState.Closed)
{
dbConnection.Open();
}

var dbTransaction = dbConnection.BeginTransaction();
return dbTransaction.JoinToTransaction(publisher, autoCommit);
}
}
}

Loading…
取消
儲存