AbstractService.cs 2.1 KB

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