- 控制器类需要继承?ControllerBase 基类
- 需要添加 [ApiController]?特性以及 [Route]?特性
- Route(路由) 特性参数规则,一般写法是?[Route("api/[controller]/[action]")] 。也就是路由访问路径:前缀api/当前控制器/具体的方法
- 为了业务处理逻辑和控制器之间实现解耦。可以通过设计:1.通用接口状态返回值类,2.共用(CURD)基础接口类,3.以及各自业务逻辑处理服务类。
- 通用接口状态返回值类提供给共用基础接口类使用。共用基础接口类提供给不同的业务处理服务类使用。然后不同服务类提供给不同控制器进行访问使用。这样就能实现服务与控制器的之间解耦。
- ApiResponse 类的构造函数用来构造返回,成功或失败的结果
public class ApiResponse
{
/// <summary>
/// 失败
/// </summary>
/// <param name="message"></param>
/// <param name="status"></param>
public ApiResponse(string message, bool status = false)
{
this.Message = message;
this.Status = status;
}
/// <summary>
/// 成功
/// </summary>
/// <param name="status"></param>
/// <param name="result"></param>
public ApiResponse(bool status,object result)
{
this.Status = status;
this.Result = result;
}
/// <summary>
/// 返回消息
/// </summary>
public string Message { get; set; }
/// <summary>
/// 返回状态
/// </summary>
public bool Status { get; set; }
/// <summary>
/// 结果
/// </summary>
public object Result { get; set; }
}
- IBaseService 基类,设计传入一个泛型 T。
- 设计的是异步实现,定义的方法需按照规范,后缀添加Async
public interface IBaseService<T>
{
/// <summary>
/// 获取全部数据
/// </summary>
/// <returns></returns>
Task<ApiResponse> GetAllAsync();
/// <summary>
/// 根据id,获取单条数据
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
Task<ApiResponse> GetSingleAsync(int id);
/// <summary>
/// 添加数据,传入T泛型实体
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
Task<ApiResponse> AddAsync(T model);
/// <summary>
/// 更新
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
Task<ApiResponse> UpdateAsync(T model);
/// <summary>
/// 删除,根据id进行删除
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
Task<ApiResponse> DeleteAsync(int id);
}
- 当前待办事项接口服务类(IToDoService),继承了IBaseService 基类后,需要传入泛型实体(待办事项实体)?ToDo
- 如果是忘备录接口服务类,就需要传入?Memo 实体了,也就是不同的实现,需传入不同的泛型实体,实现不同的逻辑。
public interface IToDoService:IBaseService<ToDo>
{
}
/// <summary>
/// 待办事项的实现
/// </summary>
public class ToDoService : IToDoService
{
private readonly IUnitOfWork work;
public ToDoService(IUnitOfWork work)
{
this.work = work;
}
public async Task<ApiResponse> AddAsync(ToDo model)
{
try
{
await work.GetRepository<ToDo>().InsertAsync(model);
if (await work.SaveChangesAsync() > 0) //保存成功
{
return new ApiResponse(true, model); //返回true,并把添加的实体返回
}
return new ApiResponse("添加数据失败");
}
catch (Exception ex)
{
return new ApiResponse(ex.Message);
}
}
public async Task<ApiResponse> DeleteAsync(int id)
{
try
{
var repository= work.GetRepository<ToDo>();//获取仓储
//删除之前,先进行查询
var todo = await repository.GetFirstOrDefaultAsync(predicate:x=>x.Id.Equals(id));
repository.Delete(todo);
if (await work.SaveChangesAsync() > 0) //删除成功
{
return new ApiResponse(true, "删除成功");
}
return new ApiResponse("删除数据失败");
}
catch (Exception ex)
{
return new ApiResponse(ex.Message);
}
}
public async Task<ApiResponse> GetAllAsync()
{
try
{
var todos= await work.GetRepository<ToDo>().GetAllAsync();
return new ApiResponse(true, todos); //返回true,并返回所有数据
}
catch (Exception ex)
{
return new ApiResponse(ex.Message);
}
}
public async Task<ApiResponse> GetSingleAsync(int id)
{
try
{
var todo= await work.GetRepository<ToDo>().GetFirstOrDefaultAsync(predicate: x => x.Id.Equals(id));
return new ApiResponse(true, todo); //把找到的数据返回
}
catch (Exception ex)
{
return new ApiResponse(ex.Message);
}
}
public async Task<ApiResponse> UpdateAsync(ToDo model)
{
try
{
var repository = work.GetRepository<ToDo>();//获取仓储
//更新之前,先拿到要更新的数据
var todo = await repository.GetFirstOrDefaultAsync(predicate: x => x.Id.Equals(model.Id));
todo.Title = model.Title;
todo.Content = model.Content;
todo.Status = model.Status;
todo.UpdateDate = DateTime.Now;
repository.Update(todo);
if (await work.SaveChangesAsync() > 0) //更新成功
{
return new ApiResponse(true, "更新成功");
}
return new ApiResponse("更新数据失败");
}
catch (Exception ex)
{
return new ApiResponse(ex.Message);
}
}
}
AddTransient 是生命周期函数,还有AddScoped,AddSingleton 等三种生命周期。
builder.Services.AddTransient<IToDoService,ToDoService>();
[FromBody] 是用于从Http请求的正文中 提取数据的属性。当在接口方法的参数前加上该标记时,web api 框架就会从http请求的正文中,提取请求的json数据并将其绑定到对应的参数上面。
[ApiController]
[Route("api/[controller]/[action]")]
public class ToDoController:ControllerBase
{
private readonly IToDoService service;
public ToDoController(IToDoService service)
{
this.service = service;
}
[HttpGet]
public async Task<ApiResponse> Get(int id)=> await service.GetSingleAsync(id);
[HttpGet]
public async Task<ApiResponse> GetAll() => await service.GetAllAsync();
[HttpPost]
public async Task<ApiResponse> Add([FromBody]ToDo model) => await service.AddAsync(model);
[HttpPost]
public async Task<ApiResponse> Update([FromBody] ToDo model) => await service.UpdateAsync(model);
[HttpDelete]
public async Task<ApiResponse> Delete(int id) => await service.DeleteAsync(id);
}