Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

LuObjectConverterPoco.cs 3.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using System;
  2. using System.Linq;
  3. using Luticate2.Auth.Utils.Business.Fields;
  4. using Luticate2.Auth.Utils.Business.Utils;
  5. using Luticate2.Auth.Utils.Dbo;
  6. using Luticate2.Auth.Utils.Dbo.Fields;
  7. using Luticate2.Auth.Utils.Dbo.Result;
  8. using Luticate2.Auth.Utils.Interfaces;
  9. using Microsoft.Extensions.DependencyInjection;
  10. namespace Luticate2.Auth.Utils.Business.Converters.ObjectConverter
  11. {
  12. public class LuObjectConverterPoco<TTypeFrom, TTypeTo> : ILuObjectConverter<TTypeFrom, TTypeTo>
  13. {
  14. protected IServiceProvider ServiceProvider { get; }
  15. public LuObjectConverterPoco(IServiceProvider serviceProvider)
  16. {
  17. ServiceProvider = serviceProvider;
  18. }
  19. public LuResult<object> Convert(object srcObj, object dstObj, LuFieldDbo path, LuPartialFieldsDbo fields, ILuObjectConverterOptions options)
  20. {
  21. if (srcObj == null)
  22. {
  23. return LuResult<object>.Ok(default(TTypeTo));
  24. }
  25. var typeTo = typeof(TTypeTo);
  26. var descriptor = ServiceProvider.GetService<ILuObjectConverterDescriptor<TTypeTo, TTypeFrom>>();
  27. if (descriptor == null)
  28. {
  29. return LuResult<object>.Error(LuStatus.InternalError.ToInt(),
  30. $"Could not find converter descriptor for {typeTo} => {typeof(TTypeFrom)}");
  31. }
  32. foreach (var memberInfo in typeTo.GetProperties())
  33. {
  34. var memberPath = LuFieldDbo.Make(path).Add(memberInfo.Name);
  35. if (!fields.Fields.IsIncluded(memberPath))
  36. {
  37. continue;
  38. }
  39. // TODO Hack to deal with inherited members; need to check if it is correct
  40. var originalMemberInfo = memberInfo.DeclaringType.GetMember(memberInfo.Name).First();
  41. var subSrcPropExpressionResult = descriptor.GetMemberValueExpression(originalMemberInfo, options.DescriptorOptions);
  42. if (!subSrcPropExpressionResult)
  43. {
  44. return subSrcPropExpressionResult.To<object>();
  45. }
  46. var subSrcPropExpression = subSrcPropExpressionResult.Data;
  47. var subSrcPropDelegate = subSrcPropExpression.Compile();
  48. var subSrcProp = subSrcPropDelegate.DynamicInvoke(srcObj);
  49. var subSrcType = subSrcProp != null ? subSrcProp.GetType() : subSrcPropExpression.ReturnType;
  50. var subDstType = memberInfo.GetUnderlyingType();
  51. var objectConverterResult = LuObjectConverterUtils.GetObjectConverter(ServiceProvider, subSrcType, subDstType);
  52. if (!objectConverterResult)
  53. {
  54. return objectConverterResult.To<object>();
  55. }
  56. var objectConverter = objectConverterResult.Data;
  57. var subDstObjResult = options.Allocator.GetInstance(subDstType);
  58. if (!subDstObjResult)
  59. {
  60. return subDstObjResult.To<object>();
  61. }
  62. var subDstObj = subDstObjResult.Data;
  63. var dstPropResult = objectConverter.Convert(subSrcProp, subDstObj, memberPath, fields, options);
  64. if (!dstPropResult)
  65. {
  66. return dstPropResult;
  67. }
  68. var dstProp = dstPropResult.Data;
  69. var result = LuExpressionUtils.SetValueFromMember(memberInfo, dstObj, dstProp);
  70. if (!result)
  71. {
  72. return result.To<object>();
  73. }
  74. }
  75. return LuResult<object>.Ok(dstObj);
  76. }
  77. }
  78. }