AbstractService.cs 1.8 KB

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