调用接口实现数据同步demo讲解
1.demo整体架构如下
2.SynchronizeModel类库
这个类库是主要用于实体对象模子的转换,包括请求参数实体RequestModel,数据库实体DBEntity,响应数据实体ResponseModel等,
2.1 新建一个数据库实体:- /// <summary>
- /// 被测件(雷达)模块信息表
- /// </summary>
- [SqlSugar.SugarTable("TestRecordModule")]
- public class TestRecordModule:EntityBase
- {
- /// <summary>
- /// 被测件
- /// </summary>
- public long TaskTestRecordID { get; set; }
- /// <summary>
- /// 模块编号
- /// </summary>
- public string ModuleNumber { get; set; }
- /// <summary>
- /// 映射位
- /// </summary>
- public int MapPosition { get; set; }
- /// <summary>
- /// 模块状态
- /// </summary>
- public int Status { get; set; }
- /// <summary>
- /// 创建时间
- /// </summary>
- public DateTime CreatedTime { get; set; }
- /// <summary>
- /// 备注
- /// </summary>
- public string Comment { get; set; }
- /// <summary>
- /// 模块型号
- /// </summary>
- public string ModuleType { get; set; }
- /// <summary>
- /// 测试流程控制状态
- /// </summary>
- public int TestFlowControlStatus { get; set; }
- /// <summary>
- /// 批次数
- /// </summary>
- public int BatchCount { get; set; }
- /// <summary>
- /// 扫码编号
- /// </summary>
- public string ScanCodeNumber { get; set; }
- /// <summary>
- /// 用户ID
- /// </summary>
- public long UserID { get; set; }
- /// <summary>
- /// 是否同步
- /// </summary>
- public SynchronizeStatus IsSynchronize { get; set; }
- /// <summary>
- /// 产线名称
- /// </summary>
- public string ProductLineName { get; set; }
- }
复制代码 2.2 新建响应数据实体:- public class ResultModel
- {
- public bool Success { get; set; }
- public string Message { get; set; }
- }
复制代码 2.3 业务逻辑的同步状态枚举:- public enum SynchronizeStatus
- {
- None = 0,
- Synchronizing = 1,
- Synchronized = 2,
- }
复制代码 3.SynchronizeService类库
主要分几层:
3.1 接口层:IRepository,IService- public interface ISynchronizeRepository
- {
- Task<ResultModel> AddTestRecordModuleAsync(TestRecordModule entity);
- Task<ResultModel> BatchDataSynchronizeAsync(List<TestRecordModule> entities);
- }
- public interface ISynchronizeService
- {
- Task<ResultModel> AddTestRecordModuleAsync(TestRecordModule entity);
- Task<ResultModel> BatchDataSynchronizeAsync(List<TestRecordModule> entities);
- }
复制代码 3.2 业务逻辑实现层:RepositoryImpl,ServiceImpl4.WebApiService,表现层Controller
- [Route("[controller]/[action]")]
- [ApiController]
- public class SynchronizeController : ControllerBase
- {
- private readonly ISynchronizeService _synchronizeService;
- public SynchronizeController(ISynchronizeService synchronizeService )
- {
- this._synchronizeService = synchronizeService;
- }
- /// <summary>
- /// 单个数据同步
- /// </summary>
- /// <param name="entity"></param>
- /// <returns></returns>
- [HttpPost]
- public async Task<ResultModel> DataSynchronize(TestRecordModule entity)
- {
- entity.IsSynchronize = SynchronizeStatus.Synchronizing;
- var result = this._synchronizeService.AddTestRecordModuleAsync(entity);
- return await result;
- }
- /// <summary>
- /// 批次数据同步
- /// </summary>
- /// <param name="entity"></param>
- /// <returns></returns>
- [HttpPost]
- public async Task<ResultModel> BatchDataSynchronize(List<TestRecordModule> entities)
- {
- var result = this._synchronizeService.BatchDataSynchronizeAsync(entities);
- return await result;
- }
- }
复制代码 具体可以访问我的代码库:https://gitee.com/chenshibao/web-api-service.git
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |