Fix unused ref
Add CodeFirst support
Add global context -> App
This commit is contained in:
Ling 2024-10-10 17:05:35 +08:00
parent c3c740ed78
commit dad8608d63
11 changed files with 119 additions and 29 deletions

View File

@ -2,13 +2,16 @@ using LFlow.Base.BusinessInterface;
using LFlow.Base.Interfaces; using LFlow.Base.Interfaces;
using LFlow.Home.Models.DtoModel; using LFlow.Home.Models.DtoModel;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Serilog;
using Serilog.Core;
namespace LFlow.Home.Controllers; namespace LFlow.Home.Controllers;
public class HomeController(IHomeService<HomeDto, string> service) : BaseController public class HomeController(IHomeService<HomeDto, string> service, ILogger logger) : BaseController
{ {
[HttpGet] [HttpGet]
public string Home(string id) public string Home(string id)
{ {
logger.Information($"request id -> {id} _ hot_reload _ 1");
return service?.GetById(id)?.ToString() ?? "No service"; return service?.GetById(id)?.ToString() ?? "No service";
} }
} }

View File

@ -21,7 +21,6 @@
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\LFlow\LFlow.Base.csproj" /> <ProjectReference Include="..\LFlow\LFlow.Base.csproj" />
<ProjectReference Include="..\LFlow.Interfaces\LFlow.Interfaces.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -12,6 +12,11 @@ public class HomeModel : IDataModel
[SugarColumn(ColumnName = "ID", IsPrimaryKey = true)] [SugarColumn(ColumnName = "ID", IsPrimaryKey = true)]
public string? Id { get; set; } public string? Id { get; set; }
public string? ModuleName { get; set; }
public string? ModuleUrl { get; set; }
public string? ModuleVer { get; set; }
public override string ToString() public override string ToString()
{ {
return $"HomeModel {Id}"; return $"HomeModel {Id}";

View File

@ -1,4 +1,3 @@
using System;
using LFlow.Base.Interfaces; using LFlow.Base.Interfaces;
using LFlow.Base.Utils; using LFlow.Base.Utils;
using LFlow.Home.Models.DataModels; using LFlow.Home.Models.DataModels;
@ -7,10 +6,11 @@ using LFlow.Home.Models.DataModels;
// using LFlow.Interfaces; // using LFlow.Interfaces;
using LFlow.Home.Models.DtoModel; using LFlow.Home.Models.DtoModel;
using LFlow.Base.BusinessInterface; using LFlow.Base.BusinessInterface;
using Serilog;
namespace LFlow.Home.Services; namespace LFlow.Home.Services;
public class HomeService(IRepo<HomeModel, string> repo) : IHomeService<HomeDto, string> public class HomeService(IRepo<HomeModel, string> repo, ILogger logger) : IHomeService<HomeDto, string>
{ {
public HomeDto DeleteById(string id) public HomeDto DeleteById(string id)
{ {
@ -20,6 +20,7 @@ public class HomeService(IRepo<HomeModel, string> repo) : IHomeService<HomeDto,
public HomeDto GetById(string id) public HomeDto GetById(string id)
{ {
logger.Information($"GetById id -> {id}");
var result = repo.Get(id); var result = repo.Get(id);
return Mapper.Map<HomeDto>(result); return Mapper.Map<HomeDto>(result);
} }

View File

@ -1,10 +1,12 @@
using System; using System;
using LFlow.Base.Interfaces; using LFlow.Base.Interfaces;
using SqlSugar;
namespace LFlow.User.Model.DataModel; namespace LFlow.User.Model.DataModel;
[SugarTable("T_B_USER")]
public record UserModel : IDataModel public record UserModel : IDataModel
{ {
[SugarColumn(IsPrimaryKey = true)]
public string? UserID { get; set; } public string? UserID { get; set; }
public string? UserName { get; set; } public string? UserName { get; set; }

15
LFlow/App.cs Normal file
View File

@ -0,0 +1,15 @@
using System;
namespace LFlow.Base;
/// <summary>
/// 程序对象
/// </summary>
public class App
{
public static IServiceCollection? services;
private static IServiceProvider? serviceProvider => services?.BuildServiceProvider();
public static T? GetService<T>()
{
return serviceProvider.GetService<T>();
}
}

View File

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup> <PropertyGroup>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
@ -13,6 +13,10 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Mapster" Version="7.4.0" /> <PackageReference Include="Mapster" Version="7.4.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.1" /> <PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.1" />
<PackageReference Include="Serilog" Version="4.0.2" />
<PackageReference Include="Serilog.AspNetCore" Version="8.0.2" />
<PackageReference Include="Serilog.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
<PackageReference Include="SQLite" Version="3.13.0" /> <PackageReference Include="SQLite" Version="3.13.0" />
<PackageReference Include="SqlSugarCore" Version="5.1.4.169" /> <PackageReference Include="SqlSugarCore" Version="5.1.4.169" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.8.1" /> <PackageReference Include="Swashbuckle.AspNetCore" Version="6.8.1" />

View File

@ -4,6 +4,8 @@ using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Npgsql.Replication; using Npgsql.Replication;
using Serilog;
using Serilog.Events;
using SqlSugar; using SqlSugar;
namespace LFlow.Base; namespace LFlow.Base;
@ -12,15 +14,33 @@ public static class Program
public static void Main(string[] args) public static void Main(string[] args)
{ {
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
.Enrich.FromLogContext()
.WriteTo.Console()
.CreateBootstrapLogger();
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSerilog((services, lc) => lc
.ReadFrom.Configuration(builder.Configuration)
.ReadFrom.Services(services)
.Enrich.FromLogContext()
// .WriteTo.Console()
);
Log.Logger.Information("LoadSubService");
builder.Services.LoadSubService(); builder.Services.LoadSubService();
builder.Services.AddEndpointsApiExplorer(); builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(); builder.Services.AddSwaggerGen();
builder.Services.AddHttpContextAccessor(); builder.Services.AddHttpContextAccessor();
Log.Logger.Information("ConfigureSqlSugar");
builder.Services.ConfigureSqlSugar(); builder.Services.ConfigureSqlSugar();
builder.Services.AddControllers(); builder.Services.AddControllers();
builder.Services.AddSwaggerGen(u => builder.Services.AddSwaggerGen(u =>
{ {
u.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo u.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo
@ -35,8 +55,15 @@ public static class Program
} }
}); });
}); });
var app = builder.Build();
builder.Host.UseSerilog();
// init App.service
App.services = builder.Services;
var app = builder.Build();
app.UseSerilogRequestLogging(options =>
{
options.GetLevel = (httpContext, elapsed, ex) => LogEventLevel.Information;
});
app.MapControllers(); app.MapControllers();
// app.MapGet("/", () => "Hello World!"); // app.MapGet("/", () => "Hello World!");
// if (app.Environment.IsDevelopment()) // if (app.Environment.IsDevelopment())
@ -57,7 +84,7 @@ public static class Program
SqlSugarScope sqlSugar = new(new ConnectionConfig() SqlSugarScope sqlSugar = new(new ConnectionConfig()
{ {
DbType = SqlSugar.DbType.Sqlite, DbType = SqlSugar.DbType.Sqlite,
ConnectionString = "DataSource=sqlsugar-dev.db", ConnectionString = "DataSource=LFlow-dev.db",
IsAutoCloseConnection = true, IsAutoCloseConnection = true,
}, },
db => db =>
@ -87,7 +114,7 @@ public static class Program
{ {
foreach (var file in files) foreach (var file in files)
{ {
Console.Write($"Load file -> {file}..."); Log.Logger.Information($"Load file -> {file}...");
var assembly = Assembly.LoadFile(file); var assembly = Assembly.LoadFile(file);
var types = assembly.GetTypes(); var types = assembly.GetTypes();
bool isUseController = false; bool isUseController = false;
@ -99,9 +126,6 @@ public static class Program
var interfaces = type.GetInterfaces(); var interfaces = type.GetInterfaces();
foreach (var inter in interfaces) foreach (var inter in interfaces)
{ {
Console.WriteLine();
Console.WriteLine(inter.Name);
Console.WriteLine(type.Name);
if (inter.Name.Contains("IRepo")) if (inter.Name.Contains("IRepo"))
{ {
//注册数据仓库 //注册数据仓库
@ -112,23 +136,25 @@ public static class Program
{ {
//注册服务 //注册服务
services.AddScoped(inter, type); services.AddScoped(inter, type);
Console.WriteLine(inter);
Console.WriteLine(type);
} }
if (inter.Name.Contains("IController")) if (inter.Name.Contains("IController"))
{ {
isUseController = true; isUseController = true;
} }
if (inter.Name.Equals("IDataModel"))
{
}
} }
} }
} }
Console.Write("done.\r\n");
if (isUseController) if (isUseController)
{ {
Console.WriteLine($"\tAdd Controllers for {assembly.FullName}"); Log.Logger.Information($"\tAdd Controllers for {assembly.FullName}");
// 添加Controller // 添加Controller
services.AddControllers().AddApplicationPart(assembly); services.AddControllers().AddApplicationPart(assembly);
} }
Log.Logger.Information("done.\r\n");
} }
} }
catch (System.Exception ex) catch (System.Exception ex)

25
LFlow/Utils/CodeFirst.cs Normal file
View File

@ -0,0 +1,25 @@
using System;
using System.Net;
namespace LFlow.Base.Utils;
public static class CodeFirst
{
private static List<Type> _tableTypes = new List<Type>();
internal static void RegisterTable(Type type)
{
if (_tableTypes.Contains(type))
{
return;
}
else
{
_tableTypes.Add(type);
}
}
internal static void DBSeed()
{
}
}

View File

@ -11,20 +11,12 @@ public class Mapper
/// <typeparam name="T"></typeparam> /// <typeparam name="T"></typeparam>
/// <param name="source"></param> /// <param name="source"></param>
/// <returns></returns> /// <returns></returns>
public static T Map<T>(object source) public static T? Map<T>(object source)
{ {
// 使用 Mapster 转换 if (source == null)
// Check if the type is immutable
if (typeof(T).IsValueType || typeof(T).IsPrimitive || typeof(T) == typeof(string))
{ {
var dest = default(T); return default;
// Use MapWith for immutable types
return source.Adapt(dest);
}
else
{
// Use Adapt for other types
return source.Adapt<T>();
} }
return source.Adapt<T>();
} }
} }

18
LFlow/appsettings.json Normal file
View File

@ -0,0 +1,18 @@
{
"Serilog": {
"MinimumLevel": {
"Default": "Debug",
"Override": {
"Microsoft.AspNetCore.Mvc": "Debug",
"Microsoft.AspNetCore.Routing": "Debug",
"Microsoft.AspNetCore.Hosting": "Debug"
}
},
"WriteTo": [
{
"Name": "Console"
}
]
},
"AllowedHosts": "*"
}