Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

LuObjectConverterPoco.cs 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System;
  2. using Luticate2.Auth.Utils.Business.Utils;
  3. using Luticate2.Auth.Utils.Dbo.Fields;
  4. using Luticate2.Auth.Utils.Dbo.Result;
  5. using Luticate2.Auth.Utils.Interfaces;
  6. using Microsoft.Extensions.DependencyInjection;
  7. namespace Luticate2.Auth.Utils.Business.ObjectConverter
  8. {
  9. public class LuObjectConverterPoco<TTypeFrom, TTypeTo> : ILuObjectConverter<TTypeFrom, TTypeTo>
  10. {
  11. protected IServiceProvider ServiceProvider { get; }
  12. public LuObjectConverterPoco(IServiceProvider serviceProvider)
  13. {
  14. ServiceProvider = serviceProvider;
  15. }
  16. public LuResult<object> Convert(object srcObj, LuPartialFieldsDbo fields, ILuObjectConverterOptions options)
  17. {
  18. // TODO Check if this is correct
  19. if (srcObj == null)
  20. {
  21. return LuResult<object>.Ok(default(TTypeTo));
  22. }
  23. // TODO Handle descriptor == null
  24. var descriptor = ServiceProvider.GetService<ILuObjectConverterDescriptor<TTypeTo, TTypeFrom>>();
  25. var typeTo = typeof(TTypeTo);
  26. var dstObj = options.Allocator.GetInstance(typeTo);
  27. foreach (var memberInfo in typeTo.GetProperties())
  28. {
  29. // TODO check if field is included in fields
  30. // TODO Handle srcPropExpression == null
  31. var srcPropExpression = descriptor.GetMemberValueExpression(memberInfo, options.DescriptorOptions);
  32. var srcPropDelegate = srcPropExpression.Compile();
  33. var srcProp = srcPropDelegate.DynamicInvoke(srcObj);
  34. var srcType = srcProp != null ? srcProp.GetType() : srcPropExpression.ReturnType;
  35. var dstType = memberInfo.GetUnderlyingType();
  36. // TODO Handle objectConverter == null
  37. var objectConverter = LuObjectConverterUtils.GetObjectConverter(ServiceProvider, srcType, dstType);
  38. var dstPropResult = objectConverter.Convert(srcProp, fields, options);
  39. if (!dstPropResult)
  40. {
  41. return dstPropResult;
  42. }
  43. var dstProp = dstPropResult.Data;
  44. var result = LuExpressionUtils.SetValueFromMember(memberInfo, dstObj, dstProp);
  45. if (!result)
  46. {
  47. return result.To<object>();
  48. }
  49. }
  50. return LuResult<object>.Ok(dstObj);
  51. }
  52. }
  53. }