Asp.NETとDapper.NETを垣間見る

dapper glimpse

質問

誰でもDapperでGlimpseを使用することについて光を当てることはできますか、それとも純粋にEntity Frameworkだけですか?

EDIT

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

受け入れられた回答

ああ、GlimpseがDB呼び出しを傍受していない理由をすぐに示すTransactionBlockコードをありがとう。

次の行を変更して、 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();

人気のある回答

Glimpse.ADOパッケージを使ってみましたか?



ライセンスを受けた: CC-BY-SA with attribution
所属していない Stack Overflow
このKBは合法ですか? はい、理由を学ぶ
ライセンスを受けた: CC-BY-SA with attribution
所属していない Stack Overflow
このKBは合法ですか? はい、理由を学ぶ