ABP教程(三)- 开始一个简单的任务管理系统 – 后端编码
上一篇 我们介绍了什么是ABP,这一篇我们通过原作者的”简单任务系统”例子,演示如何运用ABP开发项目
创建实体
一般来说任务是需要分配给人来做的,所以我们创建两个实体模型类:Task和Persion
using Abp.Domain.Entities;
using Abp.Domain.Entities.Auditing;
using System.ComponentModel.DataAnnotations.Schema;
using System;
namespace SimpleTaskSystem
{
public class Task : Entity, IHasCreationTime
{
[ForeignKey("AssignedPersonId")]
public Person AssignedPerson { get; set; }
public long? AssignedPersonId { get; set; }
public string Description { get; set; }
public TaskState State { get; set; }
public DateTime CreationTime { get; set; }
public Task()
{
State = TaskState.Active;
}
}
}
using System;
using Abp.Domain.Entities;
using Abp.Domain.Entities.Auditing;
namespace SimpleTaskSystem
{
public class Person : Entity, IHasCreationTime
{
public string Name { get; set; }
public DateTime CreationTime { get; set; }
}
}
创建DbContext
使用EntityFramework需要先定义DbContext类,ABP的模板已经创建了DbContext文件,我们只需要把Task和Person类添加到IDbSet

using Abp.EntityFramework;
using System.Data.Entity;
namespace SimpleTaskSystem.EntityFramework
{
public class SimpleTaskSystemDbContext : AbpDbContext
{
//TODO: Define an IDbSet for each Entity...
//Example:
//public virtual IDbSet Users { get; set; }
public virtual IDbSet Tasks { get; set; }
public virtual IDbSet People { get; set; }
/* NOTE:
* Setting "Default" to base class helps us when working migration commands on Package Manager Console.
* But it may cause problems when working Migrate.exe of EF. If you will apply migrations on command line, do not
* pass connection string name to base classes. ABP works either way.
*/
public SimpleTaskSystemDbContext()
: base("Default")
{
}
/* NOTE:
* This constructor is used by ABP to pass connection string defined in SimpleTaskSystemDataModule.PreInitialize.
* Notice that, actually you will not directly create an instance of SimpleTaskSystemDbContext since ABP automatically handles it.
*/
public SimpleTaskSystemDbContext(string nameOrConnectionString)
: base(nameOrConnectionString)
{
}
}
}
通过Database Migrations创建数据库
我们使用EntityFramework的Code First模式创建数据库架构。ABP模板生成的项目已经默认开启了数据迁移功能,我们添加一些默认数据进去。

using System.Data.Entity.Migrations;
namespace SimpleTaskSystem.Migrations
{
internal sealed class Configuration : DbMigrationsConfiguration
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
ContextKey = "SimpleTaskSystem";
}
protected override void Seed(SimpleTaskSystem.EntityFramework.SimpleTaskSystemDbContext context)
{
// This method will be called every time after migrating to the latest version.
// You can add any seed data here...
context.People.AddOrUpdate(
p => p.Name,
new Person { Name = "张三" },
new Person { Name = "王二" },
new Person { Name = "李四" },
new Person { Name = "老王" }
);
}
}
}
然后打开 程序包管理器控制台 ,选择默认项目并执行命令”Add-Migration InitialCreate”,此时会在Migrations目录下生成一个以 当前时间_InitialCreate.cs 的文件。

然后继续在 程序包管理器控制台 执行 Update-Database,等执行完毕则会在数据库自动创建相应的数据表

定义仓储接口
通过仓储模式,可以更好把业务代码与数据库操作代码更好的分离。我们把仓储的代码写到 Core 项目中
using Abp.Domain.Repositories;
using System.Collections.Generic;
namespace SimpleTaskSystem.Repositorys
{
public interface ITaskRepository : IRepository<Task, long>
{
List GetAllWithPeople(long? assignedPersonId, TaskState? state);
}
}
我们可以为Persion类定义一个仓储,也可以不定义,这里我选择定义,虽然ABP默认提供的仓储在本例中已经够用了,但考虑到以后扩展方便,我们还是也给它定义一个。
using Abp.Domain.Repositories;
namespace SimpleTaskSystem.Repositorys
{
public interface IPersionRepository : IRepository<Person, long>
{
}
}
这样后期我们要加一个自定义方法直接加在这里面就行了,不用改动太多。这里也介绍一下ABP自带的仓储方法,我们可以直接使用。

实现仓储类
因为本例用的是EntityFramework,所以我们将在EntityFramework项目中实现上面定义的ITaskRepository仓储接口。如果后期想换个ORM框架,比如NHibernate,我们就只要更换仓储实现的这个项目即可。
TaskRepository.cs
using System.Collections.Generic;
using Abp.EntityFramework;
using SimpleTaskSystem.EntityFramework.Repositories;
using SimpleTaskSystem.Repositorys;
using System.Linq;
using System.Data.Entity;
namespace SimpleTaskSystem.EntityFramework.Repositorys
{
public class TaskRepository : SimpleTaskSystemRepositoryBase<Task, long>, ITaskRepository
{
public TaskRepository(IDbContextProvider dbContextProvider) : base(dbContextProvider)
{
}
public List GetAllWithPeople(long? assignedPersonId, TaskState? state)
{
//在仓储方法中,不用处理数据库连接、DbContext和数据事务,ABP框架会自动处理。
var query = GetAll(); //返回一个 IQueryable接口类型
//添加一些Where条件
if (assignedPersonId.HasValue)
{
query = query.Where(task => task.AssignedPerson.Id == assignedPersonId.Value);
}
if (state.HasValue)
{
query = query.Where(task => task.State == state);
}
return query
.OrderByDescending(task => task.CreationTime)
.Include(task => task.AssignedPerson)
.ToList();
}
}
}
PersionRepository.cs
using SimpleTaskSystem.Repositorys;
using Abp.EntityFramework;
namespace SimpleTaskSystem.EntityFramework.Repositories
{
public class PersionRepository : SimpleTaskSystemRepositoryBase<Person, long>, IPersionRepository
{
public PersionRepository(IDbContextProvider dbContextProvider) : base(dbContextProvider)
{
}
}
}
创建服务(Services)
首先在Application项目中定义Task的应用服务层的接口,各dto的类请下载源码查看。
ITaskAppService.cs
using Abp.Application.Services;
namespace SimpleTaskSystem.Services
{
public interface ITaskAppService : IApplicationService
{
GetTasksOutput GetTasks(GetTasksInput input);
void UpdateTask(UpdateTaskInput input);
void CreateTask(CreateTaskInput input);
}
}
然后,我们写TaskAppService类来实现ITaskAppService接口
TaskAppService.cs
using Abp.Application.Services;
using AutoMapper;
using SimpleTaskSystem.Repositorys;
using System.Collections.Generic;
namespace SimpleTaskSystem.Services
{
public class TaskAppService : ApplicationService, ITaskAppService
{
private readonly ITaskRepository _taskRepository;
private readonly IPersionRepository _personRepository;
///
/// 构造函数自动注入我们所需要的类或接口
///
public TaskAppService(ITaskRepository taskRepository, IPersionRepository personRepository)
{
_taskRepository = taskRepository;
_personRepository = personRepository;
}
public void CreateTask(CreateTaskInput input)
{
Logger.Info("Creating a task for input: " + input);
//通过输入参数,创建一个新的Task实体
var task = new Task { Description = input.Description };
if (input.AssignedPersonId.HasValue)
{
task.AssignedPersonId = input.AssignedPersonId.Value;
}
//调用仓储基类的Insert方法把实体保存到数据库中
_taskRepository.Insert(task);
}
public GetTasksOutput GetTasks(GetTasksInput input)
{
//调用Task仓储的特定方法GetAllWithPeople
var tasks = _taskRepository.GetAllWithPeople(input.AssignedPersonId, input.State);
//用AutoMapper自动将List转换成List
return new GetTasksOutput
{
Tasks = Mapper.Map<list>(tasks)
};
}
public void UpdateTask(UpdateTaskInput input)
{
//可以直接Logger,它在ApplicationService基类中定义的
Logger.Info("Updating a task for input: " + input);
//通过仓储基类的通用方法Get,获取指定Id的Task实体对象
var task = _taskRepository.Get(input.Id);
//修改task实体的属性值
if (input.State.HasValue)
{
task.State = input.State.Value;
}
if (input.AssignedPersonId.HasValue)
{
task.AssignedPerson = _personRepository.Load(input.AssignedPersonId.Value);
}
//我们都不需要调用Update方法
//因为应用服务层的方法默认开启了工作单元模式(Unit of Work)
//ABP框架会工作单元完成时自动保存对实体的所有更改,除非有异常抛出。有异常时会自动回滚,因为工作单元默认开启数据库事务。
}
}
}
数据验证
如果应用服务(Application Service)方法的参数对象实现了IInputDto或IValidate接口,ABP会自动进行参数有效性验证。
CreateTaskInput.cs
using Abp.Application.Services.Dto;
using System.ComponentModel.DataAnnotations;
namespace SimpleTaskSystem.Services
{
public class CreateTaskInput : IInputDto
{
[Required]
public string Description { get; set; }
public long? AssignedPersonId { get; set; }
}
}
如果你想使用自定义验证,你可以实现ICustomValidate 接口
UpdateTaskInput.cs
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Abp.Application.Services.Dto;
using Abp.Runtime.Validation;
namespace SimpleTaskSystem.Services
{
public class UpdateTaskInput : IInputDto, ICustomValidate
{
public int Id { get; set; }
public string Description { get; set; }
public long? AssignedPersonId { get; set; }
public TaskState? State { get; set; }
public void AddValidationErrors(List results)
{
if (AssignedPersonId == null && State == null)
{
results.Add(new ValidationResult("AssignedPersonId和State不能同时为空!", new[] { "AssignedPersonId", "State" }));
}
}
}
}
创建Web Api服务
ABP默认已经开户了动态API,我们不需要任何设置即可非常轻松地把Application Service的public方法发布成Web Api接口,可以供客户端通过ajax调用。
下一篇我们将在此基础上构建前台页面,实现在浏览器中对任务进行简单的增删改查,静请期待……
本节源码链接: http://pan.baidu.com/s/1jIvZPSM 密码: njk5
ABP教程(三)- 开始一个简单的任务管理系统 – 后端编码的更多相关文章
- ABP教程(四)- 开始一个简单的任务管理系统 - 实现UI端的增删改查
接上一篇:ABP教程(三)- 开始一个简单的任务管理系统 – 后端编码 1.实现UI端的增删改查 1.1添加增删改查代码 打开SimpleTaskSystem.sln解决方案,添加一个“包含视图的MV ...
- Directx11教程(6) 画一个简单的三角形(2)
原文:Directx11教程(6) 画一个简单的三角形(2) 在上篇教程中,我们实现了在D3D11中画一个简单的三角形,但是,当我们改变窗口大小时候,三角形形状却随着窗口高宽比例改变而改变, ...
- Directx11教程(5) 画一个简单的三角形(1)
原文:Directx11教程(5) 画一个简单的三角形(1) 在本篇教程中,我们将通过D3D11画一个简单的三角形.在D3D11中,GPU的渲染主要通过shader来操作(当然还有一些操作 ...
- 响应式编程笔记三:一个简单的HTTP服务器
# 响应式编程笔记三:一个简单的HTTP服务器 本文我们将继续前面的学习,但将更多的注意力放在用例和编写实际能用的代码上面,而非基本的APIs学习. 我们会看到Reactive是一个有用的抽象 - 对 ...
- Directx11教程(19) 画一个简单的地形
原文:Directx11教程(19) 画一个简单的地形 通常我们在xz平面定义一个二维的网格,然后y的值根据一定的函数计算得到,比如正弦.余弦函数的组合等等,可以得到一个看似不错的地形或者 ...
- PureMVC和Unity3D的UGUI制作一个简单的员工管理系统实例
前言: 1.关于PureMVC: MVC框架在很多项目当中拥有广泛的应用,很多时候做项目前人开坑开了一半就消失了,后人为了填补各种的坑就遭殃的不得了.嘛,程序猿大家都不喜欢像文案策划一样组织文字写东西 ...
- php+js实现一个简单的用户管理系统
php + js 实现一个简单的用户管理系统 说实话,我对PHP是抵触的,但是我们的WEB课程刚好学的就是这个,不得已看了看,下面是用PHP实现的一个简单的用户管理系统. 我们首先来看一下目录结构 a ...
- 使用 ADD-ON SDK 开发 基于 Html JQuery 和 CSS 的 firefox 插件入门教程1: 创建一个简单的 Add-on
[本文转载自http://sixpoint.me/942/implementing-simple-addon/] 实现一个简单的插件 教程的这个部分带你使用 SDK 来实现, 运行并打包一个插件. 这 ...
- Linux内核分析 笔记三 构造一个简单的Linux系统MenuOS ——by王玥
一.知识点总结 (一)Linux源代码简介 arch/x86目录下的代码是我们重点关注的 内核启动相关代码都在init目录下 start_kernel函数相当于普通C程序的main函数 linux的核 ...
随机推荐
- 在EasyUI的DataGrid中嵌入Combobox
在做项目时,须要在EasyUI的DataGrid中嵌入Combobox,花了好几天功夫,在大家的帮助下,最终看到了它的庐山真面: 核心代码例如以下: <html> <head> ...
- 解决echart在IE中使用时,在div中加入postion后图表不显示问题
<!-- 为ECharts准备一个具备大小(宽高)的Dom --> <div id="main" style="height:400px;width:1 ...
- LuaInterface简单介绍
LuaInterface简单介绍 Lua是一种非常好的扩展性语言.Lua解释器被设计成一个非常easy嵌入到宿主程序的库.LuaInterface则用于实现Lua和CLR的混合编程. (一)Lua f ...
- 原始的解释器模式(Interpreter Pattern)
解释器模式的定义(现实项目中非常少遇到,因此直接理论先...) 解释器模式是一种依照规定语法进行解析的方案,在如今项目中使用较少,其定义为:给定一门语言,定义它的方法的一种表示,并定义一个解释器,该解 ...
- oracle 导出导入不含数据的空库
10g或之前,用exp导出,imp导入,带上rows=n参数 11g或以上,用expdp导出,impdp导入,带上CONTENT = METADATA_ONLY 参数 expdp带上此参数,不导出数据 ...
- aspx后台引用不到服务器控件
从其他地方拷贝的页面到自己的项目,后台CS代码引用hidden时,提示找不到,百度,发现可能是网站项目和Web应用程序的区别,右键aspx转化为Web应用程序即可.
- java8--异常处理(java疯狂讲义3复习笔记)
try,catch,finally,throw,throws java将异常分为两种,Checked异常和Runtime异常. IndexOutOfBoundsException NumberForm ...
- 正向代理tinyproxy使用总结
使用tinyproxy的问题背景: 其实以前代理一直用的是apache,后来,那次有个任务要给ios的推送设置代理,任务很紧急,可是apache报错. 原因如下:APNS发送通知的端口2195,但是A ...
- C 编程中fseek、ftell的用法总结
fseek 函数功能是将文件指针移动到指定的地方,因此可以通过fseek重置文件指针的位置.函数原型: int fseek(FILE *stream, long offset, int origi ...
- 返回模式有流式(streaming)和整体(total) 热词词表解决方案
重要术语说明_语音识别(ASR)_智能语音交互-阿里云 https://help.aliyun.com/document_detail/72238.html 返回模式(response mode) ...