Glimpse ASp.NET和Dapper.NET

时间:2014-02-15 17:54:09

标签: dapper glimpse

任何人都可以对Gapmpse与Dapper的使用有所了解,还是纯粹只是为了实体框架?

修改

 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;
        }
    }

* 编辑*

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");
        }
    }

2 个答案:

答案 0 :(得分:6)

啊,感谢TransactionBlock代码,它立即显示为什么Glimpse不拦截任何数据库调用。

尝试通过更改以下行来使用DbProviderFactories类:

var localConnectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
var conn = new SqlConnection(localConnectionString);       

var localConnectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"];
var factory = DbProviderFactories.GetFactory(localConnectionString.ProviderName); 
var conn = factory.CreateConnection();

答案 1 :(得分:2)

您是否尝试过使用Glimpse.ADO软件包?