AbstractService.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System;
  2. using Microsoft.Extensions.Logging;
  3. using Quadarax.Foundation.Core.Business.Enums;
  4. using Quadarax.Foundation.Core.Business.Interface;
  5. using Quadarax.Foundation.Core.Data.Interface.Domain;
  6. using Quadarax.Foundation.Core.Data.Interface.Entity;
  7. using Quadarax.Foundation.Core.Data.Interface.Entity.Dto;
  8. using Quadarax.Foundation.Core.Logging;
  9. namespace Quadarax.Foundation.Core.Business
  10. {
  11. public abstract class AbstractService : IService
  12. {
  13. #region *** Properties ***
  14. protected IQLog Log { get; }
  15. protected IContext CurrentContext { get; }
  16. protected IQLogger Logger { get; }
  17. public ServiceStateEnum State { get; protected set; }
  18. #endregion
  19. #region *** Constructors ***
  20. protected AbstractService(IContext currentContext, IQLogger logger)
  21. {
  22. State = ServiceStateEnum.Initializing;
  23. CurrentContext = currentContext ?? throw new ArgumentNullException(nameof(currentContext));
  24. if (logger == null)throw new ArgumentNullException(nameof(logger));
  25. Logger = logger;
  26. Log = logger.GetLogger(GetType());
  27. //if (currentContext == null)
  28. //{
  29. // var genericIdentity = new GenericIdentity("genericIdentity");
  30. // currentContext = new GenericPrincipal(genericIdentity, new string[] {});
  31. //}
  32. Log.Log(LogSeverityEnum.Trace, $"Service '{GetType().Name}' [Hash:{GetHashCode()}] initialized.");
  33. State = ServiceStateEnum.Online;
  34. }
  35. #endregion
  36. #region *** Private Operations ***
  37. protected TResult TryCatchBlock<TResult>(Func<TResult> fnc) where TResult : ResultDto
  38. {
  39. if (fnc == null)
  40. return null;
  41. var result = Activator.CreateInstance<TResult>();
  42. try
  43. {
  44. result = fnc();
  45. }
  46. catch (Exception e)
  47. {
  48. result.AppendException(e);
  49. }
  50. return result;
  51. }
  52. protected TContext GetContext<TContext>() where TContext : IContext
  53. {
  54. return (TContext)CurrentContext;
  55. }
  56. #endregion
  57. #region *** Public Operations ***
  58. public IResult Test()
  59. {
  60. return new ResultPlain();
  61. }
  62. #endregion
  63. }
  64. }