我有一个控制器,它调用 api 来获取 Positions 和 Employees 的列表。首先,它将 api 结果放入模型类 - IDMSElements (1)。然后控制器获取 IDMSElements 对象并将其转换为 PositionSlots 对象 (2)。然后,PositionSlots 对象需要使用来自数据库的附加数据进行更新 (3)。所以在我的控制器的简化版本中,我有: (1) IDMSElements elements = getslots.CopyDocToElements(doc); (2) PositionSlots myslots = (PositionSlots)elements; (3) myslots.UpdateDetails(db);我担心 myslots.UpdateDetails(db) 因为控制器中的其他代码取决于已运行的 UpdateDetails。我希望在创建 PositionSlots 对象时默认运行 UpdateDetails。但我认为可能不应该在构造函数中进行多个数据库调用。如何在实例化 PositionSlots 对象后自动调用 UpdateDetails?非常感谢!控制器:[Authorize]public class PSListController : Controller{ private static readonly log4net.ILog _logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); private PositionControlContext db = new PositionControlContext(); private GetPositionSlots getslots = new GetPositionSlots(); ... public async Task<ActionResult> ByCostCenter(int costCenter) { string ssapiuri = getslots.BuildAPIuri($"/current/?UDAKunit={costCenter.ToString()}"); _logger.Debug($"{ssapiuri.ToString()}"); Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); HttpResponseMessage result = await getslots.GetDataFromIDMSapi(ssapiuri); stopWatch.Stop(); _logger.Debug($"Response received. Milliseconds elapsed: {stopWatch.Elapsed.TotalMilliseconds.ToString()}"); if (result.StatusCode != HttpStatusCode.OK) { _logger.Debug("Error retrieving data from API. Milliseconds elapsed: " + stopWatch.Elapsed.TotalMilliseconds.ToString()); throw new HttpException(404, "NotFound"); } stopWatch.Restart(); XDocument doc = XDocument.Load(result.Content.ReadAsStreamAsync().Result); stopWatch.Stop(); _logger.Debug($"API result loaded into XDocument. Milliseconds elapsed: {stopWatch.Elapsed.TotalMilliseconds.ToString()}\n"); _logger.Debug(doc.ToString()); }
1 回答
海绵宝宝撒
TA贡献1809条经验 获得超8个赞
您可以通过编写一个方法来实现这一点:
IDMSElement.ToPositionSlot(db)
然后按如下方式使用它:
PositionSlots myslots = elements.Select(x => x.ToPositionSlot(db))
- 1 回答
- 0 关注
- 79 浏览
添加回答
举报
0/150
提交
取消