DbRelationService.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using GreenTree.Nachtragsmanagement.Core.Domain.Appendix;
  9. using GreenTree.Nachtragsmanagement.Core.Domain.Deviation;
  10. using GreenTree.Nachtragsmanagement.Core.Domain.Site;
  11. using GreenTree.Nachtragsmanagement.Core.Domain.User;
  12. using GreenTree.Nachtragsmanagement.Data;
  13. using Newtonsoft.Json;
  14. using Newtonsoft.Json.Serialization;
  15. namespace GreenTree.Nachtragsmanagement.Services.Test
  16. {
  17. public class DbRelationService : IDbRelationService
  18. {
  19. private readonly IDbContext _idbContext;
  20. public DbRelationService(
  21. IDbContext idbContext)
  22. {
  23. _idbContext = idbContext;
  24. }
  25. #region Testing
  26. /// <summary>
  27. /// Serialize all existing entities of the current DbContext
  28. /// </summary>
  29. /// <param name="depth">The depth the serialization should dive in.</param>
  30. /// <param name="format">The output format.</param>
  31. /// <returns>Return all entities either in JSON- or XML-format.</returns>
  32. public List<string> GetRelations(int depth, DbRelationFormat format)
  33. {
  34. var result = new List<string>();
  35. var dbContext = (AppendixObjectContext)_idbContext;
  36. var userList = dbContext.Get<Core.Domain.User.User>().ToList();
  37. var deviationList = dbContext.Get<Core.Domain.Deviation.Deviation>().ToList();
  38. var siteList = dbContext.Get<Core.Domain.Site.Site>().ToList();
  39. var appendixList = dbContext.Get<Core.Domain.Appendix.Appendix>().ToList();
  40. if (format == DbRelationFormat.Json)
  41. {
  42. result.Add(SerializeObjectJson(userList, depth));
  43. result.Add(SerializeObjectJson(deviationList, depth));
  44. result.Add(SerializeObjectJson(siteList, depth));
  45. result.Add(SerializeObjectJson(appendixList, depth));
  46. }
  47. else
  48. result.Add(String.Empty);
  49. return result;
  50. }
  51. #endregion
  52. #region Utilities
  53. /// <summary>
  54. /// Serialize objects using CustomJsonTextWriter
  55. /// </summary>
  56. /// <param name="obj">The object to be serialized.</param>
  57. /// <param name="maxDepth">The maximum depth.</param>
  58. public static string SerializeObjectJson(object obj, int maxDepth)
  59. {
  60. using (var strWriter = new StringWriter())
  61. {
  62. using (var jsonWriter = new CustomJsonTextWriter(strWriter))
  63. {
  64. Func<bool> include = () => jsonWriter.CurrentDepth <= maxDepth;
  65. var resolver = new CustomContractResolver(include);
  66. var serializer = new JsonSerializer
  67. {
  68. ContractResolver = resolver,
  69. Formatting = Formatting.Indented,
  70. ReferenceLoopHandling = ReferenceLoopHandling.Serialize
  71. };
  72. serializer.Serialize(jsonWriter, obj);
  73. }
  74. return strWriter.ToString();
  75. }
  76. }
  77. #endregion
  78. }
  79. public class CustomJsonTextWriter : JsonTextWriter
  80. {
  81. public CustomJsonTextWriter(TextWriter textWriter) : base(textWriter) { }
  82. public int CurrentDepth { get; private set; }
  83. public override void WriteStartObject()
  84. {
  85. CurrentDepth++;
  86. base.WriteStartObject();
  87. }
  88. public override void WriteEndObject()
  89. {
  90. CurrentDepth--;
  91. base.WriteEndObject();
  92. }
  93. }
  94. public class CustomContractResolver : DefaultContractResolver
  95. {
  96. private readonly Func<bool> _includeProperty;
  97. public CustomContractResolver(Func<bool> includeProperty)
  98. {
  99. _includeProperty = includeProperty;
  100. }
  101. protected override JsonProperty CreateProperty(
  102. MemberInfo member, MemberSerialization memberSerialization)
  103. {
  104. var property = base.CreateProperty(member, memberSerialization);
  105. var shouldSerialize = property.ShouldSerialize;
  106. property.ShouldSerialize = obj => _includeProperty() &&
  107. (shouldSerialize == null ||
  108. shouldSerialize(obj));
  109. return property;
  110. }
  111. }
  112. }