public ObjectConverter(JsonSerializerOptions options) { _objectMapping = options.GetObjectMappingRegistry().Lookup <T>(); _memberConverters = new Lazy <MemberConverters>(() => MemberConverters.Create(options, _objectMapping)); _isInterfaceOrAbstract = typeof(T).IsInterface || typeof(T).IsAbstract; _isStruct = typeof(T).IsStruct(); if (!_isInterfaceOrAbstract && _objectMapping.CreatorMapping == null && !_isStruct) { ConstructorInfo defaultConstructorInfo = typeof(T).GetConstructor( BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, Type.EmptyTypes, null); if (defaultConstructorInfo == null) { throw new JsonException($"Cannot find a default constructor on type {typeof(T)}"); } _constructor = defaultConstructorInfo.CreateDelegate <T>(); } _discriminatorConvention = options.GetDiscriminatorConventionRegistry().GetConvention(typeof(T)); _referenceHandling = _isStruct ? ReferenceHandling.Default : options.GetReferenceHandling(); }
public void Visit(IObjectMapping mapping) { this.AddTypeLine(mapping); if (this.CheckRecursivelyProcessed(mapping)) { return; } this.aLevel++; foreach (var member in mapping.Members) { this.aTypeIdentification = $"{ToStringVisitor.GetFullName(member.From)} => {ToStringVisitor.GetFullName(member.To)}"; if (member.Mapping == null) { this.AddNullLine(); } else { member.Mapping.Accept(this); } } this.aLevel--; this.aProcessed.Remove(mapping.GetType()); }
internal static void ValidateObjectClasses(IObjectMapping baseTypeMapping, IObjectMapping subTypeMapping) { if (!(baseTypeMapping.ObjectClasses ?? new string[0]).Any()) { throw new InvalidOperationException( $"In order to use subclass mapping {baseTypeMapping.Type.Name} must be mapped with objectClasses"); } if (!(subTypeMapping.ObjectClasses ?? new string[0]).Any()) { throw new InvalidOperationException( $"In order to use subclass mapping {subTypeMapping.Type.Name} must be mapped with objectClasses"); } var currentMappings = new[] { baseTypeMapping }.Union(baseTypeMapping.HasSubTypeMappings ? baseTypeMapping.SubTypeMappings : (IList <IObjectMapping>) new List <IObjectMapping>()); if (currentMappings.Any(objectMapping => objectMapping.ObjectClasses.OrderBy(x => x) .SequenceEqual(subTypeMapping.ObjectClasses.OrderBy(x => x), StringComparer.InvariantCultureIgnoreCase))) { throw new InvalidOperationException($"All sub types of {baseTypeMapping.Type.Name} must have a unique sequence of objectClasses."); } }
private IDiscriminatorConvention?InternalGetConvention(Type type) { IDiscriminatorConvention?convention = _conventions.FirstOrDefault(c => c.TryRegisterType(type)); if (convention != null) { IObjectMapping objectMapping = _options.GetObjectMappingRegistry().Lookup(type); objectMapping.AddDiscriminatorMapping(); // setup discriminator for all base types for (Type?currentType = type.BaseType; currentType != null && currentType != typeof(object); currentType = currentType.BaseType) { objectMapping = _options.GetObjectMappingRegistry().Lookup(currentType); objectMapping.AddDiscriminatorMapping(); _conventionsByType.TryAdd(currentType, convention); } // setup discriminator for all interfaces foreach (Type @interface in type.GetInterfaces()) { _conventionsByType.TryAdd(@interface, convention); } } return(convention); }
public IQueryCommand GetCommand(QueryCommandType type, IQueryCommandOptions options, IObjectMapping mapping) { Type = type; Options = options; Mapping = mapping; return(QueryCommandToReturn); }
public MemberMapping(JsonSerializerOptions options, IObjectMapping objectMapping, MemberInfo memberInfo, Type memberType) { _objectMapping = objectMapping; _options = options; MemberInfo = memberInfo; MemberType = memberType; DefaultValue = Default.Value(memberType); }
// Constructor public MainWindowViewModel(IDataAccess <Person> repository, IObjectMapping mapper, IObjectMapping reverseMapper) { this.personRepository = repository; this.mapper = mapper; this.reverseMapper = reverseMapper; Reset(); logger.Info("Login"); }
public MemberMapping(CborConverterRegistry converterRegistry, IObjectMapping objectMapping, MemberInfo memberInfo, Type memberType) { _objectMapping = objectMapping; _converterRegistry = converterRegistry; MemberInfo = memberInfo; MemberType = memberType; MemberName = null; DefaultValue = (memberType.IsClass || memberType.IsInterface) ? null : Activator.CreateInstance(memberType); }
public IObjectMapping Lookup(Type type) { IObjectMapping objectMapping = _objectMappings.GetOrAdd(type, t => CreateDefaultObjectMapping(type)); if (objectMapping is IMappingInitialization mappingInitialization) { mappingInitialization.Initialize(); } return(objectMapping); }
public bool TryRegisterType(Type type) { IObjectMapping objectMapping = _options.GetObjectMappingRegistry().Lookup(type); if (objectMapping.Discriminator == null || !(objectMapping.Discriminator is T discriminator)) { return(false); } _discriminatorsByType[type] = discriminator; _typesByDiscriminator.Add(discriminator, type); return(true); }
public bool TryRegisterType(Type type) { IObjectMapping objectMapping = _serializationRegistry.ObjectMappingRegistry.Lookup(type); if (objectMapping.Discriminator == null || !(objectMapping.Discriminator is T discriminator)) { return(false); } _discriminatorsByType.TryAdd(type, discriminator); _typesByDiscriminator.TryAdd(discriminator, type); return(true); }
public ObjectConverter(CborOptions options) { _options = options; _registry = options.Registry; _objectMapping = _registry.ObjectMappingRegistry.Lookup <T>(); _memberConvertersForWrite = new List <IMemberConverter>(); foreach (IMemberMapping memberMapping in _objectMapping.MemberMappings) { IMemberConverter memberConverter = memberMapping.GenerateMemberConverter(); if (memberMapping.CanBeDeserialized || _objectMapping.IsCreatorMember(memberConverter.MemberName)) { _memberConvertersForRead.Add(memberConverter.MemberName, memberConverter); if (memberConverter.RequirementPolicy == RequirementPolicy.AllowNull || memberConverter.RequirementPolicy == RequirementPolicy.Always) { _requiredMemberConvertersForRead.Add(memberConverter); } } if (memberMapping.CanBeSerialized) { _memberConvertersForWrite.Add(memberConverter); } } _isInterfaceOrAbstract = typeof(T).IsInterface || typeof(T).IsAbstract; _isStruct = typeof(T).IsStruct(); if (!_isInterfaceOrAbstract && !_isStruct && _objectMapping.CreatorMapping == null) { ConstructorInfo?defaultConstructorInfo = typeof(T).GetConstructor( BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, Type.EmptyTypes, null); if (defaultConstructorInfo == null) { throw new CborException($"Cannot find a default constructor on type {typeof(T)}"); } _constructor = defaultConstructorInfo.CreateDelegate <T>(); } _discriminatorConvention = _registry.DiscriminatorConventionRegistry.GetConvention(typeof(T)); }
private IObjectMapping CreateDefaultObjectMapping(Type type) { IObjectMapping objectMapping = (IObjectMapping)Activator.CreateInstance(typeof(ObjectMapping <>).MakeGenericType(type), _options); objectMapping.AutoMap(); if (objectMapping is IMappingInitialization mappingInitialization) { mappingInitialization.Initialize(); } return(objectMapping); }
protected QueryCommand(IQueryCommandOptions options, IObjectMapping mapping, bool initializeAttributes) { Options = options; Mapping = mapping; SearchRequest = new SearchRequest { Filter = options.Filter }; if (Options.Controls != null) { SearchRequest.Controls.AddRange(Options.Controls.ToArray()); } if (initializeAttributes) { InitializeAttributes(); } }
public void Register(IObjectMapping objectMapping) { _objectMappings.AddOrUpdate(objectMapping.ObjectType, objectMapping, (type, existingObjectMapping) => objectMapping); IMappingInitialization mappingInitialization = objectMapping as IMappingInitialization; if (mappingInitialization != null) { mappingInitialization.Initialize(); } if (objectMapping.Discriminator != null) { _registry.DiscriminatorConventionRegistry.RegisterType(objectMapping.ObjectType); } }
public void Register(IObjectMapping objectMapping) { IMappingInitialization mappingInitialization = objectMapping as IMappingInitialization; if (mappingInitialization != null) { mappingInitialization.Initialize(); } _objectMappings.AddOrUpdate(objectMapping.ObjectType, objectMapping, (type, existingObjectMapping) => objectMapping); _options.GetDiscriminatorConventionRegistry().RegisterType(objectMapping.ObjectType); if (mappingInitialization != null) { mappingInitialization.PostInitialize(); } }
public DirectoryQueryProvider(LdapConnection connection, SearchScope scope, IObjectMapping mapping, bool pagingEnabled) { if (mapping == null) { throw new ArgumentNullException("mapping"); } if (connection == null) { throw new ArgumentNullException("connection"); } _scope = scope; #if (NET35 || NET40) _connection = new WeakReference(connection); #else _connection = new WeakReference <LdapConnection>(connection); #endif _mapping = mapping; _pagingEnabled = pagingEnabled; }
public static MemberConverters Create(JsonSerializerOptions options, IObjectMapping objectMapping) { MemberConverters converters = new MemberConverters(); if (options.PropertyNameCaseInsensitive) { converters.ForReadAsString = new Dictionary <string, IMemberConverter>(StringComparer.OrdinalIgnoreCase); } converters.ExtensionData = objectMapping.ExtensionData; foreach (IMemberMapping memberMapping in objectMapping.MemberMappings) { IMemberConverter memberConverter = memberMapping.GenerateMemberConverter(); if (memberMapping.CanBeDeserialized || objectMapping.IsCreatorMember(memberConverter.MemberName)) { if (options.PropertyNameCaseInsensitive) { converters.ForReadAsString.Add(memberConverter.MemberNameAsString, memberConverter); } converters.ForRead.Add(memberConverter.MemberName, memberConverter); if (memberConverter.RequirementPolicy == RequirementPolicy.AllowNull || memberConverter.RequirementPolicy == RequirementPolicy.Always) { converters.RequiredForRead.Add(memberConverter); } } if (memberMapping.CanBeSerialized) { converters.ForWrite.Add(memberConverter); } } return(converters); }
public virtual void AddSubTypeMapping(IObjectMapping mapping) { if (WithoutSubTypeMapping || SubTypeMappingsObjectClassDictionary.Values.Contains(mapping)) { return; } var currentMappings = SortByInheritanceDescending(SubTypeMappingsObjectClassDictionary.Values.Union(new[] { mapping })); SubTypeMappingsObjectClassDictionary.Clear(); SubTypeMappingsTypeDictionary.Clear(); foreach (var currentMapping in currentMappings) { var objectClasses = currentMapping.ObjectClasses.ToList(); //find direct ancestor object classes or default to this class' object classes if a direct ancestor hasn't been mapped yet. var parentObjectClasses = currentMappings .Where(x => currentMapping.Type.IsSubclassOf(x.Type)) .Select(x => x.ObjectClasses) .FirstOrDefault() ?? ObjectClasses; objectClasses = objectClasses.Except(parentObjectClasses, StringComparer.OrdinalIgnoreCase).ToList(); if (objectClasses.Count == 0) { throw new InvalidOperationException("Unable to identify distinct object class based on mapped inheritance"); } SubTypeMappingsObjectClassDictionary.Add(objectClasses[0], currentMapping); SubTypeMappingsTypeDictionary.Add(currentMapping.Type, currentMapping); } _readOnlySubTypeMappings = null; _propertyNames = InitializePropertyNames(); }
private void MapSubTypes(IObjectMapping mapping) { #if (NET35 || NET40) var mappings = _mappings.ToReadOnly(); foreach (var objectMapping in mappings) #else foreach (var objectMapping in _mappings) #endif { //check if already mapped instance is in new mappings inheritance chain var alreadyMappedBaseType = objectMapping.Key; while (alreadyMappedBaseType != null && alreadyMappedBaseType != typeof(object)) { if (alreadyMappedBaseType == mapping.Type) { ValidateObjectClasses(mapping, objectMapping.Value); mapping.AddSubTypeMapping(objectMapping.Value); break; } alreadyMappedBaseType = alreadyMappedBaseType.BaseType; } //check if new mapping is in the inheritance chain of an existing mapping var newMappedBaseType = mapping.Type; while (newMappedBaseType != null && newMappedBaseType != typeof(object)) { if (newMappedBaseType == objectMapping.Key) { ValidateObjectClasses(objectMapping.Value, mapping); objectMapping.Value.AddSubTypeMapping(mapping); break; } newMappedBaseType = newMappedBaseType.BaseType; } } }
public QueryTranslator(IObjectMapping mapping) { _mapping = mapping; }
public ResultTransformer(IDictionary <string, string> queriedProperties, IObjectMapping mapping, bool setOriginalValues = true) { QueriedProperties = queriedProperties; ObjectMapping = mapping; _setOriginalValues = setOriginalValues; }
public void Visit(IObjectMapping mapping) { this.aInnerVisitor.Visit(mapping); }
public FirstOrDefaultQueryCommand(IQueryCommandOptions options, IObjectMapping mapping) : base(options, mapping, true) { }
public override void AddSubTypeMapping(IObjectMapping mapping) { throw new NotSupportedException("Anonymous objects can't support sub types"); }
public CreatorMapping(IObjectMapping objectMapping, MethodInfo method) { _objectMapping = objectMapping; _delegate = method.CreateDelegate(); _parameters = method.GetParameters(); }
public CreatorMapping(IObjectMapping objectMapping, Delegate @delegate) { _objectMapping = objectMapping; _delegate = @delegate; _parameters = @delegate.Method.GetParameters(); }
public CreatorMapping(IObjectMapping objectMapping, ConstructorInfo constructorInfo) { _objectMapping = objectMapping; _delegate = constructorInfo.CreateDelegate(); _parameters = constructorInfo.GetParameters(); }
public SingleQueryCommand(IQueryCommandOptions options, IObjectMapping mapping) : base(options, mapping, true) { }
public GetRequestCommand(IQueryCommandOptions options, IObjectMapping mapping) : base(options, mapping, true) { }