Compare commits

..

No commits in common. "1a362319d0d17479bfcf6ec64671d4bb59b0618f" and "dad8608d63573d8a56dbb6dfddae62dce579aa50" have entirely different histories.

16 changed files with 125 additions and 333 deletions

View File

@ -6,7 +6,7 @@ using Serilog;
using Serilog.Core; using Serilog.Core;
namespace LFlow.Home.Controllers; namespace LFlow.Home.Controllers;
public class HomeController(IHomeService<HomeDto, string> service, ILogger logger) public class HomeController(IHomeService<HomeDto, string> service, ILogger logger) : BaseController
{ {
[HttpGet] [HttpGet]
public string Home(string id) public string Home(string id)

View File

@ -1,30 +0,0 @@
using System;
using System.Reflection;
using LFlow.Base.BusinessInterface;
using LFlow.Base.Interfaces;
using LFlow.Base.Utils;
using LFlow.Home.Models.DataModels;
using LFlow.Home.Models.DtoModel;
using LFlow.Home.Services;
using Microsoft.Extensions.DependencyInjection;
namespace LFlow.Home;
public class HomeModule : IModule
{
public void ConfigureModule(IServiceCollection services)
{
// 将HomeModel注册到CodeFirst将会在程序启动后自动创建表
CodeFirst.AddType(typeof(HomeModel));
var assembly = Assembly.GetAssembly(typeof(HomeService))!;
var types = assembly.GetTypes().ToList();
RegisterModule.RegisterAllService(types, services);
RegisterModule.RegisterAllRepo(types, services);
RegisterModule.RegisterAllModule(types, services);
services.AddControllers().AddApplicationPart(assembly);
Console.WriteLine("HomeModule ConfigureModule");
}
}

View File

@ -5,26 +5,11 @@ namespace LFlow.Home.Models.DtoModel;
public class HomeDto : IModel public class HomeDto : IModel
{ {
public string? Id public string? Id { get; set; }
{
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 $"HomeDto {Id} _ {ModuleName} _ {ModuleUrl} _ {ModuleVer}"; return $"HomeDto {Id}";
} }
} }

View File

@ -1,5 +1,4 @@
using System; using System;
using LFlow.Base.Default;
using LFlow.Base.Interfaces; using LFlow.Base.Interfaces;
// using LFlow.Interfaces; // using LFlow.Interfaces;
@ -8,21 +7,33 @@ using SqlSugar;
namespace LFlow.Home.Repositorys; namespace LFlow.Home.Repositorys;
public class HomeRepo(ISqlSugarClient db) : DefaultCurdRepo<HomeModel, string>(db) public class HomeRepo(ISqlSugarClient db) : IRepo<HomeModel, string>
{ {
public HomeModel Delete(string id)
{
throw new NotImplementedException();
}
/// <summary> public HomeModel Get(string id)
/// 查询 {
/// </summary> return new HomeModel
/// <param name="whereObj"></param> {
/// <returns></returns> Id = id
/// <exception cref="NotImplementedException"></exception> };
public override List<HomeModel> Search(HomeModel whereObj) => throw new NotImplementedException(); }
/// <summary>
/// 根据条件查询ID public HomeModel SaveOrUpdate(HomeModel entity, bool isUpdate)
/// </summary> {
/// <param name="whereObj"></param> throw new NotImplementedException();
/// <returns></returns> }
/// <exception cref="NotImplementedException"></exception>
public override List<string> WhereSearchId(HomeModel whereObj) => throw new NotImplementedException(); public List<HomeModel> Search(HomeModel whereObj)
{
throw new NotImplementedException();
}
public List<string> WhereSearchId(HomeModel whereObj)
{
throw new NotImplementedException();
}
} }

View File

@ -7,33 +7,31 @@ using LFlow.Home.Models.DataModels;
using LFlow.Home.Models.DtoModel; using LFlow.Home.Models.DtoModel;
using LFlow.Base.BusinessInterface; using LFlow.Base.BusinessInterface;
using Serilog; using Serilog;
using Microsoft.AspNetCore.Mvc;
namespace LFlow.Home.Services; namespace LFlow.Home.Services;
public class HomeService(IRepo<HomeModel, string> repo, ILogger logger) : BaseController, IHomeService<HomeDto?, string> public class HomeService(IRepo<HomeModel, string> repo, ILogger logger) : IHomeService<HomeDto, string>
{ {
[HttpGet] public HomeDto DeleteById(string id)
public HomeDto? DeleteById(string id)
{ {
var result = repo.Delete(id); var result = repo.Delete(id);
return Mapper.Map<HomeDto>(result); return Mapper.Map<HomeDto>(result);
} }
[HttpGet]
public HomeDto? GetById(string id) public HomeDto GetById(string id)
{ {
logger.Information($"GetById id -> {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);
} }
[HttpPost]
public HomeDto? Save(HomeDto entity) public HomeDto Save(HomeDto entity)
{ {
var result = repo.SaveOrUpdate(Mapper.Map<HomeModel>(entity), false); var result = repo.SaveOrUpdate(Mapper.Map<HomeModel>(entity), false);
return Mapper.Map<HomeDto>(result); return Mapper.Map<HomeDto>(result);
} }
[HttpGet]
public List<HomeDto>? Search(HomeDto whereObj) public List<HomeDto> Search(HomeDto whereObj)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }

View File

@ -1,25 +1,41 @@
using System; using System;
using LFlow.Base.Default;
using LFlow.Base.Interfaces; using LFlow.Base.Interfaces;
using LFlow.User.Model.DataModel; using LFlow.User.Model.DataModel;
using SqlSugar; using SqlSugar;
namespace LFlow.User.Repositorys; namespace LFlow.User.Repositorys;
public class UserRepo(ISqlSugarClient db) : DefaultCurdRepo<UserModel, string>(db) public class UserRepo(ISqlSugarClient db) : IRepo<UserModel, string>
{ {
/// <summary> public UserModel Delete(string id)
/// 查询 {
/// </summary> throw new NotImplementedException();
/// <param name="whereObj"></param> }
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception> public UserModel Get(string id)
public override List<UserModel> Search(UserModel whereObj) => throw new NotImplementedException(); {
/// <summary> return new UserModel
/// 根据条件查询ID {
/// </summary> UserID = id,
/// <param name="whereObj"></param> UserName = "Test User",
/// <returns></returns> UserNickName = "Tester",
/// <exception cref="NotImplementedException"></exception> EmailAddress = "Test@Ling.chat",
public override List<string> WhereSearchId(UserModel whereObj) => throw new NotImplementedException();
};
}
public UserModel SaveOrUpdate(UserModel entity, bool isUpdate)
{
throw new NotImplementedException();
}
public List<UserModel> Search(UserModel whereObj)
{
throw new NotImplementedException();
}
public List<string> WhereSearchId(UserModel whereObj)
{
throw new NotImplementedException();
}
} }

View File

@ -7,9 +7,9 @@ namespace LFlow.Base;
public class App public class App
{ {
public static IServiceCollection? services; public static IServiceCollection? services;
private static IServiceProvider? ServiceProvider => services?.BuildServiceProvider(); private static IServiceProvider? serviceProvider => services?.BuildServiceProvider();
public static T? GetService<T>() public static T? GetService<T>()
{ {
return ServiceProvider!.GetService<T>(); return serviceProvider.GetService<T>();
} }
} }

View File

@ -1,40 +0,0 @@
using System;
using System.Reflection;
using LFlow.Base.Interfaces;
namespace LFlow.Base.Default;
/// <summary>
/// 默认的CURD操作
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="K"></typeparam>
public abstract class DefaultCurd<T, K> where T : IModel, IDataModel
{
private readonly IRepo<T, K> _repo;
public DefaultCurd(IRepo<T, K> repo)
{
_repo = repo;
}
public virtual T Add(T model)
{
return _repo.SaveOrUpdate(model, false);
}
public virtual int Delete(K id)
{
return _repo.Delete(id);
}
public virtual T Update(T model)
{
return _repo.SaveOrUpdate(model, true);
}
public virtual T Get(K id)
{
return _repo.Get(id);
}
public virtual List<T> GetAll(int pageIndex, int pageSize, ref int pageTotal)
{
return _repo.GetAll(pageIndex, pageSize, ref pageTotal);
}
public abstract List<T> GetWhere(T WhereObject);
}

View File

@ -1,50 +0,0 @@
using System;
using LFlow.Base.Interfaces;
using SqlSugar;
namespace LFlow.Base.Default;
public abstract class DefaultCurdRepo<T, K> : IRepo<T, K> where T : class, IDataModel, new()
{
private readonly ISqlSugarClient _client;
public DefaultCurdRepo(ISqlSugarClient client)
{
_client = client;
}
public virtual int Delete(K id)
{
if (Get(id) != null)
{
return _client.Deleteable<T>().In(id).ExecuteCommand();
}
else
{
throw new Exception("删除的对象不存在");
}
}
public virtual T Get(K id)
{
return _client.Queryable<T>().InSingle(id);
}
public List<T> GetAll(int pageIndex, int pageSize, ref int pageTotal)
{
return _client.Queryable<T>().ToPageList(pageIndex, pageSize, ref pageTotal);
}
public virtual T SaveOrUpdate(T entity, bool isUpdate)
{
if (isUpdate)
{
_client.Updateable(entity).ExecuteCommand();
}
else
{
_client.Insertable(entity).ExecuteCommand();
}
return entity;
}
public abstract List<T> Search(T whereObj);
public abstract List<K> WhereSearchId(T whereObj);
}

View File

@ -1,10 +0,0 @@
using System;
namespace LFlow.Base.Interfaces;
/// <summary>
/// 模块接口
/// </summary>
public interface IModule
{
void ConfigureModule(IServiceCollection services);
}

View File

@ -20,21 +20,13 @@ public interface IRepo<T, K> where T : IDataModel
/// </summary> /// </summary>
/// <param name="id"></param> /// <param name="id"></param>
/// <returns></returns> /// <returns></returns>
int Delete(K id); T Delete(K id);
/// <summary> /// <summary>
/// 保存与更新 /// 保存与更新
/// </summary> /// </summary>
/// <param name="entity"></param> /// <param name="entity"></param>
/// <returns></returns> /// <returns></returns>
T SaveOrUpdate(T entity, bool isUpdate); T SaveOrUpdate(T entity, bool isUpdate);
/// <summary>
/// 获取所有对象列表(默认分页)
/// </summary>
/// <param name="pageIndex"></param>
/// <param name="pageSize"></param>
/// <returns></returns>
List<T> GetAll(int pageIndex, int pageSize, ref int pageTotal);
/// <summary> /// <summary>
/// 根据条件搜索对象列表 /// 根据条件搜索对象列表
/// </summary> /// </summary>

Binary file not shown.

View File

@ -1,6 +1,4 @@
using System.Reflection; using System.Reflection;
using LFlow.Base.Interfaces;
using LFlow.Base.Utils;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
@ -58,13 +56,7 @@ public static class Program
}); });
}); });
builder.Host.UseSerilog((context, services, config) => builder.Host.UseSerilog();
{
config.ReadFrom.Configuration(context.Configuration)
.ReadFrom.Services(services)
.Enrich.FromLogContext();
// .WriteTo.Console();
}, true);
// init App.service // init App.service
App.services = builder.Services; App.services = builder.Services;
var app = builder.Build(); var app = builder.Build();
@ -82,13 +74,6 @@ public static class Program
u.DocumentTitle = "LFlow"; u.DocumentTitle = "LFlow";
}); });
} }
// 在启动后调用Sqlsugar进行CodeFirst
// 挂载启动后事件
app.Services.GetRequiredService<IHostApplicationLifetime>().ApplicationStarted.Register(() =>
{
Log.Logger.Information("ApplicationStarted");
CodeFirst.InitTable();
});
app.Run(); app.Run();
} }
@ -102,20 +87,18 @@ public static class Program
ConnectionString = "DataSource=LFlow-dev.db", ConnectionString = "DataSource=LFlow-dev.db",
IsAutoCloseConnection = true, IsAutoCloseConnection = true,
}, },
db => db =>
{ {
//单例参数配置,所有上下文生效 //单例参数配置,所有上下文生效
db.Aop.OnLogExecuting = (sql, pars) => db.Aop.OnLogExecuting = (sql, pars) =>
{ {
//获取作IOC作用域对象 //获取作IOC作用域对象
var appServive = s.GetService<IHttpContextAccessor>(); var appServive = s.GetService<IHttpContextAccessor>();
// var obj = appServive?.HttpContext?.RequestServices.GetService<Log>(); // var obj = appServive?.HttpContext?.RequestServices.GetService<Log>();
// Console.WriteLine("AOP" + obj.GetHashCode()); // Console.WriteLine("AOP" + obj.GetHashCode());
Console.WriteLine($"{appServive?.HttpContext?.Request.Path}:{sql}\r\n{pars}"); Console.WriteLine($"{appServive?.HttpContext?.Request.Path}:{sql}\r\n{pars}");
}; };
}); });
sqlSugar.DbMaintenance.CreateDatabase();
return sqlSugar; return sqlSugar;
}); });
} }
@ -134,27 +117,43 @@ public static class Program
Log.Logger.Information($"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;
foreach (var type in types) foreach (var type in types)
{ {
// Console.WriteLine(type); // Console.WriteLine(type);
if (type.IsClass && !type.IsAbstract) if (type.IsClass && !type.IsAbstract)
{ {
var interfaces = type.GetInterfaces(); var interfaces = type.GetInterfaces();
if (interfaces.Contains(typeof(IModule))) foreach (var inter in interfaces)
{ {
Log.Logger.Information($"\tFound IModule -> {type.FullName}"); if (inter.Name.Contains("IRepo"))
var module = Activator.CreateInstance(type) as IModule; {
module.ConfigureModule(services); //注册数据仓库
services.AddScoped(inter, type);
}
if (inter.Name.Contains(type.Name))
{
//注册服务
services.AddScoped(inter, type);
}
if (inter.Name.Contains("IController"))
{
isUseController = true;
}
if (inter.Name.Equals("IDataModel"))
{
}
} }
} }
} }
// if (isUseController) if (isUseController)
// { {
// Log.Logger.Information($"\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"); Log.Logger.Information("done.\r\n");
} }
} }
@ -165,6 +164,4 @@ public static class Program
} }
} }
} }

View File

@ -1,32 +1,25 @@
using System; using System;
using System.Collections.Concurrent;
using System.Net; using System.Net;
using Serilog;
using SqlSugar;
namespace LFlow.Base.Utils; namespace LFlow.Base.Utils;
public static class CodeFirst public static class CodeFirst
{ {
// 线程安全的类型列表 private static List<Type> _tableTypes = new List<Type>();
private static readonly ConcurrentBag<Type> _types = new();
public static void AddType(Type type) internal static void RegisterTable(Type type)
{ {
_types.Add(type); if (_tableTypes.Contains(type))
}
internal static void InitTable()
{
var logger = App.GetService<Serilog.ILogger>()!;
var db = App.GetService<ISqlSugarClient>()!;
foreach (var type in _types)
{ {
db.CodeFirst.InitTables(type); return;
logger.Information($"Init table {type.Name} success"); }
else
{
_tableTypes.Add(type);
} }
} }
internal static void DBSeed()
internal static void InitDBSeed()
{ {
//TODO: Seed data
} }
} }

View File

@ -1,67 +0,0 @@
using System;
using LFlow.Base.Interfaces;
namespace LFlow.Base.Utils;
public static class RegisterModule
{
public static void RegisterAllModule(this List<Type> types, IServiceCollection services)
{
foreach (var type in types)
{
if (type.IsAbstract || !type.IsClass)
{
continue;
}
var interfaces = type.GetInterfaces();
foreach (var inter in interfaces)
{
if (inter.Name.Equals("IDataModel"))
{
services.AddScoped(inter, type);
}
}
}
}
public static void RegisterAllService(this List<Type> types, IServiceCollection services)
{
foreach (var type in types)
{
if (type.IsAbstract || !type.IsClass)
{
continue;
}
var interfaces = type.GetInterfaces();
foreach (var inter in interfaces)
{
if (inter.Name.Contains(type.Name) && inter.Name.StartsWith('I') && inter.Name.Contains("Service"))
{
//注册服务
services.AddScoped(inter, type);
}
}
}
}
public static void RegisterAllRepo(this List<Type> types, IServiceCollection services)
{
foreach (var type in types)
{
if (type.IsAbstract || !type.IsClass)
{
continue;
}
var interfaces = type.GetInterfaces();
foreach (var inter in interfaces)
{
if (inter.Name.Contains("IRepo"))
{
//注册数据仓库
services.AddScoped(inter, type);
}
}
}
}
}

View File

@ -14,8 +14,5 @@
} }
] ]
}, },
"AllowedHosts": "*", "AllowedHosts": "*"
"ConnectionStrings": {
"DefaultConnection": "Data Source=192.168.3.85;Initial Catalog=PluginAdmin-dev;User Id=sa;Password=Sa1234;Encrypt=True;TrustServerCertificate=True"
}
} }