BaseService.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. 
  2. using Quadarax.Foundation.Core.Business;
  3. using Quadarax.Foundation.Core.Data;
  4. using Quadarax.Foundation.Core.Data.Interface.Repository;
  5. using Quadarax.Foundation.Core.Logging;
  6. using System.Runtime.CompilerServices;
  7. using System.Security.Principal;
  8. namespace qdr.app.bundleboiler.business.Services
  9. {
  10. public abstract class BaseService<TRepository> : AbstractRepositoryService<TRepository>
  11. where TRepository : IRepository
  12. {
  13. #region *** Properties ***
  14. protected IOptions Options { get; }
  15. #endregion
  16. protected BaseService(TRepository repository, IOptions options, ILogger logger) : base(repository, Thread.CurrentPrincipal ?? new WindowsPrincipal(WindowsIdentity.GetAnonymous()), logger)
  17. {
  18. Options = options ?? throw new ArgumentNullException(nameof(options));
  19. }
  20. protected TResult Operation<TResult>(Func<TResult> operation, [CallerMemberName] string callerName = "unknown")
  21. where TResult : IResult
  22. {
  23. var result = Result.Ok;
  24. try
  25. {
  26. result = operation();
  27. }
  28. catch (Exception ex)
  29. {
  30. Log.Log(LogSeverityEnum.Fatal, $"Error during '{callerName}' operation: {ex.Message}", ex);
  31. result = new Result([new Error(ex)]);
  32. }
  33. return (TResult)result;
  34. }
  35. }
  36. public abstract class BaseService : AbstractService
  37. {
  38. #region *** Properties ***
  39. protected IOptions Options { get; }
  40. #endregion
  41. protected BaseService(IOptions options, ILogger logger) : base(Thread.CurrentPrincipal ?? new WindowsPrincipal(WindowsIdentity.GetAnonymous()), logger)
  42. {
  43. Options = options ?? throw new ArgumentNullException(nameof(options));
  44. }
  45. protected TResult Operation<TResult>(Func<TResult> operation, [CallerMemberName] string callerName = "unknown")
  46. where TResult : IResult
  47. {
  48. var result = Result.Ok;
  49. try
  50. {
  51. result = operation();
  52. }
  53. catch (Exception ex)
  54. {
  55. Log.Log(LogSeverityEnum.Fatal, $"Error during '{callerName}' operation: {ex.Message}", ex);
  56. result = new Result([new Error(ex)]);
  57. }
  58. return (TResult)result;
  59. }
  60. }
  61. }