private static void RefreshForeignKeys <T>(ref T element) { var type = typeof(T); foreach (var relationshipProperty in type.GetRelationshipProperties()) { var relationshipAttribute = relationshipProperty.GetAttribute <RelationshipAttribute>(); if (relationshipAttribute is OneToOneAttribute || relationshipAttribute is ManyToOneAttribute) { var foreignKeyProperty = type.GetForeignKeyProperty(relationshipProperty); if (foreignKeyProperty != null) { EnclosedType enclosedType; var entityType = relationshipProperty.GetEntityType(out enclosedType); var destinationPrimaryKeyProperty = entityType.GetPrimaryKey(); Debug.Assert(enclosedType == EnclosedType.None, "ToOne relationships cannot be lists or arrays"); Debug.Assert(destinationPrimaryKeyProperty != null, "Found foreign key but destination Type doesn't have primary key"); var relationshipValue = relationshipProperty.GetValue(element, null); object foreignKeyValue = null; if (relationshipValue != null) { foreignKeyValue = destinationPrimaryKeyProperty.GetValue(relationshipValue, null); } foreignKeyProperty.SetValue(element, foreignKeyValue, null); } } else if (relationshipAttribute is TextBlobAttribute) { TextBlobOperations.UpdateTextBlobProperty(ref element, relationshipProperty); } } }
public void TestGetTextProperty() { const string textValue = "Mock Serialized String"; var values = new List <string> { "Foo string 1", "Foo string 2" }; var obj = new ClassA { Foo = "Foo String", ElementsBlobbed = textValue }; var mockSerializer = new Mock <ITextBlobSerializer>(); TextBlobOperations.SetTextSerializer(mockSerializer.Object); // Override default JSON serializer var obj1 = obj; var elementsProperty = typeof(ClassA).GetProperty("Elements"); mockSerializer.Setup(serializer => serializer.Deserialize(textValue, elementsProperty.PropertyType)).Returns(() => values); TextBlobOperations.GetTextBlobChild(obj, typeof(ClassA).GetProperty("Elements")); Assert.AreEqual(values, obj1.Elements); }
public void TestUpdateTextProperty() { var obj = new ClassA { Foo = "Foo String", Elements = new List <string> { "Foo String 1", "Foo String 2" } }; const string textValue = "Mock Serialized String"; var mockSerializer = new Mock <ITextBlobSerializer>(); TextBlobOperations.SetTextSerializer(mockSerializer.Object); // Override default JSON serializer var obj1 = obj; mockSerializer.Setup(serializer => serializer.Serialize(obj1.Elements)).Returns(() => textValue); TextBlobOperations.UpdateTextBlobProperty(obj, typeof(ClassA).GetProperty("Elements")); Assert.AreEqual(textValue, obj1.ElementsBlobbed); }
private static void GetChildRecursive(this SQLiteConnection conn, object element, PropertyInfo relationshipProperty, bool recursive, ObjectCache objectCache) { var relationshipAttribute = relationshipProperty.GetAttribute <RelationshipAttribute>(); if (relationshipAttribute is OneToOneAttribute) { conn.GetOneToOneChildren(new List <object> { element }, relationshipProperty, recursive, objectCache); } else if (relationshipAttribute is OneToManyAttribute) { conn.GetOneToManyChildren(element, relationshipProperty, recursive, objectCache); } else if (relationshipAttribute is ManyToOneAttribute) { conn.GetManyToOneChildren(new List <object> { element }, relationshipProperty, recursive, objectCache); } else if (relationshipAttribute is ManyToManyAttribute) { conn.GetManyToManyChildren(element, relationshipProperty, recursive, objectCache); } else if (relationshipAttribute is TextBlobAttribute) { TextBlobOperations.GetTextBlobChild(element, relationshipProperty); } }
public static void GetChild <T>(this SQLiteConnection conn, ref T element, PropertyInfo relationshipProperty) { var relationshipAttribute = relationshipProperty.GetAttribute <RelationshipAttribute>(); if (relationshipAttribute is OneToOneAttribute) { conn.GetOneToOneChild(ref element, relationshipProperty); } else if (relationshipAttribute is OneToManyAttribute) { conn.GetOneToManyChildren(ref element, relationshipProperty); } else if (relationshipAttribute is ManyToOneAttribute) { conn.GetManyToOneChild(ref element, relationshipProperty); } else if (relationshipAttribute is ManyToManyAttribute) { conn.GetManyToManyChildren(ref element, relationshipProperty); } else if (relationshipAttribute is TextBlobAttribute) { TextBlobOperations.GetTextBlobChild(ref element, relationshipProperty); } }
private static void GetChildrenRecursiveBatched <T>(this SQLiteConnection conn, IList <T> elements, ObjectCache objectCache) { var element = elements[0]; foreach (var relationshipProperty in element.GetType().GetRelationshipProperties()) { var relationshipAttribute = relationshipProperty.GetAttribute <RelationshipAttribute>(); if (relationshipAttribute.IsCascadeRead) { if (relationshipAttribute is OneToOneAttribute) { conn.GetOneToOneChildren(elements, relationshipProperty, true, objectCache); } else if (relationshipAttribute is OneToManyAttribute) { foreach (var e in elements) { conn.GetOneToManyChildren(e, relationshipProperty, true, objectCache); } } else if (relationshipAttribute is ManyToOneAttribute) { conn.GetManyToOneChildren(elements, relationshipProperty, true, objectCache); } else if (relationshipAttribute is ManyToManyAttribute) { foreach (var e in elements) { conn.GetManyToManyChildren(e, relationshipProperty, true, objectCache); } } else if (relationshipAttribute is TextBlobAttribute) { TextBlobOperations.GetTextBlobChild(element, relationshipProperty); } } else if (relationshipAttribute is TextBlobAttribute) { foreach (var e in elements) { conn.GetChildRecursive(e, relationshipProperty, false, objectCache); } } } }