using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using Luticate2.Auth.Utils.Business.Converters.ObjectConverter; using Luticate2.Auth.Utils.Business.Converters.ObjectConverterDescriptor; using Luticate2.Auth.Utils.Interfaces; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; namespace Luticate2.Auth.Utils.Business.Converters { public static class LuConvertersExtensions { public class LuObjectConvertersOptions { public List ValueTypes { get; set; } public List ListTypes { get; set; } } public static void AddLuObjectConverterDescriptors(this IServiceCollection services) { var typeILuObjectConverterDescriptor = typeof(ILuObjectConverterDescriptor<,>); var typeILuObjectConverterDescriptorEnumerable = typeILuObjectConverterDescriptor.MakeGenericType(typeof(Enumerable), typeof(Enumerable)); services.AddSingleton(); services.AddSingleton(typeILuObjectConverterDescriptorEnumerable, typeof(LuObjectConverterDescriptorEnumerable)); } public static void AddLuObjectConverters(this IServiceCollection services, Action optionsAction = null) { var typeILuObjectConverter = typeof(ILuObjectConverter<,>); var options = new LuObjectConvertersOptions { ValueTypes = new List { typeof(bool), typeof(decimal), typeof(double), typeof(float), typeof(sbyte), typeof(byte), typeof(char), typeof(short), typeof(ushort), typeof(int), typeof(uint), typeof(long), typeof(ulong), typeof(Guid), typeof(DateTime), typeof(DateTimeOffset), typeof(TimeSpan) }, ListTypes = new List { typeof(IEnumerable<>), typeof(ICollection<>), typeof(Collection<>), typeof(IList<>), typeof(List<>) } }; optionsAction?.Invoke(options); services.TryAddSingleton(); services.AddSingleton(); foreach (var valueType in options.ValueTypes) { var nullableValueType = typeof(Nullable<>).MakeGenericType(valueType); var converterType = typeILuObjectConverter.MakeGenericType(valueType, nullableValueType); services.AddSingleton(converterType, typeof(LuObjectConverterIdentity)); } foreach (var typeFrom in options.ListTypes) { foreach (var typeTo in options.ListTypes) { services.AddSingleton(typeILuObjectConverter.MakeGenericType(typeFrom, typeTo), typeof(LuObjectConverterLists)); } } } public static void AddLuObjectConverterPoco(this IServiceCollection services) { services.AddSingleton, LuObjectConverterPoco>(); } } }