Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allowed collections to be inherited from Base Types #161

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions GraphDiff/GraphDiff.Tests/Models/TestModels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,25 @@ public class NullableKeyModel
public Guid? Id { get; set; }
}

// Generic Collection models:
public class SimpleTitleModel
{
[Key]
public string Title { get; set; }
}

public class CollectionFromListModel : List<SimpleTitleModel>
{

}

public class CollectionFromListEntity : Entity
{
public CollectionFromListModel CollectionItems { get; set; }

public List<SimpleTitleModel> SimpleTitleItems { get; set; }
}

// ====================================
// Second tier models
// ====================================
Expand Down
2 changes: 2 additions & 0 deletions GraphDiff/GraphDiff.Tests/TestDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public class TestDbContext : DbContext
public IDbSet<OneToOneOwnedParentModel> OneToOneOwnedParentModels { get; set; }
public IDbSet<OneToManyOwnedParentModel> OneToManyOwnedParentModels { get; set; }

public IDbSet<CollectionFromListEntity> CollectionFromListEntities { get; set; }

public TestDbContext() : base("GraphDiff") { }
public TestDbContext(DbConnection connection) : base(connection, true) { }

Expand Down
39 changes: 39 additions & 0 deletions GraphDiff/GraphDiff.Tests/Tests/OwnedCollectionBehaviours.cs
Original file line number Diff line number Diff line change
Expand Up @@ -415,5 +415,44 @@ public void ShouldUpdateOwnedCollectionElementWithTwoParents()
Assert.AreEqual(secondParent.Id, attachedChild.SecondParent.Id);
}
}

[TestMethod]
public void ShouldWorkWithListEntities()
{
var source = new CollectionFromListEntity()
{
CollectionItems = new CollectionFromListModel()
{
new SimpleTitleModel() { Title = "Test" }
},
SimpleTitleItems = new List<SimpleTitleModel>()
{
new SimpleTitleModel() { Title = "Old" }
}
};
using (var context = new TestDbContext())
{
context.CollectionFromListEntities.Add(source);
context.SaveChanges();
}

var newExpectedCollectionValue = "New Collection Value";
var newExpectedSimpleValue = "New Simple Value";
source.CollectionItems[0].Title = newExpectedCollectionValue;
source.SimpleTitleItems[0].Title = newExpectedSimpleValue;

using (var context = new TestDbContext())
{
context.UpdateGraph(source, map => map.OwnedCollection(c => c.CollectionItems).OwnedCollection(c => c.SimpleTitleItems));
context.SaveChanges();

var reloaded = context.CollectionFromListEntities
.Include(x => x.CollectionItems)
.Include(x => x.SimpleTitleItems).FirstOrDefault(c => c.Id == source.Id);

Assert.AreEqual(newExpectedCollectionValue, reloaded.CollectionItems[0].Title);
Assert.AreEqual(newExpectedSimpleValue, reloaded.SimpleTitleItems[0].Title);
}
}
}
}
8 changes: 7 additions & 1 deletion GraphDiff/GraphDiff/Internal/Graph/CollectionGraphNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,13 @@ private Type GetCollectionElementType()
return Accessor.PropertyType.GetGenericArguments()[0];
}

throw new InvalidOperationException("GraphDiff requires the collection to be either IEnumerable<T> or T[]");
var baseType = Accessor.PropertyType.BaseType;
if (baseType != null && baseType.IsGenericType)
{
return baseType.GetGenericArguments()[0];
}

throw new InvalidOperationException("GraphDiff requires the collection to be either IEnumerable<T> or T[] or derived from GenericType");
}
}
}