待辦事項接口增刪(CURD)改查實現(xiàn)
一.添加待辦事項控制器(ToDoController)
- 控制器類需要繼承?ControllerBase 基類
- 需要添加 [ApiController]?特性以及 [Route]?特性
- Route(路由) 特性參數(shù)規(guī)則,一般寫法是?[Route("api/[controller]/[action]")] 。也就是路由訪問路徑:前綴api/當(dāng)前控制器/具體的方法
?二.封裝服務(wù)共用類以及ToDoController 控制器邏輯實現(xiàn)
- 為了業(yè)務(wù)處理邏輯和控制器之間實現(xiàn)解耦??梢酝ㄟ^設(shè)計:1.通用接口狀態(tài)返回值類,2.共用(CURD)基礎(chǔ)接口類,3.以及各自業(yè)務(wù)邏輯處理服務(wù)類。
- 通用接口狀態(tài)返回值類提供給共用基礎(chǔ)接口類使用。共用基礎(chǔ)接口類提供給不同的業(yè)務(wù)處理服務(wù)類使用。然后不同服務(wù)類提供給不同控制器進行訪問使用。這樣就能實現(xiàn)服務(wù)與控制器的之間解耦。
1.創(chuàng)建通用接口狀態(tài)返回值(ApiResponse)類?
- ApiResponse 類的構(gòu)造函數(shù)用來構(gòu)造返回,成功或失敗的結(jié)果
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>
/// 返回狀態(tài)
/// </summary>
public bool Status { get; set; }
/// <summary>
/// 結(jié)果
/// </summary>
public object Result { get; set; }
}
2.創(chuàng)建一個共用的基礎(chǔ)接口 增刪改查(CURD) IBaseService 基類。
- IBaseService 基類,設(shè)計傳入一個泛型 T。
- 設(shè)計的是異步實現(xiàn),定義的方法需按照規(guī)范,后綴添加Async
public interface IBaseService<T>
{
/// <summary>
/// 獲取全部數(shù)據(jù)
/// </summary>
/// <returns></returns>
Task<ApiResponse> GetAllAsync();
/// <summary>
/// 根據(jù)id,獲取單條數(shù)據(jù)
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
Task<ApiResponse> GetSingleAsync(int id);
/// <summary>
/// 添加數(shù)據(jù),傳入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>
/// 刪除,根據(jù)id進行刪除
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
Task<ApiResponse> DeleteAsync(int id);
}
3.創(chuàng)建待辦事項接口服務(wù)類(IToDoService),繼承自共用的 IBaseService 接口類
- 當(dāng)前待辦事項接口服務(wù)類(IToDoService),繼承了IBaseService 基類后,需要傳入泛型實體(待辦事項實體)?ToDo
- 如果是忘備錄接口服務(wù)類,就需要傳入?Memo 實體了,也就是不同的實現(xiàn),需傳入不同的泛型實體,實現(xiàn)不同的邏輯。
public interface IToDoService:IBaseService<ToDo>
{
}
4.接著,創(chuàng)建待辦事項服務(wù)實現(xiàn)類(ToDoService),實現(xiàn)基類定義的方法邏輯。
/// <summary>
/// 待辦事項的實現(xiàn)
/// </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("添加數(shù)據(jù)失敗");
}
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("刪除數(shù)據(jù)失敗");
}
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,并返回所有數(shù)據(jù)
}
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); //把找到的數(shù)據(jù)返回
}
catch (Exception ex)
{
return new ApiResponse(ex.Message);
}
}
public async Task<ApiResponse> UpdateAsync(ToDo model)
{
try
{
var repository = work.GetRepository<ToDo>();//獲取倉儲
//更新之前,先拿到要更新的數(shù)據(jù)
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("更新數(shù)據(jù)失敗");
}
catch (Exception ex)
{
return new ApiResponse(ex.Message);
}
}
}
5.完成上述步驟后,還需要在Program.cs 中進行依賴注入。
AddTransient 是生命周期函數(shù),還有AddScoped,AddSingleton 等三種生命周期。
builder.Services.AddTransient<IToDoService,ToDoService>();
文章來源:http://www.zghlxwxcb.cn/news/detail-793159.html
6.最后在 ToDoController 控制器構(gòu)造函數(shù)中,注入?IToDoService 服務(wù)進行使用。
[FromBody] 是用于從Http請求的正文中 提取數(shù)據(jù)的屬性。當(dāng)在接口方法的參數(shù)前加上該標(biāo)記時,web api 框架就會從http請求的正文中,提取請求的json數(shù)據(jù)并將其綁定到對應(yīng)的參數(shù)上面。文章來源地址http://www.zghlxwxcb.cn/news/detail-793159.html
[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);
}
到了這里,關(guān)于Wpf 使用 Prism 實戰(zhàn)開發(fā)Day12的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!