123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- 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.Business.Utils;
- using Luticate2.Auth.Utils.Interfaces;
- using Microsoft.CodeAnalysis.Scripting;
- using Microsoft.Extensions.DependencyInjection;
- using Microsoft.Extensions.DependencyInjection.Extensions;
-
- namespace Luticate2.Auth.Utils.Business.Converters
- {
- public static class LuConvertersExtensions
- {
- public class LuObjectConvertersOptions
- {
- public List<Type> ValueTypes { get; set; }
-
- public List<Type> ListTypes { get; set; }
- }
-
- public static void AddLuObjectConverterDescriptors(this IServiceCollection services)
- {
- var typeILuObjectConverterDescriptor = typeof(ILuObjectConverterDescriptor<,>);
- var typeILuObjectConverterDescriptorEnumerable =
- typeILuObjectConverterDescriptor.MakeGenericType(typeof(Enumerable), typeof(Enumerable));
-
- services.AddSingleton<ILuObjectConverterDescriptorIdentity, LuObjectConverterDescriptorIdentity>();
- services.AddSingleton(typeILuObjectConverterDescriptorEnumerable, typeof(LuObjectConverterDescriptorEnumerable));
- }
-
- public static void AddLuObjectConverters(this IServiceCollection services, Action<LuObjectConvertersOptions> optionsAction = null)
- {
- var typeILuObjectConverter = typeof(ILuObjectConverter<,>);
-
- var options = new LuObjectConvertersOptions
- {
- ValueTypes = new List<Type>
- {
- 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<Type>
- {
- typeof(IEnumerable<>),
- typeof(ICollection<>),
- typeof(Collection<>),
- typeof(IList<>),
- typeof(List<>)
- }
- };
-
- optionsAction?.Invoke(options);
-
- services.TryAddSingleton<ILuConvertersAllocator, LuConvertersAllocator>();
- services.AddSingleton<ILuObjectConverterIdentity, LuObjectConverterIdentity>();
- 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<TTypeFrom, TTypeTo>(this IServiceCollection services)
- {
- services.AddSingleton<ILuObjectConverter<TTypeFrom, TTypeTo>, LuObjectConverterPoco<TTypeFrom, TTypeTo>>();
- }
-
- public static void AddLuOptions(this IServiceCollection services)
- {
- services.AddOptions<LuExpressionUtils.LuExpressionParserOptions>().Configure(options =>
- {
- options.ScriptOptions = ScriptOptions.Default
- .AddReferences(typeof(LuExpressionUtils.LuExpressionParserOptions).Assembly)
- .AddImports("System.Linq.Enumerable");
- });
- }
- }
- }
|