You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LuConvertersExtensions.cs 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Linq;
  5. using Luticate2.Auth.Utils.Business.Converters.ObjectConverter;
  6. using Luticate2.Auth.Utils.Business.Converters.ObjectConverterDescriptor;
  7. using Luticate2.Auth.Utils.Interfaces;
  8. using Microsoft.Extensions.DependencyInjection;
  9. using Microsoft.Extensions.DependencyInjection.Extensions;
  10. namespace Luticate2.Auth.Utils.Business.Converters
  11. {
  12. public static class LuConvertersExtensions
  13. {
  14. public class LuObjectConvertersOptions
  15. {
  16. public List<Type> ValueTypes { get; set; }
  17. public List<Type> ListTypes { get; set; }
  18. }
  19. public static void AddLuObjectConverterDescriptors(this IServiceCollection services)
  20. {
  21. var typeILuObjectConverterDescriptor = typeof(ILuObjectConverterDescriptor<,>);
  22. var typeILuObjectConverterDescriptorEnumerable =
  23. typeILuObjectConverterDescriptor.MakeGenericType(typeof(Enumerable), typeof(Enumerable));
  24. services.AddSingleton<ILuObjectConverterDescriptorIdentity, LuObjectConverterDescriptorIdentity>();
  25. services.AddSingleton(typeILuObjectConverterDescriptorEnumerable, typeof(LuObjectConverterDescriptorEnumerable));
  26. }
  27. public static void AddLuObjectConverters(this IServiceCollection services, Action<LuObjectConvertersOptions> optionsAction = null)
  28. {
  29. var typeILuObjectConverter = typeof(ILuObjectConverter<,>);
  30. var options = new LuObjectConvertersOptions
  31. {
  32. ValueTypes = new List<Type>
  33. {
  34. typeof(bool),
  35. typeof(decimal),
  36. typeof(double),
  37. typeof(float),
  38. typeof(sbyte),
  39. typeof(byte),
  40. typeof(char),
  41. typeof(short),
  42. typeof(ushort),
  43. typeof(int),
  44. typeof(uint),
  45. typeof(long),
  46. typeof(ulong),
  47. typeof(Guid),
  48. typeof(DateTime),
  49. typeof(DateTimeOffset),
  50. typeof(TimeSpan)
  51. },
  52. ListTypes = new List<Type>
  53. {
  54. typeof(IEnumerable<>),
  55. typeof(ICollection<>),
  56. typeof(Collection<>),
  57. typeof(IList<>),
  58. typeof(List<>)
  59. }
  60. };
  61. optionsAction?.Invoke(options);
  62. services.TryAddSingleton<ILuConvertersAllocator, LuConvertersAllocator>();
  63. services.AddSingleton<ILuObjectConverterIdentity, LuObjectConverterIdentity>();
  64. foreach (var valueType in options.ValueTypes)
  65. {
  66. var nullableValueType = typeof(Nullable<>).MakeGenericType(valueType);
  67. var converterType = typeILuObjectConverter.MakeGenericType(valueType, nullableValueType);
  68. services.AddSingleton(converterType, typeof(LuObjectConverterIdentity));
  69. }
  70. foreach (var typeFrom in options.ListTypes)
  71. {
  72. foreach (var typeTo in options.ListTypes)
  73. {
  74. services.AddSingleton(typeILuObjectConverter.MakeGenericType(typeFrom, typeTo), typeof(LuObjectConverterLists));
  75. }
  76. }
  77. }
  78. public static void AddLuObjectConverterPoco<TTypeFrom, TTypeTo>(this IServiceCollection services)
  79. {
  80. services.AddSingleton<ILuObjectConverter<TTypeFrom, TTypeTo>, LuObjectConverterPoco<TTypeFrom, TTypeTo>>();
  81. }
  82. }
  83. }