加入專案檔案。
This commit is contained in:
10
CoreProfilerExample.Common/Constants/WeatherConstant.cs
Normal file
10
CoreProfilerExample.Common/Constants/WeatherConstant.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
namespace CoreProfilerExample.Common.Constants
|
||||
{
|
||||
public static class WeatherConstant
|
||||
{
|
||||
public readonly static string[] WeatherSummaries =
|
||||
[
|
||||
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,13 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\CoreProfilerExample.Common\CoreProfilerExample.Common.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,19 @@
|
||||
using CoreProfilerExample.Repository.Interfaces;
|
||||
using CoreProfilerExample.Repository.Models.DataModels;
|
||||
using static CoreProfilerExample.Common.Constants.WeatherConstant;
|
||||
|
||||
namespace CoreProfilerExample.Repository.Implements
|
||||
{
|
||||
public class WeatherForecastRepository : IWeatherForecastRepository
|
||||
{
|
||||
public Task<IEnumerable<WeatherForecastDataModel>> GetAsync(int days)
|
||||
{
|
||||
return Task.Run(() => days > 0 ? Enumerable.Range(1, days).Select(index => new WeatherForecastDataModel
|
||||
{
|
||||
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
|
||||
TemperatureC = Random.Shared.Next(-20, 55),
|
||||
Summary = WeatherSummaries[Random.Shared.Next(WeatherSummaries.Length)]
|
||||
}) : []);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
using CoreProfilerExample.Repository.Models.DataModels;
|
||||
|
||||
namespace CoreProfilerExample.Repository.Interfaces
|
||||
{
|
||||
public interface IWeatherForecastRepository
|
||||
{
|
||||
public Task<IEnumerable<WeatherForecastDataModel>> GetAsync(int days);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
namespace CoreProfilerExample.Repository.Models.DataModels
|
||||
{
|
||||
public class WeatherForecastDataModel
|
||||
{
|
||||
public DateOnly Date { get; set; }
|
||||
|
||||
public int TemperatureC { get; set; }
|
||||
|
||||
public string? Summary { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\CoreProfilerExample.Repository\CoreProfilerExample.Repository.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
33
CoreProfilerExample.Service/Extensions/MapperExtension.cs
Normal file
33
CoreProfilerExample.Service/Extensions/MapperExtension.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
using CoreProfilerExample.Repository.Models.DataModels;
|
||||
using CoreProfilerExample.Service.Models.Dtos;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CoreProfilerExample.Service.Extensions
|
||||
{
|
||||
public static class MapperExtension
|
||||
{
|
||||
public static async Task<GetWeatherForecastDto> ToDtoAsync(this Task<IEnumerable<WeatherForecastDataModel>> task)
|
||||
{
|
||||
var dataModels = await task;
|
||||
|
||||
return new GetWeatherForecastDto
|
||||
{
|
||||
WeatherForecasts = dataModels.Select(model => model.ToDto()),
|
||||
};
|
||||
}
|
||||
|
||||
public static GetWeatherForecastItemDto ToDto(this WeatherForecastDataModel dataModel)
|
||||
{
|
||||
return new GetWeatherForecastItemDto
|
||||
{
|
||||
Date = dataModel.Date,
|
||||
Summary = dataModel.Summary,
|
||||
TemperatureC = dataModel.TemperatureC,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
21
CoreProfilerExample.Service/Implements/WeatherService.cs
Normal file
21
CoreProfilerExample.Service/Implements/WeatherService.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using CoreProfilerExample.Repository.Interfaces;
|
||||
using CoreProfilerExample.Service.Extensions;
|
||||
using CoreProfilerExample.Service.Interfaces;
|
||||
using CoreProfilerExample.Service.Models.Dtos;
|
||||
using CoreProfilerExample.Service.Models.ParameterDtos;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CoreProfilerExample.Service.Implements
|
||||
{
|
||||
public class WeatherService(IWeatherForecastRepository weatherForecast) : IWeatherService
|
||||
{
|
||||
public Task<GetWeatherForecastDto> GetWeatherForecastAsync(GetWeatherForecastParameterDto parameterDto)
|
||||
{
|
||||
return weatherForecast.GetAsync(parameterDto.ForecastDays).ToDtoAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
10
CoreProfilerExample.Service/Interfaces/IWeatherService.cs
Normal file
10
CoreProfilerExample.Service/Interfaces/IWeatherService.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using CoreProfilerExample.Service.Models.Dtos;
|
||||
using CoreProfilerExample.Service.Models.ParameterDtos;
|
||||
|
||||
namespace CoreProfilerExample.Service.Interfaces
|
||||
{
|
||||
public interface IWeatherService
|
||||
{
|
||||
public Task<GetWeatherForecastDto> GetWeatherForecastAsync(GetWeatherForecastParameterDto parameterDto);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CoreProfilerExample.Service.Models.Dtos
|
||||
{
|
||||
public class GetWeatherForecastDto
|
||||
{
|
||||
public IEnumerable<GetWeatherForecastItemDto> WeatherForecasts { get; set; } = [];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CoreProfilerExample.Service.Models.Dtos
|
||||
{
|
||||
public class GetWeatherForecastItemDto
|
||||
{
|
||||
public DateOnly Date { get; set; }
|
||||
|
||||
public int TemperatureC { get; set; }
|
||||
|
||||
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
|
||||
|
||||
public string? Summary { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CoreProfilerExample.Service.Models.ParameterDtos
|
||||
{
|
||||
public class GetWeatherForecastParameterDto
|
||||
{
|
||||
public int ForecastDays { get; set; }
|
||||
}
|
||||
}
|
||||
43
CoreProfilerExample.sln
Normal file
43
CoreProfilerExample.sln
Normal file
@@ -0,0 +1,43 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 17
|
||||
VisualStudioVersion = 17.9.34607.119
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CoreProfilerExample", "CoreProfilerExample\CoreProfilerExample.csproj", "{A7B95E40-1807-45E8-831C-88246897C9B5}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CoreProfilerExample.Repository", "CoreProfilerExample.Repository\CoreProfilerExample.Repository.csproj", "{F5114126-C965-4F36-9302-AC897C29D898}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CoreProfilerExample.Service", "CoreProfilerExample.Service\CoreProfilerExample.Service.csproj", "{69C081ED-D31A-4C66-9EC9-5CF46A16C8E8}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CoreProfilerExample.Common", "CoreProfilerExample.Common\CoreProfilerExample.Common.csproj", "{DDD6FCC3-454E-42CB-94D0-23A7EA57D039}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{A7B95E40-1807-45E8-831C-88246897C9B5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{A7B95E40-1807-45E8-831C-88246897C9B5}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{A7B95E40-1807-45E8-831C-88246897C9B5}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{A7B95E40-1807-45E8-831C-88246897C9B5}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{F5114126-C965-4F36-9302-AC897C29D898}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{F5114126-C965-4F36-9302-AC897C29D898}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{F5114126-C965-4F36-9302-AC897C29D898}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{F5114126-C965-4F36-9302-AC897C29D898}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{69C081ED-D31A-4C66-9EC9-5CF46A16C8E8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{69C081ED-D31A-4C66-9EC9-5CF46A16C8E8}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{69C081ED-D31A-4C66-9EC9-5CF46A16C8E8}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{69C081ED-D31A-4C66-9EC9-5CF46A16C8E8}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{DDD6FCC3-454E-42CB-94D0-23A7EA57D039}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{DDD6FCC3-454E-42CB-94D0-23A7EA57D039}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{DDD6FCC3-454E-42CB-94D0-23A7EA57D039}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{DDD6FCC3-454E-42CB-94D0-23A7EA57D039}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {626E928F-5EA0-4F58-BFD4-1A47EF4043B4}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
30
CoreProfilerExample/Controllers/WeatherController.cs
Normal file
30
CoreProfilerExample/Controllers/WeatherController.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using CoreProfilerExample.Infrastructure.Extensions;
|
||||
using CoreProfilerExample.Models.Parameters;
|
||||
using CoreProfilerExample.Models.ViewModels;
|
||||
using CoreProfilerExample.Service.Interfaces;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Net;
|
||||
|
||||
namespace CoreProfilerExample.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("weather")]
|
||||
public class WeatherController(IWeatherService weather) : ControllerBase
|
||||
{
|
||||
[HttpPost("forecast", Name = "GetWeatherForecast")]
|
||||
[Produces("application/json")]
|
||||
[ProducesResponseType(typeof(GetWeatherForecastItemViewModel), (int)HttpStatusCode.OK)]
|
||||
[ProducesResponseType(typeof(ExceptionViewModel), (int)HttpStatusCode.BadRequest)]
|
||||
public async Task<IActionResult> GetWeatherForecastAsync(GetWeatherForecastParameter parameter)
|
||||
{
|
||||
try
|
||||
{
|
||||
return Ok(await weather.GetWeatherForecastAsync(parameter.ToDto()).ToViewModel());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return BadRequest(ex.ToViewModel());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
17
CoreProfilerExample/CoreProfilerExample.csproj
Normal file
17
CoreProfilerExample/CoreProfilerExample.csproj
Normal file
@@ -0,0 +1,17 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\CoreProfilerExample.Service\CoreProfilerExample.Service.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,22 @@
|
||||
using CoreProfilerExample.Repository.Implements;
|
||||
using CoreProfilerExample.Repository.Interfaces;
|
||||
using CoreProfilerExample.Service.Implements;
|
||||
using CoreProfilerExample.Service.Interfaces;
|
||||
|
||||
namespace CoreProfilerExample.Infrastructure.Extensions
|
||||
{
|
||||
public static class DependencyInjectionExtension
|
||||
{
|
||||
public static IServiceCollection AddRepository(this IServiceCollection services)
|
||||
{
|
||||
services.AddScoped<IWeatherForecastRepository, WeatherForecastRepository>();
|
||||
return services;
|
||||
}
|
||||
|
||||
public static IServiceCollection AddService(this IServiceCollection services)
|
||||
{
|
||||
services.AddScoped<IWeatherService, WeatherService>();
|
||||
return services;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
using CoreProfilerExample.Models.Parameters;
|
||||
using CoreProfilerExample.Models.ViewModels;
|
||||
using CoreProfilerExample.Service.Models.Dtos;
|
||||
using CoreProfilerExample.Service.Models.ParameterDtos;
|
||||
|
||||
namespace CoreProfilerExample.Infrastructure.Extensions
|
||||
{
|
||||
public static class MapperExtension
|
||||
{
|
||||
public static ExceptionViewModel ToViewModel(this Exception exception)
|
||||
{
|
||||
return new ExceptionViewModel
|
||||
{
|
||||
Message = exception.Message,
|
||||
};
|
||||
}
|
||||
|
||||
public static async Task<GetWeatherForecastViewModel> ToViewModel(this Task<GetWeatherForecastDto> task)
|
||||
{
|
||||
var dto = await task;
|
||||
|
||||
return new GetWeatherForecastViewModel
|
||||
{
|
||||
WeatherForecasts = dto.WeatherForecasts.Select(forecast => forecast.ToViewModel())
|
||||
};
|
||||
}
|
||||
|
||||
public static GetWeatherForecastItemViewModel ToViewModel(this GetWeatherForecastItemDto dto)
|
||||
{
|
||||
return new GetWeatherForecastItemViewModel
|
||||
{
|
||||
Date = dto.Date,
|
||||
Summary = dto.Summary,
|
||||
TemperatureC = dto.TemperatureC,
|
||||
TemperatureF = dto.TemperatureF,
|
||||
};
|
||||
}
|
||||
|
||||
public static GetWeatherForecastParameterDto ToDto(this GetWeatherForecastParameter parameter)
|
||||
{
|
||||
return new GetWeatherForecastParameterDto
|
||||
{
|
||||
ForecastDays = parameter.ForecastDays,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace CoreProfilerExample.Models.Parameters
|
||||
{
|
||||
public class GetWeatherForecastParameter
|
||||
{
|
||||
public int ForecastDays { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace CoreProfilerExample.Models.ViewModels
|
||||
{
|
||||
public class ExceptionViewModel
|
||||
{
|
||||
public string? Message { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
namespace CoreProfilerExample.Models.ViewModels
|
||||
{
|
||||
public class GetWeatherForecastItemViewModel
|
||||
{
|
||||
public DateOnly Date { get; set; }
|
||||
|
||||
public int TemperatureC { get; set; }
|
||||
|
||||
public int TemperatureF { get; set; }
|
||||
|
||||
public string? Summary { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace CoreProfilerExample.Models.ViewModels
|
||||
{
|
||||
public class GetWeatherForecastViewModel
|
||||
{
|
||||
public IEnumerable<GetWeatherForecastItemViewModel> WeatherForecasts { get; set; } = [];
|
||||
}
|
||||
}
|
||||
25
CoreProfilerExample/Program.cs
Normal file
25
CoreProfilerExample/Program.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using CoreProfilerExample.Infrastructure.Extensions;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Add services to the container.
|
||||
#region CoreProfilerExample
|
||||
builder.Services.AddRepository();
|
||||
builder.Services.AddService();
|
||||
#endregion
|
||||
builder.Services.AddControllers();
|
||||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI();
|
||||
|
||||
app.UseAuthorization();
|
||||
|
||||
app.MapControllers();
|
||||
|
||||
app.Run();
|
||||
31
CoreProfilerExample/Properties/launchSettings.json
Normal file
31
CoreProfilerExample/Properties/launchSettings.json
Normal file
@@ -0,0 +1,31 @@
|
||||
{
|
||||
"$schema": "http://json.schemastore.org/launchsettings.json",
|
||||
"iisSettings": {
|
||||
"windowsAuthentication": false,
|
||||
"anonymousAuthentication": true,
|
||||
"iisExpress": {
|
||||
"applicationUrl": "http://localhost:22953",
|
||||
"sslPort": 0
|
||||
}
|
||||
},
|
||||
"profiles": {
|
||||
"http": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"applicationUrl": "http://localhost:5100",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"IIS Express": {
|
||||
"commandName": "IISExpress",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
8
CoreProfilerExample/appsettings.Development.json
Normal file
8
CoreProfilerExample/appsettings.Development.json
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
||||
9
CoreProfilerExample/appsettings.json
Normal file
9
CoreProfilerExample/appsettings.json
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
Reference in New Issue
Block a user