Guest User

Untitled

a guest
Jan 20th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.27 KB | None | 0 0
  1. public async Task UpdateStaffPlanStatus(long staffPlanId, string status)
  2. {
  3. // Update status in DB then commit
  4. await _context.SaveChangesAsync();
  5. // Doing logging in DB. Getting Error at below line.
  6. _logManager.LogStaffPlanStatusChange(staffPlanId, oldStatus, newStatus);
  7. }
  8.  
  9. services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
  10. services.AddScoped<UserContext>();
  11. services.AddScoped<IStaffPlanService, StaffPlanService>();
  12. services.AddScoped<IStaffPlanDataService, StaffPlanDataService>();
  13. services.AddScoped<IStaffPlanLogManager, StaffPlanLogManager>();
  14. services.AddScoped<StaffPlanLogger, StaffPlanLogger>(provider =>
  15. {
  16. var pnyContextGlobal = provider.GetRequiredService<PnyContext>();
  17. StaffPlanLogger logger;
  18. using (var scope = provider.CreateScope())
  19. {
  20. // Get PnyContext object in another scope, different from object in StaffPlanDataService.
  21. var pnyContext = scope.ServiceProvider.GetRequiredService<PnyContext>();
  22. var userContext = scope.ServiceProvider.GetRequiredService<Core.Extensions.UserContext>();
  23. logger = new StaffPlanLogger(pnyContext, userContext);
  24. }
  25. // StaffPlanLogger will be created with diffrent PnyContext object
  26. return logger;
  27. });
  28.  
  29. public class StaffPlanService : IStaffPlanService
  30. {
  31. readonly IStaffPlanDataService _dataService;
  32. /// <summary>
  33. /// Initializes a new instance of the <see cref="StaffPlanService"/> class.
  34. /// </summary>
  35. /// <param name="dataService">dataService</param>
  36. public StaffPlanService(IStaffPlanDataService dataService)
  37. {
  38. _dataService = dataService;
  39. }
  40. }
  41.  
  42. public class StaffPlanDataService : IStaffPlanDataService
  43. {
  44. private IStaffPlanLogManager _logManager;
  45. private readonly PnyContext _context;
  46. private readonly UserContext _userContext;
  47. /// <summary>
  48. /// Initializes a new instance of the <see cref="StaffPlanDataService"/> class.
  49. /// </summary>
  50. /// <param name="context">PnyContext</param>
  51. public StaffPlanDataService(PnyContext context, UserContext userContext, IStaffPlanLogManager logManager)
  52. {
  53. _context = context;
  54. _userContext = userContext;
  55. _logManager = logManager;
  56. }
  57. }
  58.  
  59. public class StaffPlanLogManager : IStaffPlanLogManager
  60. {
  61. private StaffPlanLogger _staffPlanLogger;
  62. readonly IAppLogger<StaffPlanLogManager> _logger;
  63.  
  64. /// <summary>
  65. /// Initializes a new instance of the <see cref="StaffPlanLogManager"/> class.
  66. /// </summary>
  67. /// <param name="context"></param>
  68. /// <param name="staffPlanLogger">staffPlanLogger</param>
  69. /// <param name="logger">logger</param>
  70. public StaffPlanLogManager(StaffPlanLogger staffPlanLogger, IAppLogger<StaffPlanLogManager> logger)
  71. {
  72. _staffPlanLogger = staffPlanLogger;
  73. _logger = logger;
  74. }
  75. }
  76.  
  77. public class StaffPlanLogger
  78. {
  79. private object lockObj = new object();
  80. private readonly PnyContext _context;
  81. private readonly UserContext _userContext;
  82. private readonly ObjectComparator<StaffPlan> comparator = new ObjectComparator<StaffPlan>();
  83. /// <summary>
  84. /// Initializes a new instance of the <see cref="StaffPlanLogger"/> class.
  85. /// </summary>
  86. /// <param name="context">PnyContext</param>
  87. public StaffPlanLogger(PnyContext context, UserContext userContext)
  88. {
  89. _context = context;
  90. _userContext = userContext;
  91. }
  92. }
  93.  
  94. public async Task UpdateStaffPlanStatus(long staffPlanId, string status)
  95. {
  96. // Update status in DB then commit
  97. await _context.SaveChangesAsync();
  98. // Doing logging in DB. Getting Error at below line if await is removed.
  99. await _logManager.LogStaffPlanStatusChange(staffPlanId, oldStatus, newStatus);
  100. }
Add Comment
Please, Sign In to add comment