/// <summary> /// Initializes a new instance of the <see cref="CacheKeyConfiguration"/> class, using specified type to look /// for <see cref="AffinityKeyMappedAttribute"/>. /// </summary> public CacheKeyConfiguration(Type keyType) { IgniteArgumentCheck.NotNull(keyType, "keyType"); TypeName = keyType.FullName; AffinityKeyFieldName = AffinityKeyMappedAttribute.GetFieldNameFromAttribute(keyType); }
public void TestFieldAttribute() { Assert.AreEqual("Abc", AffinityKeyMappedAttribute.GetFieldNameFromAttribute(typeof(PublicField))); Assert.AreEqual("_abc", AffinityKeyMappedAttribute.GetFieldNameFromAttribute(typeof(PrivateField))); Assert.AreEqual("Abc", AffinityKeyMappedAttribute.GetFieldNameFromAttribute(typeof(InheritPublicField))); Assert.AreEqual("_abc", AffinityKeyMappedAttribute.GetFieldNameFromAttribute(typeof(InheritPrivateField))); }
public void TestPropertyAttribute() { Assert.IsNull(AffinityKeyMappedAttribute.GetFieldNameFromAttribute(typeof(NoAttr))); Assert.AreEqual("Abc", AffinityKeyMappedAttribute.GetFieldNameFromAttribute(typeof(PublicProperty))); Assert.AreEqual("Abc", AffinityKeyMappedAttribute.GetFieldNameFromAttribute(typeof(InheritPublicProperty))); Assert.AreEqual("Abc", AffinityKeyMappedAttribute.GetFieldNameFromAttribute(typeof(PrivateProperty))); Assert.AreEqual("Abc", AffinityKeyMappedAttribute.GetFieldNameFromAttribute(typeof(InheritPrivateProperty))); }
public void TestMultipleAttributes() { var ex = Assert.Throws <BinaryObjectException>(() => AffinityKeyMappedAttribute.GetFieldNameFromAttribute(typeof(MultipleAttributes))); Assert.AreEqual(string.Format( "Multiple 'AffinityKeyMappedAttribute' attributes found on type '{0}'. There can be only one " + "affinity field.", typeof(MultipleAttributes).FullName), ex.Message); }
/// <summary> /// Add user type. /// </summary> /// <param name="typeCfg">Type configuration.</param> /// <param name="typeResolver">The type resolver.</param> /// <exception cref="BinaryObjectException"></exception> private BinaryFullTypeDescriptor AddUserType(BinaryTypeConfiguration typeCfg, TypeResolver typeResolver) { // Get converter/mapper/serializer. IBinaryNameMapper nameMapper = typeCfg.NameMapper ?? _cfg.NameMapper ?? GetDefaultNameMapper(); IBinaryIdMapper idMapper = typeCfg.IdMapper ?? _cfg.IdMapper; bool keepDeserialized = typeCfg.KeepDeserialized ?? _cfg.KeepDeserialized; // Try resolving type. Type type = typeResolver.ResolveType(typeCfg.TypeName); if (type != null) { ValidateUserType(type); if (typeCfg.IsEnum != BinaryUtils.IsIgniteEnum(type)) { throw new BinaryObjectException( string.Format( "Invalid IsEnum flag in binary type configuration. " + "Configuration value: IsEnum={0}, actual type: IsEnum={1}, type={2}", typeCfg.IsEnum, type.IsEnum, type)); } // Type is found. var typeName = GetTypeName(type, nameMapper); int typeId = GetTypeId(typeName, idMapper); var affKeyFld = typeCfg.AffinityKeyFieldName ?? AffinityKeyMappedAttribute.GetFieldNameFromAttribute(type); var serializer = GetSerializer(_cfg, typeCfg, type, typeId, nameMapper, idMapper, _log); return(AddType(type, typeId, typeName, true, keepDeserialized, nameMapper, idMapper, serializer, affKeyFld, BinaryUtils.IsIgniteEnum(type))); } else { // Type is not found. string typeName = GetTypeName(typeCfg.TypeName, nameMapper); int typeId = GetTypeId(typeName, idMapper); return(AddType(null, typeId, typeName, true, keepDeserialized, nameMapper, idMapper, null, typeCfg.AffinityKeyFieldName, typeCfg.IsEnum)); } }
/// <summary> /// Add user type. /// </summary> /// <param name="type">The type.</param> /// <param name="typeId">The type id.</param> /// <param name="typeName">Name of the type.</param> /// <param name="registered">Registered flag.</param> /// <param name="desc">Existing descriptor.</param> /// <returns>Descriptor.</returns> private BinaryFullTypeDescriptor AddUserType(Type type, int typeId, string typeName, bool registered, BinaryFullTypeDescriptor desc) { Debug.Assert(type != null); Debug.Assert(typeName != null); var ser = GetSerializer(_cfg, null, type, typeId, null, null, _log); desc = desc == null ? new BinaryFullTypeDescriptor(type, typeId, typeName, true, _cfg.NameMapper, _cfg.IdMapper, ser, false, AffinityKeyMappedAttribute.GetFieldNameFromAttribute(type), BinaryUtils.IsIgniteEnum(type), registered) : new BinaryFullTypeDescriptor(desc, type, ser, registered); if (RegistrationDisabled) { return(desc); } var typeKey = BinaryUtils.TypeKey(true, typeId); var desc0 = _idToDesc.GetOrAdd(typeKey, x => desc); if (desc0.Type != null && desc0.TypeName != typeName) { ThrowConflictingTypeError(type, desc0.Type, typeId); } desc0 = _typeNameToDesc.GetOrAdd(typeName, x => desc); if (desc0.Type != null && desc0.TypeName != typeName) { ThrowConflictingTypeError(type, desc0.Type, typeId); } ValidateRegistration(type); _typeToDesc.Set(type, desc); return(desc); }