前一篇「MiniProfiler 3 + ASP.NET WebForms + ADO.NET」是說明怎麼在 ASP.NET WebForms 與 ADO.NET 的情況下去怎麼使用 MiniProfiler 以及如何修改設定,同樣的在 twMVC#15 裡也有分享另一個套件「Glimpse」。
與 MiniProfiler 比較起來,Glimpse 不止是提供 EF 的 SQL Command 追蹤或執行方法的效能追蹤,此外還有提供多種的頁面訊息,讓我們在開發時可以掌握更多的資訊,這些在以前的文章都曾經有介紹過,可以查看部落格裡的 Glimpse 相關文章。
之前的文章都是以 ASP.NET MVC + Entity Framework 的情況下來做說明,而 Glimpse 在 1.7.0 版之後也發布了對於 ASP.NET WebForms 的支援,而 Glimpse.WebForms 可以說是原本 ASP.NET Trace 功能的進階加強版,所能提供的資訊與內容是更多樣且更為清晰。
但是在使用 ASP.NET WebForms + ADO.NET 的情況下,如果使用 Glimpse 而沒有經過任何調整的處理,那麼在 SQL Tab 是不會有任何作用與資訊,所以這一篇就來說明要怎麼調整讓 Glimpse 的 SQL Tab 可以顯示頁面執行 ADO.NET 存取資料的 SQL Command 內容。
Glimpse
對於 Glimpse 不了解或是不知道它有什麼用處的朋友,可以前往下面的連結,先體驗一下 Glimpser 的功能,
而至於想要了解 Glimpse for ASP.NET WebForms 的朋友,可以看看以下連結的影片,大概就能夠了解,
https://www.youtube.com/watch?v=vS6ONq6YwyI
實作
範例程式的架構跟前一篇文章「MiniProfiler 3 + ASP.NET WebForms + ADO.NET」的是一樣的,
然後透過 NuGet 加入以下的 Glimpse 相關套件:
Glimpse Core
Glimpse WebForms
Glimpse ADO
Glimpse ASP.NET
安裝完成之後並不需要修改 Global.asax.cs 與 Site.Master,不過必須要修改透過 ADO.NET 資料存取的部份程式內容,因為要使用 Glimpse 所提供的方法來建立 DbConnection 與 DbCommand,如此才能在 Glimpse 的 SQL Tab 顯示 SQL Command 內容。
BaseRepository.cs
在 BaseRepository 裡有一個 CreateGlimpseDbConnection 方法,這是用來建立 GlimpseDbConnection。
namespace WebApplication1.Models.Repository{public abstract class BaseRepository
{private string _connectionString;
public string ConnectionString
{ get { return _connectionString; } set { _connectionString = value; }}
public string ProviderName
{get
{return WebConfigurationManager.ConnectionStrings["Northwind"].ProviderName;
}
}
public BaseRepository() {this.ConnectionString = WebConfigurationManager.ConnectionStrings["Northwind"].ConnectionString;
}
public BaseRepository(string connectionString)
{if (!string.IsNullOrWhiteSpace(connectionString))
{ this._connectionString = connectionString;}
}
public GlimpseDbConnection CreateGlimpseDbConnection() {return new GlimpseDbConnection(new SqlConnection(this.ConnectionString));
}
}
}
CategoryRepository - GetCategories 方法
DbConnection 要使用在 BaseRepository 的 GetGlimpseDbConnection 方法來取得 GlimpseDbConnection,在方法裡的 DbCommand 建立,這地方要建立 Glimpse.ADO 的 GlimpseDbCommand 類別的物件,除此之外就沒有地方要做修改了。
public List<Category> GetCategories(){ var categories = new List<Category>();string sqlStatement = "select * from Categories order by CategoryID desc";
using (var conn = this.CreateGlimpseDbConnection())
using (var command = new GlimpseDbCommand(new SqlCommand()))
{command.Connection = conn;
command.CommandType = CommandType.Text;
command.CommandTimeout = 180;
command.CommandText = sqlStatement;
if (conn.State != ConnectionState.Open) conn.Open(); using (IDataReader reader = command.ExecuteReader()) { while (reader.Read()) { var item = new Category {CategoryID = int.Parse(reader["CategoryID"].ToString()),
CategoryName = reader["CategoryName"].ToString(), Description = reader["Description"].ToString()};
categories.Add(item);
}
}
}
return categories;}
CategoryRepository – GetOne 方法
/// <summary>/// Gets the one./// </summary>/// <param name="id">The id.</param>/// <returns></returns>public Category GetOne(int id)
{string sqlStatement = "select * from Categories where CategoryID = @CategoryID";
var item = new Category();using (var conn = this.CreateGlimpseDbConnection())
using (var command = new GlimpseDbCommand(new SqlCommand()))
{command.Connection = conn;
command.CommandType = CommandType.Text;
command.CommandTimeout = 180;
command.CommandText = sqlStatement;
command.Parameters.Add(new SqlParameter("CategoryID", id));
if (conn.State != ConnectionState.Open) conn.Open(); using (IDataReader reader = command.ExecuteReader()) { if (reader.Read()) {item.CategoryID = int.Parse(reader["CategoryID"].ToString());
item.CategoryName = reader["CategoryName"].ToString(); item.Description = reader["Description"].ToString();}
}
}
return item;}
執行結果
參考連結
Manual ADO Integration · Glimpse/Glimpse Wiki
以上
沒有留言:
張貼留言