前后端分离系统 后端搭建
技术栈
.net8 web api
AutoMapper
WebApplicationBuilder builder = WebApplication.CreateBuilder(args); builder.Services.AddAutoMapper(typeof(AutoMapConfig));
using AutoMapper; using jxc.Model; using jxc.ModelDto; namespace jxc.Api.AutoMapExtend; public class AutoMapConfig : Profile { public AutoMapConfig() { CreateMap<InventoryModel, InventoryDto>() .ForMember(c => c.ItemName,s => s.MapFrom(x=>x.ItemName)) .ForMember(c => c.Quantity,s => s.MapFrom(x=>x.Quantity)) .ReverseMap(); } }
Autofac
//指定Provider的工厂为AutofacServiceProviderFactory builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory()); builder.Host.ConfigureContainer<ContainerBuilder>(ConfigurationBinder => { //在这里就是可以注册10C中抽象和具体之间的关系 ConfigurationBinder.RegisterType<CustomLogInterceptor>(); ConfigurationBinder.RegisterType<InventoryService>() .EnableClassInterceptors();//aop 扩展 类支持 ConfigurationBinder.RegisterType<InventoryRepository>().As<IInventoryRepository>() .EnableInterfaceInterceptors();//aop 扩展 接口支持 //ConfigurationBinder.RegisterAssemblyTypes(typeof(Program).Assembly) // .Where(t => t.IsSubclassOf(typeof(ControllerBase))) // .PropertiesAutowired() // .EnableClassInterceptors(); }); builder.Services.AddControllers();
AutofacAOP
using Castle.DynamicProxy; using Microsoft.Extensions.Logging; namespace jxcApi.AutofacAOP; public class CustomLogInterceptor : IInterceptor { //支持依赖注入 private readonly ILogger<CustomLogInterceptor> _Logger; public CustomLogInterceptor(ILogger<CustomLogInterceptor> logger) { _Logger = logger; } public void Intercept(IInvocation invocation) { _Logger.LogInformation($"================={invocation.GetType().Name}{invocation.Method.Name}=执行前=================="); invocation.Proceed(); _Logger.LogInformation($"================={invocation.GetType().Name}{invocation.Method.Name}=执行完成=================="); } }
freesql
IServiceCollection services = builder.Services; Func<IServiceProvider, IFreeSql> fsqlFactory = r => { IFreeSql fsql = new FreeSql.FreeSqlBuilder() .UseConnectionString(FreeSql.DataType.Sqlite, @"Data Source=freedb2024.db") .UseMonitorCommand(cmd => Console.WriteLine($"Sql:{cmd.CommandText}")) .UseAutoSyncStructure(true) //自动同步实体结构到数据库,只有CRUD时才会生成表 .Build(); return fsql; }; services.AddSingleton<IFreeSql>(fsqlFactory);
Swagger 显示控制器层注释
builder.Services.AddSwaggerGen(option => { option.SwaggerDoc("v1", new OpenApiInfo { Title = "jxc API", Version = "V1", Description = "前后端测试框架", Contact = new OpenApiContact { Name = "GitHub源码地址", Url = new Uri("https://www.cnblogs.com/hlm750908") } }); // 获取xml文件名 var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"; // 获取xml文件路径 var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile); // 添加控制器层注释,true表示显示控制器注释 option.IncludeXmlComments(xmlPath, true); // 对action的名称进行排序,如果有多个,就可以看见效果了 option.OrderActionsBy(o => o.RelativePath); });
log4net
WebApplicationBuilder builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Logging.AddLog4Net("log4net.Config");
public interface IRepositoryBase<TEntity> where TEntity : class, new()
public class RepositoryBase<TEntity> : IRepositoryBase<TEntity> where TEntity : class, new()
public interface IUnitOfWork
public class UnitOfWork: IUnitOfWork,IDisposable

浙公网安备 33010602011771号