1
0

練習資料庫紀錄完成

This commit is contained in:
2024-03-25 18:18:41 +08:00
parent 3382627c8c
commit 088ec7620a
17 changed files with 119 additions and 22 deletions

View File

@@ -8,6 +8,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="coreprofiler" Version="1.1.4" /> <PackageReference Include="coreprofiler" Version="1.1.4" />
<PackageReference Include="Microsoft.Data.Sqlite.Core" Version="8.0.3" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CoreProfilerExample.Common
{
public interface IDatabaseOption
{
public IDbConnection SqliteConnection { get; }
}
}

View File

@@ -0,0 +1,30 @@
using CoreProfiler;
using CoreProfiler.Data;
using Microsoft.Data.Sqlite;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CoreProfilerExample.Common.Options
{
public class DatabaseOption(IConfiguration configuration) : IDatabaseOption
{
public IDbConnection SqliteConnection
{
get
{
var connectionString = configuration.GetConnectionString("SQLITE");
var dbProfiler = ProfilingSession.Current != null
? new DbProfiler(ProfilingSession.Current.Profiler)
: null;
return new ProfiledDbConnection(new SqliteConnection(connectionString), dbProfiler);
}
}
}
}

View File

@@ -6,6 +6,10 @@
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<PackageReference Include="Dapper" Version="2.1.35" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\CoreProfilerExample.Common\CoreProfilerExample.Common.csproj" /> <ProjectReference Include="..\CoreProfilerExample.Common\CoreProfilerExample.Common.csproj" />
</ItemGroup> </ItemGroup>

View File

@@ -1,23 +1,47 @@
using CoreProfilerExample.Common.Extensions; using CoreProfiler;
using CoreProfilerExample.Common;
using CoreProfilerExample.Common.Extensions;
using CoreProfilerExample.Repository.Interfaces; using CoreProfilerExample.Repository.Interfaces;
using CoreProfilerExample.Repository.Models.DataModels; using CoreProfilerExample.Repository.Models.DataModels;
using Dapper;
using System.Reflection; using System.Reflection;
using static CoreProfilerExample.Common.Constants.WeatherConstant; using static CoreProfilerExample.Common.Constants.WeatherConstant;
namespace CoreProfilerExample.Repository.Implements namespace CoreProfilerExample.Repository.Implements
{ {
public class WeatherForecastRepository : IWeatherForecastRepository public class WeatherForecastRepository(IDatabaseOption option) : IWeatherForecastRepository
{ {
public Task<IEnumerable<WeatherForecastDataModel>> GetAsync(int days) public async Task<IEnumerable<WeatherForecastDataModel>> GetAsync(int days)
{ {
var type = MethodBase.GetCurrentMethod()?.DeclaringType;
var name = MethodBase.GetCurrentMethod()?.Name;
using (MethodBase.GetCurrentMethod()?.ProfilingStep()) using (MethodBase.GetCurrentMethod()?.ProfilingStep())
{ {
return Task.Run(() => days > 0 ? Enumerable.Range(1, days).Select(index => new WeatherForecastDataModel var forecasts = days > 0 ? Enumerable.Range(1, days).Select(index => new WeatherForecastDataModel
{ {
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)), Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)).ToString(),
TemperatureC = Random.Shared.Next(-20, 55), TemperatureC = Random.Shared.Next(-20, 55),
Summary = WeatherSummaries[Random.Shared.Next(WeatherSummaries.Length)] Summary = WeatherSummaries[Random.Shared.Next(WeatherSummaries.Length)]
}) : []); }) : [];
var sql = @"INSERT INTO [WeatherForecast] (
[Date],
[TemperatureC],
[Summary]
)
VALUES (
@Date,
@TemperatureC,
@Summary
)";
using var connection = option.SqliteConnection;
await connection.ExecuteAsync(sql, forecasts);
return forecasts;
} }
} }
} }

View File

@@ -2,7 +2,7 @@
{ {
public class WeatherForecastDataModel public class WeatherForecastDataModel
{ {
public DateOnly Date { get; set; } public string Date { get; set; } = null!;
public int TemperatureC { get; set; } public int TemperatureC { get; set; }

View File

@@ -2,7 +2,7 @@
{ {
public class GetWeatherForecastItemDto public class GetWeatherForecastItemDto
{ {
public DateOnly Date { get; set; } public string Date { get; set; } = null!;
public int TemperatureC { get; set; } public int TemperatureC { get; set; }

View File

@@ -8,6 +8,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="CoreProfiler.Web" Version="1.1.4" /> <PackageReference Include="CoreProfiler.Web" Version="1.1.4" />
<PackageReference Include="Microsoft.Data.Sqlite" Version="8.0.3" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" /> <PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup> </ItemGroup>

Binary file not shown.

View File

@@ -1,4 +1,5 @@
using CoreProfiler.Web; using CoreProfiler.Web;
using CoreProfilerExample.Common;
using CoreProfilerExample.Common.Options; using CoreProfilerExample.Common.Options;
using CoreProfilerExample.Repository.Implements; using CoreProfilerExample.Repository.Implements;
using CoreProfilerExample.Repository.Interfaces; using CoreProfilerExample.Repository.Interfaces;
@@ -9,6 +10,12 @@ namespace CoreProfilerExample.Infrastructure.Extensions
{ {
public static class DependencyInjectionExtension public static class DependencyInjectionExtension
{ {
public static IServiceCollection AddOption(this IServiceCollection services)
{
services.AddScoped<IDatabaseOption, DatabaseOption>();
return services;
}
public static IServiceCollection AddRepository(this IServiceCollection services) public static IServiceCollection AddRepository(this IServiceCollection services)
{ {
services.AddScoped<IWeatherForecastRepository, WeatherForecastRepository>(); services.AddScoped<IWeatherForecastRepository, WeatherForecastRepository>();

View File

@@ -2,7 +2,7 @@
{ {
public class GetWeatherForecastItemViewModel public class GetWeatherForecastItemViewModel
{ {
public DateOnly Date { get; set; } public string Date { get; set; } = null!;
public int TemperatureC { get; set; } public int TemperatureC { get; set; }

View File

@@ -4,6 +4,7 @@ var builder = WebApplication.CreateBuilder(args);
// Add services to the container. // Add services to the container.
#region CoreProfilerExample #region CoreProfilerExample
builder.Services.AddOption();
builder.Services.AddRepository(); builder.Services.AddRepository();
builder.Services.AddService(); builder.Services.AddService();
#endregion #endregion

View File

@@ -1,8 +1,6 @@
{ {
"Logging": { "ConnectionStrings": {
"LogLevel": { "SQLITE": "Data Source=CoreProfilerExample.db"
"Default": "Information", },
"Microsoft.AspNetCore": "Warning" "AllowedHosts": "*"
}
}
} }

View File

@@ -0,0 +1,3 @@
{
"AllowedHosts": "*"
}

View File

@@ -1,9 +1,3 @@
{ {
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*" "AllowedHosts": "*"
} }

Binary file not shown.

After

Width:  |  Height:  |  Size: 81 KiB

View File

@@ -116,4 +116,24 @@ public static IApplicationBuilder UseCoreProfiler(this IApplicationBuilder app)
紀錄的詳細如下: 紀錄的詳細如下:
![LastestProfilingResultDetail](CoreProfilerExampleResource/LastestProfilingResultDetail.JPG) ![LastestProfilingResultDetail](CoreProfilerExampleResource/LastestProfilingResultDetail.JPG)
## CoreProfiler extends DB
CoreProfiler也可用於紀錄SQL執行將原本資料庫使用的<code>Connection</code>替換成CoreProfiler實作的<code>Connection</code>就可以紀錄執行的T-SQL指令。
替換方法如下:
```C#
var connectionString = configuration.GetConnectionString("SQLITE");
var dbProfiler = ProfilingSession.Current != null
? new DbProfiler(ProfilingSession.Current.Profiler)
: null;
return new ProfiledDbConnection(new SqliteConnection(connectionString), dbProfiler);
```
在替換之前先確認CoreProfiler是否有運行的實體才替換資料庫連線執行結果如下:
![LastestProfilingResultDetailForDB](CoreProfilerExampleResource/LastestProfilingResultDetailForDB.JPG)