AbstractService.cs 2.0 KB

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