有人能解释一下在Dapper中使用Glimpse的情况吗?还是它仅适用于Entity Framework?
编辑:
public List<UserRole> GetUserRoles(string userName)
{
using (var block = new TransactionBlock())
{
const string sql = "SELECT AspNetRoles.Name FROM AspNetUsers INNER JOIN " +
"AspNetUserRoles ON AspNetUsers.Id = AspNetUserRoles.UserId INNER JOIN " +
"AspNetRoles ON AspNetUserRoles.RoleId = AspNetRoles.Id " +
"WHERE (AspNetUsers.UserName = @userName)";
var results = TransactionBlock.Connection.Query<UserRole>(sql, new{userName}, transaction: TransactionBlock.Transaction).ToList();
block.Commit();
return results;
}
}
*EDIT *
public class TransactionBlock : IDisposable
{
private bool m_FirstTransactionBlock = false;
private bool m_Disposed = false;
public TransactionBlock(string connectionStringConfigKey = null)
{
if (connectionStringConfigKey != null && CurrentTransaction != null)
throw new Exception("Can't create a new transactionBlock with a specific connectionstring if you are already within a block.");
// Open a connection if we haven't yet already
if (CurrentTransaction == null) // we don't have a transaction yet so create one:
{
var localConnectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
var conn = new SqlConnection(localConnectionString); // Create a new connection object for the connection string.
try
{
conn.Open();
}
catch (SqlException ex)
{
// Severity 20 errors are returned when there are connection
// problems, so we only retry if the severity is 20.
// Don't worry about deadlock errors in here - we're just connecting
// to the database, not running anything which could be deadlocked.
if (ex.Class != 20) throw;
// Clear connection pool of all connections related to the given connection string.
SqlConnection.ClearPool(conn);
// Try to open the connection once again, catching any error.
try { conn.Open(); }
catch { conn = null; }
// If 2nd attempt failed, throw original exception.
if (conn == null) throw;
}
CurrentTransaction = conn.BeginTransaction();
m_FirstTransactionBlock = true;
}
}
public void Commit()
{
if (m_FirstTransactionBlock)
{
Dispose(true);
}
}
public void Dispose()
{
if (m_FirstTransactionBlock)
{
Dispose(false);
}
}
public void Dispose(bool commit)
{
if (m_Disposed)
return; // committed and cleaned up already.
try
{
var transaction = CurrentTransaction;
var connection = transaction.Connection; // taking a reference to the connection object as rollback will set it to null;
if (commit)
{
transaction.Commit();
}
else // rollback the transaction if it hasn't been commited.
{
transaction.Rollback();
}
if (connection != null)
{
connection.Close();
connection.Dispose();
}
transaction.Dispose();
}
finally // ensure that regardless of connection exceptions, the state is set to disposed.
{
// remove current transaction from context.
CurrentTransaction = null;
m_Disposed = true;
}
}
private static SqlTransaction CurrentTransaction
{
get
{
return CurrentContext.GetItem<SqlTransaction>("__Transaction");
}
set
{
CurrentContext.SetItem("__Transaction", value);
}
}
public static SqlConnection Connection
{
get { return CurrentTransaction.Connection; }
}
public static SqlTransaction Transaction
{
get { return CurrentTransaction; }
}
public static string ConnectionString
{
get
{
return System.Configuration.ConfigurationManager.AppSettings.Get("ConnectionString");
}
}
conn.ConnectionString = localConnectionString.ConnectionString;
- Gary Chapman