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. namespace Quadarax.Foundation.Core.Business
  9. {
  10. public abstract class AbstractService : IService
  11. {
  12. #region *** Properties ***
  13. protected ILogger<AbstractService> Log { get; }
  14. protected IContext CurrentContext { get; }
  15. protected ILoggerFactory LoggerFactory { get; }
  16. public ServiceStateEnum State { get; protected set; }
  17. #endregion
  18. #region *** Constructors ***
  19. protected AbstractService(IContext currentContext, ILoggerFactory logger)
  20. {
  21. State = ServiceStateEnum.Initializing;
  22. CurrentContext = currentContext ?? throw new ArgumentNullException(nameof(currentContext));
  23. if (logger == null)throw new ArgumentNullException(nameof(logger));
  24. Log = logger.CreateLogger<AbstractService>();
  25. LoggerFactory = logger;
  26. //if (currentContext == null)
  27. //{
  28. // var genericIdentity = new GenericIdentity("genericIdentity");
  29. // currentContext = new GenericPrincipal(genericIdentity, new string[] {});
  30. //}
  31. Log.LogTrace($"Service '{GetType().Name}' [Hash:{GetHashCode()}] initialized.");
  32. State = ServiceStateEnum.Online;
  33. }
  34. #endregion
  35. #region *** Private Operations ***
  36. protected TResult TryCatchBlock<TResult>(Func<TResult> fnc) where TResult : ResultDto
  37. {
  38. if (fnc == null)
  39. return null;
  40. var result = Activator.CreateInstance<TResult>();
  41. try
  42. {
  43. result = fnc();
  44. }
  45. catch (Exception e)
  46. {
  47. result.AppendException(e);
  48. }
  49. return result;
  50. }
  51. protected TContext GetContext<TContext>() where TContext : IContext
  52. {
  53. return (TContext)CurrentContext;
  54. }
  55. #endregion
  56. #region *** Public Operations ***
  57. public IResult Test()
  58. {
  59. return new ResultPlain();
  60. }
  61. #endregion
  62. }
  63. }