天空影视,云南考试,pop字体转换器
abp(net core)+easyui+efcore仓储系统——abp总体介绍(一)
abp(net core)+easyui+efcore仓储系统——解决方案介绍(二)
abp(net core)+easyui+efcore仓储系统——领域层创建实体(三)
abp(net core)+easyui+efcore仓储系统——定义仓储并实现 (四)
在上一篇文章中学习了abp的仓储(repository)功能,repository对数据库进行增删改查操作。在这一篇文章中我们主要了解应用服务层。
一、解释下应用服务层
应用服务用于将领域(业务)逻辑暴露给展现层。展现层通过传入dto(数据传输对象)参数来调用应用服务,而应用服务通过领域对象来执行相应的业务逻辑并且将dto返回给展现层。因此,展现层和领域层将被完全隔离开来。
以下几点,在创建应用服务时需要注意:
二、定义应用服务接口需要用到的dto
1. 在visual studio 2017的“解决方案资源管理器”中,右键单击“abp.tplms.application”项目。 选择“添加” > “新建文件夹”。
2.将文件夹命名为“modules”。
3. 右键单击“modules”文件夹,选择“添加” > “新建文件夹”。将文件夹重命名为“dto”。如下图。
4. 右键单击“dto”文件夹,然后选择“添加” > “类”。 将类命名为 moduledto,然后选择“添加”。代码如下。
using abp.application.services.dto; using abp.automapper; using abp.tplms.entitys; using system; using system.collections.generic; using system.text; namespace abp.tplms.modules.dto { [automapfrom(typeof(module))] public class moduledto:entitydto<long> { public string displayname { get; set; } public string name { get; set; } public string url { get; set; } public string hotkey { get; set; } public int parentid { get; set; } public bool requiresauthentication { get; set; } public bool isautoexpand { get; set; } public string iconname { get; set; } public int status { get; set; } public string parentname { get; set; } public string requiredpermissionname { get; set; } public int sortno { get; set; } } }
dto
被用来将模块数据传递到基础设施层。dto
继承自 entitydto<long>
.跟在领域层定义的module类一样具有一些相同属性。[automapfrom(typeof(module))]
用来创建从module类到moduledto
的映射.使用这种方法。你可以将module对象自动转换成moduledto
对象(而不是手动复制所有的属性)。5. 右键单击“dto”文件夹,然后选择“添加” > “类”。 将类命名为 createupdatemoduledto ,然后选择“添加”。代码如下。
using abp.application.services.dto; using system; using system.collections.generic; using system.componentmodel.dataannotations; using system.text; namespace abp.tplms.modules.dto { public class createupdatemoduledto : entitydto<long> { public const int maxlength = 255; [required] [stringlength(maxlength)] public string displayname { get; set; } [required] [stringlength(maxlength)] public string name { get; set; } [required] [stringlength(maxlength)] public string url { get; set; } [stringlength(maxlength)] public string hotkey { get; set; } public int parentid { get; set; } public bool requiresauthentication { get; set; } public bool isautoexpand { get; set; } [stringlength(maxlength)] public string iconname { get; set; } public int status { get; set; } [required] [stringlength(maxlength)] public string parentname { get; set; } [stringlength(maxlength)] public string requiredpermissionname { get; set; } public int sortno { get; set; } } }
6. 为什么需要通过dto进行数据传输?
一般来说,使用dto进行数据传输具有以下好处。
7.dto规范 (灵活应用)
定义完dto,是不是脑袋有个疑问,我在用dto在展现层与应用服务层进行数据传输,但最终这些dto都需要转换为实体才能与数据库直接打交道啊。如果每个dto都要自己手动去转换成对应实体,这个工作量也是不可小觑啊。
聪明如你,你肯定会想肯定有什么方法来减少这个工作量。
三、使用automapper自动映射dto与实体
1.简要介绍automapper
开始之前,如果对automapper不是很了解,建议看下这篇文章。
automapper的使用步骤,简单总结下:
在abp中有两种方式创建映射规则:
2.为module实体相关的dto定义映射规则
moduledto、createupdatemoduledto中的属性名与module实体的属性命名一致,且只需要从dto映射到实体,不需要反向映射。所以通过automapto创建单向映射即可。
[automapto(typeof(module))] //定义单向映射 public class moduledto:entitydto<long> { ... } [automapto(typeof(module))] //定义单向映射 public class createupdatemoduledto : entitydto<long> { ... }
四、定义imoduleappservice接口
1. 右键单击“dto”文件夹,然后选择“添加” > “新建项”,在弹出对话框中选择“接口”。为应用服务定义一个名为 imoduleappservice
的接口。代码如下。
using abp.application.services; using abp.application.services.dto; using abp.tplms.modules.dto; using system; using system.collections.generic; using system.text; using system.threading.tasks; namespace abp.tplms.modules { public interface imoduleappservice : iapplicationservice { task createasync(createupdatemoduledto input); task updateasync(createupdatemoduledto input); task<listresultdto<moduledto>> getallasync(); task deleteasync(int id); void delete(int id); } }
从上面的代码中我们仔细看一下方法的参数及返回值,大家可能会发现并未直接使用module实体对象。这是为什么呢?因为展现层与应用服务层是通过data transfer object(dto)进行数据传输。
五、实现imoduleappservice
对于具体的业务来讲,只是简单的增删该查,实现起来就很简单了。代码如下:
using abp.application.services; using abp.application.services.dto; using abp.domain.repositories; using abp.tplms.entitys; using abp.tplms.modules.dto; using automapper; using system; using system.collections.generic; using system.text; using system.threading.tasks; namespace abp.tplms.modules { public class moduleappservice : applicationservice, imoduleappservice { private readonly irepository<module> _modulerepository; public moduleappservice(irepository<module> modulerepository) { _modulerepository = modulerepository; } public task createasync(createupdatemoduledto input) { var module = mapper.map<module>(input); return _modulerepository.insertasync(module); } public task updateasync(createupdatemoduledto input) { var module = mapper.map<module>(input); return _modulerepository.updateasync(module); } public async task<listresultdto<moduledto>> getallasync() { var books = await _modulerepository.getalllistasync(); return new listresultdto<moduledto>(objectmapper.map<list<moduledto>>(books)); } public async task deleteasync(int id) { await _modulerepository.deleteasync(id); } public void delete(int id) { _modulerepository.delete(id); } } }
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Blazor server side 自家的一些开源的, 实用型项目的进度之 CEF客户端
.NET IoC模式依赖反转(DIP)、控制反转(Ioc)、依赖注入(DI)
vue+.netcore可支持业务代码扩展的开发框架 VOL.Vue 2.0版本发布
网友评论