public void TestPrimaryMapperMappingViaExpressionForBaseProperty() { // Derived 1 // Setup PrimaryMapper.Add <PrimaryMapperTestDerivedClass1>(e => e.ColumnId); // Act var actual = PrimaryMapper.Get <PrimaryMapperTestDerivedClass1>(); var expected = "ColumnId"; // Assert Assert.IsTrue(actual?.IsPrimary() == true); Assert.AreEqual(expected, actual?.GetMappedName()); // Derived 2 // Setup PrimaryMapper.Add <PrimaryMapperTestDerivedClass2>(e => e.ColumnId); // Act actual = PrimaryMapper.Get <PrimaryMapperTestDerivedClass2>(); expected = "ColumnId"; // Assert Assert.IsTrue(actual?.IsPrimary() == true); Assert.AreEqual(expected, actual?.GetMappedName()); }
public void TestPrimaryMapperMappingViaExpression() { // Setup PrimaryMapper.Add <PrimaryMapperTestClass>(e => e.ColumnInt); // Act var actual = PrimaryMapper.Get <PrimaryMapperTestClass>(); var expected = "ColumnInt"; // Assert Assert.IsTrue(actual?.IsPrimary() == true); Assert.AreEqual(expected, actual?.GetMappedName()); }
public void TestPrimaryMapperMappingViaFieldOverride() { // Setup PrimaryMapper.Add <PrimaryMapperTestClass>(new Field("ColumnInt")); PrimaryMapper.Add <PrimaryMapperTestClass>(new Field("ColumnString"), true); // Act var actual = PrimaryMapper.Get <PrimaryMapperTestClass>(); var expected = "ColumnString"; // Assert Assert.IsTrue(actual?.IsPrimary() == true); Assert.AreEqual(expected, actual?.GetMappedName()); }
/// <summary> /// Resolves the primary <see cref="ClassProperty"/> of the data entity type. /// </summary> /// <param name="entityType">The type of the data entity.</param> /// <returns>The instance of the primary <see cref="ClassProperty"/> object.</returns> public ClassProperty Resolve(Type entityType) { var properties = PropertyCache.Get(entityType); // Check for the properties if (properties == null) { return(null); } // Get the first entry with Primary attribute var property = properties .FirstOrDefault(p => p.GetPrimaryAttribute() != null); // Get from the implicit mapping if (property == null) { property = PrimaryMapper.Get(entityType); } // Id Property if (property == null) { property = properties .FirstOrDefault(p => string.Equals(p.PropertyInfo.Name, "id", StringComparison.OrdinalIgnoreCase)); } // Type.Name + Id if (property == null) { property = properties .FirstOrDefault(p => string.Equals(p.PropertyInfo.Name, string.Concat(p.GetDeclaringType().Name, "id"), StringComparison.OrdinalIgnoreCase)); } // Mapping.Name + Id if (property == null) { property = properties .FirstOrDefault(p => string.Equals(p.PropertyInfo.Name, string.Concat(ClassMappedNameCache.Get(p.GetDeclaringType()), "id"), StringComparison.OrdinalIgnoreCase)); } // Return the instance return(property); }
public void TestPrimaryMapperMappingViaFieldWithMapAttribute() { // Setup PrimaryMapper.Add <PrimaryMapperTestWithAttributeClass>(new Field("ColumnInt")); // Act var actual = PrimaryMapper.Get <PrimaryMapperTestWithAttributeClass>(); var expected = "ColumnInt"; // Assert Assert.IsTrue(actual?.IsPrimary() == true); Assert.AreEqual(expected, actual?.GetMappedName()); // Act actual = PrimaryCache.Get <PrimaryMapperTestWithAttributeClass>(); expected = "ColumnString"; // Assert Assert.IsTrue(actual?.IsPrimary() == true); Assert.AreEqual(expected, actual?.GetMappedName()); }