NCF(NeuCharFramework)框架的使用 当前所用框架版本0.3.1-beta3
1、官网介绍:NCF - NeuCharFramework | NCF文档
2、下载NCF框架代码:https://github.com/NeuCharFramework/NCF
3、运行NCF框架
- 用vs2022 打开下载的NCF项目NCF\src\back-end\NCF.sln文件
- 修改数据库配置文件NCF\src\back-end\Senparc.Web\App_Data\DataBase\SenparcConfig.config数据库连接,根据需求进行修改 NCF第一次拉取后默认配置为SQLServer
-- 默认方式 <SenparcConfig> <Id>3</Id> <Name>Local-SqlServer</Name> <!--本地Demo默认数据库(可在appsettings.json中修改DatabaseName配置)--> <ConnectionStringFull><![CDATA[Server=.\;Database=NCF; initial catalog=NCF;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework]]></ConnectionStringFull> <ApplicationPath><![CDATA[/]]></ApplicationPath> </SenparcConfig> -- 含用户名密码的设置方式 <SenparcConfig> <Id>3</Id> <Name>Local-SqlServer</Name> <!--本地Demo默认数据库(可在appsettings.json中修改DatabaseName配置)--> <ConnectionStringFull><![CDATA[Server=192.168.8.254,14480;Database=NCF_Dapr; User ID=sa; Password=@WSXzaq1;Application Name=fxu]]></ConnectionStringFull> <ApplicationPath><![CDATA[/]]></ApplicationPath> </SenparcConfig>
- 启动Ctrl + F5
4、安装
- 首次启动会提示安装
- 保存密码进入后台
5、创建NCF模块
- 安装XNCF Model 模板:dotnet new --install Senparc.Xncf.XncfBuilder.Template
- 进入后台模块管理安装Senparc.Xncf.XncfBuilder模块
- 开启
- 选中生成XNCF并执行
- 重新生成Senparc.Web并解决报错信息(把缺少引用的命名空间给加上)然后运行就完成了NCF模块的创建
6、实现基本的增删改查
- Senparc.Xncf.TestModular-Domain-Models-DatabaseModel创建实体Student类
/// <summary> /// 学生 /// </summary> [Table(Register.DATABASE_PREFIX + nameof(Student))]//必须添加前缀,防止全系统中发生冲突 public class Student : EntityBase<int> { /// <summary> /// 姓名 /// </summary> [MaxLength(50)] public string Name { get; private set; } /// <summary> /// 性别 /// </summary> public bool Sex { get; private set; } /// <summary> /// 年龄 /// </summary> public int Age { get; private set; } /// <summary> /// 地址 /// </summary> [MaxLength(200)] public string Address { get; private set; } public Student() { } public void BaseUpdateOrAdd(StudentDto entityDto) { Name= entityDto.Name; Age= entityDto.Age; Sex= entityDto.Sex; Address = entityDto.Address; } public void Add(StudentDto entityDto) { BaseUpdateOrAdd(entityDto); AddTime = DateTime.Now; } public void Update(StudentDto entityDto) { BaseUpdateOrAdd(entityDto); LastUpdateTime = DateTime.Now; } }
- Senparc.Xncf.TestModular-Domain-Models-DatabaseModel-Dto创建StudentDto类
public record class StudentDto { /// <summary> /// 姓名 /// </summary> [MaxLength(50)] public string Name { get; set; } /// <summary> /// 性别 /// </summary> public bool Sex { get; set; } /// <summary> /// 年龄 /// </summary> public int Age { get; set; } /// <summary> /// 地址 /// </summary> [MaxLength(200)] public string Address { get; set; } } public record class CreateORUpdate_StudentDto : StudentDto { } public record class View_StudentDto : StudentDto { /// <summary> /// 标识 /// </summary> public int Id { get; set; } public View_StudentDto() { } public View_StudentDto(Student entity) { Id = entity.Id; Name = entity.Name; Sex = entity.Sex; Age = entity.Age; Address = entity.Address; } }
- 在TestModularSenparcEntities.cs中添加 public DbSet<Student> Students { get; set; } 代码
- Senparc.Web.DatabasePlant引用 Senparc.Xncf.TestModular 模块
- 使用后台管理XNCF 模块生成器生成数据表
- 更改Xncf版本号,在Register.cs中( public override string Version => "1.1";),然后点击立即更新、开启
刷新Sqlserver 数据库表数据即可看到Student表已经添加
也可以使用命令来实现生成数据表(建议使用这种方式)
dotnet ef migrations add Add_Color -c TestModularSenparcEntities_SqlServer -s E:\Kyson3\NCF\src\back-end\Senparc.Web.DatabasePlant -o E:\Kyson3\NCF\src\back-end\Senparc.Xncf.TestModular\Domain\Migrations\Migrations.SqlServer -v
dotnet ef database update -c TestModularSenparcEntities_SqlServer -s E:\Kyson3\NCF\src\back-end\Senparc.Web.DatabasePlant
- Senparc.Xncf.TestModular模块新建AppServiceBase.cs
public abstract class AppServiceBase<TEntity> : AppServiceBase where TEntity : EntityBase { protected readonly ClientServiceBase<TEntity> _service; protected AppServiceBase(IServiceProvider serviceProvider, ClientServiceBase<TEntity> clientServiceBase) : base(serviceProvider) { _service = clientServiceBase; } } /// <summary> /// 服务基础库 /// </summary> /// <typeparam name="TEntityDto">数据传输对象Dto</typeparam> /// <typeparam name="TEntity">实体对象</typeparam> /// <typeparam name="TResponse">返回值对象</typeparam> public abstract class AppServiceBase<TEntityDto, TEntity, TResponse, TResponsePage> : AppServiceBase<TEntity> where TEntityDto : class where TEntity : EntityBase where TResponse : class { protected AppServiceBase(IServiceProvider serviceProvider, ClientServiceBase<TEntity> clientServiceBase) : base(serviceProvider, clientServiceBase) { } /// <summary> /// 新增 /// </summary> /// <param name="input"></param> /// <returns></returns> [HttpPost] public abstract Task<AppResponseBase<TResponse>> AddAsync([FromBody] TEntityDto input); /// <summary> /// 删除 /// </summary> /// <param name="id"></param> /// <returns></returns> [HttpDelete] public abstract Task<AppResponseBase<TResponse>> DelAsync([FromQuery] int id); /// <summary> /// 修改 /// </summary> /// <param name="id"></param> /// <param name="input"></param> /// <returns></returns> [HttpPut] public abstract Task<AppResponseBase<TResponse>> PutAsync([FromQuery] int id, [FromBody] TEntityDto input); /// <summary> /// 分页查询 /// </summary> /// <param name="id"></param> /// <returns></returns> [HttpGet] public abstract Task<AppResponseBase<TResponsePage>> GetPageAsync([FromQuery] BaseQueryDto baseQueryDto); /// <summary> /// 单条查询 /// </summary> /// <param name="id"></param> /// <returns></returns> [HttpGet] public abstract Task<AppResponseBase<TResponse>> GetSingleAsync([FromQuery] int id); }
- Senparc.Xncf.TestModular-Domain-Services添加BaseClientService.cs和IBaseClientService.cs
using Senparc.Ncf.Core.Models; using Senparc.Ncf.Repository; using Senparc.Ncf.Service; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Senparc.Xncf.TestModular.Domain.Services { public interface IBaseClientService<T> : IClientServiceBase<T> where T : EntityBase//global::System.Data.Objects.DataClasses.EntityObject, new() { } }
using Senparc.Ncf.Core.Models; using Senparc.Ncf.Repository; using Senparc.Ncf.Service; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Senparc.Xncf.TestModular.Domain.Services { public class BaseClientService<T> : ClientServiceBase<T>, IBaseClientService<T> where T : EntityBase//global::System.Data.Objects.DataClasses.EntityObject, new() { public BaseClientService(IClientRepositoryBase<T> repo, IServiceProvider serviceProvider) : base(repo, serviceProvider) { } } }
- Senparc.Xncf.TestModular-OHS-Local-PL添加StudentResponse.cs
using Senparc.Xncf.TestModular.Domain.Models.DatabaseModel.Dto; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Senparc.Xncf.TestModular.OHS.Local.PL { public class Student_GetListResponse { public List<View_StudentDto> StudentDtosList { get; set; } public int Total { get; set; } public int PageSize { get; set; } public Student_GetListResponse() { } public Student_GetListResponse(List<View_StudentDto> studentDtosList, int total, int pageSize) { StudentDtosList = studentDtosList; Total = total; PageSize = pageSize; } } public class Student_GetStudentResponse { public View_StudentDto StudentDto { get; set; } public Student_GetStudentResponse(View_StudentDto studentDtos) { StudentDto = studentDtos; } } public class Student_GetStudentResponseList { public List<Student_GetStudentResponse> Student_GetStudentResponses { get; set; } public Student_GetStudentResponseList(List<Student_GetStudentResponse> student_GetStudentResponses) { Student_GetStudentResponses = student_GetStudentResponses; } } }
- Senparc.Xncf.TestModular-Domain-Services添加StudentService.cs
using Senparc.Ncf.Repository; using Senparc.Ncf.Utility; using Senparc.Xncf.TestModular.Domain.Models.DatabaseModel; using Senparc.Xncf.TestModular.Domain.Models.DatabaseModel.Dto; using Senparc.Xncf.TestModular.OHS.Local.PL; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Senparc.Xncf.TestModular.Domain.Services { public class StudentService : BaseClientService<Student> { public StudentService(ClientRepositoryBase<Student> repo, IServiceProvider serviceProvider) : base(repo, serviceProvider) { } public async Task<Student_GetListResponse> GetStudentList(BaseQueryDto queryDto) { SenparcExpressionHelper<Student> helper = new SenparcExpressionHelper<Student>(); helper.ValueCompare .AndAlso(!string.IsNullOrEmpty(queryDto.Key), z => z.Name.Contains(queryDto.Key)); var Students = await this.GetObjectListAsync(queryDto.PageIndex, queryDto.PageSize, helper.BuildWhereExpression(), "Id desc"); List<View_StudentDto> view_StudentDtos = new List<View_StudentDto>(); if (Students != null) { for (int i = 0; i < Students.Count; i++) { View_StudentDto StudentDto = GetSingleStudent(Students[i]); view_StudentDtos.Add(StudentDto); } } Student_GetListResponse Student_GetListResponse = new Student_GetListResponse(view_StudentDtos, Students.TotalCount, Students.Count()); return Student_GetListResponse; } public async Task<View_StudentDto> GetSingleStudent(int id) { var entity = await this.GetObjectAsync(z => z.Id == id); View_StudentDto StudentDto = GetSingleStudent(entity); return StudentDto; } public View_StudentDto GetSingleStudent(Student Student) { View_StudentDto StudentDto = null; if (Student != null) { StudentDto = new View_StudentDto(Student); } return StudentDto; } } }
- Senparc.Xncf.TestModular-OHS-Local-AppService\添加StudentAppService.cs实现增删改查
using Microsoft.AspNetCore.Mvc; using Senparc.CO2NET.WebApi; using Senparc.CO2NET; using Senparc.Ncf.Core.AppServices; using Senparc.Ncf.Service; using Senparc.Xncf.TestModular.Domain.Models.DatabaseModel; using Senparc.Xncf.TestModular.Domain.Models.DatabaseModel.Dto; using Senparc.Xncf.TestModular.Domain.Services; using Senparc.Xncf.TestModular.OHS.Local.PL; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml.Linq; namespace Senparc.Xncf.TestModular.OHS.Local.AppService { public class StudentAppService : AppServiceBase<CreateORUpdate_StudentDto, Student, Student_GetStudentResponse, Student_GetListResponse> { public readonly StudentService studentService; public StudentAppService(IServiceProvider serviceProvider, ClientServiceBase<Student> clientServiceBase, StudentService studentService) : base(serviceProvider, clientServiceBase) { this.studentService = studentService; } [ApiBind(ApiRequestMethod = ApiRequestMethod.Post)] public override async Task<AppResponseBase<Student_GetStudentResponse>> AddAsync([FromBody] CreateORUpdate_StudentDto input) { return await this.GetResponseAsync<AppResponseBase<Student_GetStudentResponse>, Student_GetStudentResponse>(async (response, logger) => { Student student = new Student(); student.Add(input); await studentService.SaveObjectAsync(student); var entity = studentService.GetSingleStudent(student); Student_GetStudentResponse student_GetListResponse = new Student_GetStudentResponse(entity); return student_GetListResponse; }); } [ApiBind(ApiRequestMethod = ApiRequestMethod.Delete)] public override async Task<AppResponseBase<Student_GetStudentResponse>> DelAsync([FromQuery] int id) { return await this.GetResponseAsync<AppResponseBase<Student_GetStudentResponse>, Student_GetStudentResponse>(async (response, logger) => { var entity = await studentService.GetObjectAsync(z => z.Id == id); if (entity == null) { response.StateCode = 500; response.ErrorMessage = "未找到该学生!"; return response.Data; } await studentService.DeleteObjectAsync(entity); var studentDto = studentService.GetSingleStudent(entity); Student_GetStudentResponse student_GetListResponse = new Student_GetStudentResponse(studentDto); return student_GetListResponse; }); } [ApiBind(ApiRequestMethod = ApiRequestMethod.Get)] public override Task<AppResponseBase<Student_GetListResponse>> GetPageAsync([FromQuery] BaseQueryDto baseQueryDto) { return this.GetResponseAsync<AppResponseBase<Student_GetListResponse>, Student_GetListResponse>(async (response, logger) => { var ProductDtos = await studentService.GetStudentList(baseQueryDto); return ProductDtos; }); } [ApiBind(ApiRequestMethod = ApiRequestMethod.Get)] public override async Task<AppResponseBase<Student_GetStudentResponse>> GetSingleAsync([FromQuery] int id) { return await this.GetResponseAsync<AppResponseBase<Student_GetStudentResponse>, Student_GetStudentResponse>(async (response, logger) => { var entity = await studentService.GetSingleStudent(id); if (entity == null) { response.StateCode = 500; response.ErrorMessage = "未找到该学生!"; return response.Data; } Student_GetStudentResponse result = new Student_GetStudentResponse(entity); return result; }); } [ApiBind(ApiRequestMethod = ApiRequestMethod.Put)] public override async Task<AppResponseBase<Student_GetStudentResponse>> PutAsync([FromQuery] int id, [FromBody] CreateORUpdate_StudentDto input) { return await this.GetResponseAsync<AppResponseBase<Student_GetStudentResponse>, Student_GetStudentResponse>(async (response, logger) => { var entity = await studentService.GetObjectAsync(z => z.Id == id); if (entity == null) { response.StateCode = 500; response.ErrorMessage = "未找到该学生!"; return response.Data; } entity.Update(input); await studentService.SaveObjectAsync(entity); var studentDto = studentService.GetSingleStudent(entity); Student_GetStudentResponse result = new Student_GetStudentResponse(studentDto); return result; }); } } }
- 实际效果
源代码:链接:https://pan.baidu.com/s/1KTCL3eILLK2ZcfFjq8xi3w
提取码:7deq
后续会陆续更新其他资料,喜欢请关注哦!