素材巴巴 > 程序开发 >

使用.net core ABP和Angular模板构建博客管理系统(创建后端服务)

程序开发 2023-09-21 13:28:20

如何创建.net core ABP和Angular模板可以参考我的这篇文章:http://blog.csdn.net/yiershan1314/article/details/78219322

创建实体

如下所示项目下创建blog/notes文件夹,并加入我们的实体文件

这里写图片描述

/// /// 文章信息/// public class Note : Entity, IHasCreationTime, IHasDeletionTime, IHasModificationTime, ICreationAudited{/// /// 标题/// public string Title { get; set; }/// /// 内容/// public string Content { get; set; }/// /// 创建时间/// public DateTime CreationTime { get; set; }/// /// 删除时间/// public DateTime? DeletionTime { get; set; }/// /// 是否删除/// public bool IsDeleted { get; set; }/// /// 上次修改时间/// public DateTime? LastModificationTime { get; set; }/// /// 创建人/// public long? CreatorUserId { get; set; }/// /// 点赞次数/// public long Like { get; set; }/// /// 收藏次数/// public long Collect { get; set; }/// /// 浏览次数/// public long Scan { get; set; }/// /// 内容的数据类型 markdown内容,html内容,或者其他/// public int TextType { get; set; }/// /// 简单描述,用于微信推送时的描述或者其他/// public string Des { get; set; }/// /// 封面图片,可用于微信推送时或者其他/// public string Img { get; set; }/// /// 关键字,可用于搜索,分类等/// public string Tags { get; set; }/// /// 是否发布/// public bool IsPublic { get; set; }}
/// /// 文章专辑/// public class NoteBook : Entity, IHasCreationTime, IHasDeletionTime, IHasModificationTime, ICreationAudited{/// /// 专辑名称/// public string Name { get; set; }/// /// 专辑描述/// public string Des { get; set; }/// /// 专辑封面/// public string Img { get; set; }/// /// 专辑创建时间/// public DateTime CreationTime { get; set; }/// /// 专辑删除时间/// public DateTime? DeletionTime { get; set; }/// /// 专辑是否删除/// public bool IsDeleted { get; set; }/// /// 专辑最后修改时间/// public DateTime? LastModificationTime { get; set; }/// /// 专辑创建人/// public long? CreatorUserId { get; set; }}
/// /// 专辑和文章对应关系/// public class NoteToNoteBook : Entity, IHasCreationTime, ICreationAudited{/// /// 文章的id/// public int NoteId { get; set; }/// /// 文章内容/// public Note Note { get; set; }/// /// 专辑id/// public int NoteBookId { get; set; }/// /// 专辑内容/// public NoteBook NoteBook { get; set; }/// /// 创建时间/// public DateTime CreationTime { get; set; }/// /// 创建人/// public long? CreatorUserId { get; set; }}

创建DbContext

提到DbContext,对于经常使用DbFirst模式的开发者来说已经再熟悉不过了,EntityFramework全靠这员大将。它的作用是代表与数据库连接的会话,提供了查询、状态跟踪、保存等功能。
还有一个重要的对象是DbSet,对实体类型提供了集合操作,比如Add、Attach、Remove。继承了DbQuery,所以可以提供查询功能。
ABP框架为我们创建了一个DbContext模板,如下图:

这里写图片描述

添加数据集如下:

public class MZCDbContext : AbpZeroDbContext{/* Define an IDbSet for each entity of the application */public MZCDbContext(DbContextOptions options): base(options){}public DbSet Notes { get; set; }public DbSet NoteBooks { get; set; }public DbSet NoteToNoteBooks { get; set; }}

创建数据库迁移

现在我们通过创建的实体类和DbContext类利用EF的Code First数据库迁移来创建数据库。ABP模板默认开启了迁移。
多的就不说了,执行add-migration notes 和 update-database命令如下:

这里写图片描述

查看我们的数据库表添加成功:
这里写图片描述

构建应用层服务

在DDD(领域驱动设计)设计中,仓储实现了对数据进行特定操作的代码。ABP使用泛型IRepository接口为每一个实体创建了一个自动的仓储。IRepository定义了select,insert,update和一些更多的通用方法:

这里写图片描述

ABP框架已经内置了这么多常用的操作方法,当然,我们也可以根据自己的需求扩展这些仓储。这里我就直接用自动仓储,不再创建仓储。

添加如下文件:

这里写图片描述

初步定义dto内容如下:

/// /// 创建的时候不需要太多信息,内容更新主要依靠update/// 在用户点击创建的时候数据库便创建数据,在用户编辑过程中自动更新保存数据。/// public class CreateNoteDto{/// /// 创建时间/// public DateTime CreationTime { get; set; }/// /// 创建人/// public long CreatorUserId { get; set; }/// /// 内容的数据类型 markdown内容,html内容,或者其他/// public int TextType { get; set; }}/// /// 自动更新所传的数据/// public class UpdateNoteDto : EntityDto{/// /// 标题/// public string Title { get; set; }/// /// 内容/// public string Content { get; set; }/// /// 上次修改时间/// public DateTime? LastModificationTime { get; set; }}/// /// 发布更新时所用/// public class PublicNoteDto: UpdateNoteDto{/// /// 简单描述,用于微信推送时的描述或者其他/// public string Des { get; set; }/// /// 封面图片,可用于微信推送时或者其他/// public string Img { get; set; }/// /// 关键字,可用于搜索,分类等/// public string Tags { get; set; }/// /// 是否发布/// public bool IsPublic { get; set; }}/// /// 用于列表展示/// public class NoteDto : EntityDto{/// /// 标题/// public string Title { get; set; }/// /// 创建时间/// public DateTime CreationTime { get; set; }/// /// 点赞次数/// public long Like { get; set; }/// /// 收藏次数/// public long Collect { get; set; }/// /// 浏览次数/// public long Scan { get; set; }/// /// 是否发布/// public string IsPublic { get; set; }}

初步定义接口如下:

public interface INoteAppServer: IAsyncCrudAppService{}

IAsyncCrudAppService继承了IApplicationService接口,后面会讲到,继承了这个接口的service会自动封装成webapi。当然我们可以根据需要来选择继承或者不继承apb提供的接口。其中也定义了如下接口函数:

        Task Create(TCreateInput input);Task Delete(TDeleteInput input);Task Get(TGetInput input);Task> GetAll(TGetAllInput input);Task Update(TUpdateInput input);

简单实现接口

    public class NoteAppServer : AsyncCrudAppService, INoteAppServer{public NoteAppServer(IRepository repository): base(repository){}}

可以看出来,我们什么不写也可以,因为继承了AsyncCrudAppService类,这里就默认有以下实现。但是用起来怎么样,可以试试。

        [AsyncStateMachine(typeof(AsyncCrudAppService<,,,,,,,>.d__7))]public virtual Task Create(TCreateInput input);public virtual Task Delete(TDeleteInput input);[AsyncStateMachine(typeof(AsyncCrudAppService<,,,,,,,>.d__5))]public virtual Task Get(TGetInput input);[AsyncStateMachine(typeof(AsyncCrudAppService<,,,,,,,>.d__6))]public virtual Task> GetAll(TGetAllInput input);[AsyncStateMachine(typeof(AsyncCrudAppService<,,,,,,,>.d__8))]public virtual Task Update(TUpdateInput input);protected virtual Task GetEntityByIdAsync(TPrimaryKey id);

这里写图片描述

这仅仅只是开始,更多的还在后面。
关于AsyncCrudAppService更多属性和方法可以参考官方api:
https://aspnetboilerplate.com/api-docs/html/T_Abp_Application_Services_AsyncCrudAppService_6.htm


标签:

素材巴巴 Copyright © 2013-2021 http://www.sucaibaba.com/. Some Rights Reserved. 备案号:备案中。