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
-- 默认方式
-- 含用户名密码的设置方式
启动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类
///
/// 学生
///
[Table(Register.DATABASE_PREFIX + nameof(Student))]//必须添加前缀,防止全系统中发生冲突
public class Student : EntityBase
{
///
/// 姓名
///
[MaxLength(50)]
public string Name { get; private set; }
///
/// 性别
///
public bool Sex { get; private set; }
///
/// 年龄
///
public int Age { get; private set; }
///
/// 地址
///
[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
{
///
/// 姓名
///
[MaxLength(50)]
public string Name { get; set; }
///
/// 性别
///
public bool Sex { get; set; }
///
/// 年龄
///
public int Age { get; set; }
///
/// 地址
///
[MaxLength(200)]
public string Address { get; set; }
}
public record class CreateORUpdate_StudentDto : StudentDto
{
}
public record class View_StudentDto : StudentDto
{
///
/// 标识
///
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
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
{
protected readonly ClientServiceBase
protected AppServiceBase(IServiceProvider serviceProvider, ClientServiceBase
{
_service = clientServiceBase;
}
}
///
/// 服务基础库
///
///
///
///
public abstract class AppServiceBase
{
protected AppServiceBase(IServiceProvider serviceProvider, ClientServiceBase
: base(serviceProvider, clientServiceBase)
{
}
///
/// 新增
///
///
///
[HttpPost]
public abstract Task
///
/// 删除
///
///
///
[HttpDelete]
public abstract Task
///
/// 修改
///
///
///
///
[HttpPut]
public abstract Task
///
/// 分页查询
///
///
///
[HttpGet]
public abstract Task
///
/// 单条查询
///
///
///
[HttpGet]
public abstract Task
}
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
{
}
}
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
{
public BaseClientService(IClientRepositoryBase
: 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
public int Total { get; set; }
public int PageSize { get; set; }
public Student_GetListResponse()
{
}
public Student_GetListResponse(List
{
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
public Student_GetStudentResponseList(List
{
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
{
public StudentService(ClientRepositoryBase
{
}
public async Task
{
SenparcExpressionHelper
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
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
{
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
{
public readonly StudentService studentService;
public StudentAppService(IServiceProvider serviceProvider, ClientServiceBase
{
this.studentService = studentService;
}
[ApiBind(ApiRequestMethod = ApiRequestMethod.Post)]
public override async Task
{
return await this.GetResponseAsync
{
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
{
return await this.GetResponseAsync
{
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
{
return this.GetResponseAsync
{
var ProductDtos = await studentService.GetStudentList(baseQueryDto);
return ProductDtos;
});
}
[ApiBind(ApiRequestMethod = ApiRequestMethod.Get)]
public override async Task
{
return await this.GetResponseAsync
{
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
{
return await this.GetResponseAsync
{
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
后续会陆续更新其他资料,喜欢请关注哦!