diff --git a/src/Marten.Testing/Linq/query_with_inheritance.cs b/src/Marten.Testing/Linq/query_with_inheritance.cs index 5a9edcba6a..a285c565ed 100644 --- a/src/Marten.Testing/Linq/query_with_inheritance.cs +++ b/src/Marten.Testing/Linq/query_with_inheritance.cs @@ -142,5 +142,22 @@ public void get_all_subclasses_of_an_interface() theSession.Query().Count().ShouldBe(3); } // ENDSAMPLE + + [Fact] + public void get_all_subclasses_of_an_interface_and_instantiate_them() + { + var smurf = new Smurf { Ability = "Follow the herd" }; + var papa = new PapaSmurf { Ability = "Lead" }; + var papy = new PapySmurf { Ability = "Lead" }; + var brainy = new BrainySmurf { Ability = "Invent" }; + theSession.Store(smurf, papa, brainy, papy); + + theSession.SaveChanges(); + + var list = theSession.Query().ToList(); + list.Count().ShouldBe(3); + list.Where(s => s.Ability == "Invent").Count().ShouldBe(1); + } + } } \ No newline at end of file diff --git a/src/Marten/Schema/Hierarchies/SubClassDocumentStorage.cs b/src/Marten/Schema/Hierarchies/SubClassDocumentStorage.cs index 02cf1ec2ea..2d801feb53 100644 --- a/src/Marten/Schema/Hierarchies/SubClassDocumentStorage.cs +++ b/src/Marten/Schema/Hierarchies/SubClassDocumentStorage.cs @@ -16,10 +16,12 @@ public class SubClassDocumentStorage where TBase : class { private readonly IDocumentStorage _parent; + private readonly SubClassMapping _mapping; - public SubClassDocumentStorage(IDocumentStorage parent) + public SubClassDocumentStorage(IDocumentStorage parent, SubClassMapping mapping) { _parent = parent; + _mapping = mapping; } public Type DocumentType => typeof(T); @@ -98,8 +100,11 @@ public T Resolve(int startingIndex, DbDataReader reader, IIdentityMap map) var id = reader[startingIndex + 1]; var version = reader.GetFieldValue(3); + var typeAlias = reader.GetString(startingIndex + 2); - return map.Get(id, typeof(T), json, version) as T; + var actualType = _mapping.TypeFor(typeAlias); + + return map.Get(id, actualType, json, version) as T; } public async Task ResolveAsync(int startingIndex, DbDataReader reader, IIdentityMap map, @@ -109,8 +114,9 @@ public async Task ResolveAsync(int startingIndex, DbDataReader reader, IIdent var id = await reader.GetFieldValueAsync(startingIndex + 1, token).ConfigureAwait(false); var version = await reader.GetFieldValueAsync(3, token).ConfigureAwait(false); + var typeAlias = await reader.GetFieldValueAsync(startingIndex + 2, token).ConfigureAwait(false); - return map.Get(id, typeof(T), json, version) as T; + return map.Get(id, _mapping.TypeFor(typeAlias), json, version) as T; } diff --git a/src/Marten/Schema/SubClassMapping.cs b/src/Marten/Schema/SubClassMapping.cs index d52b68dafe..6d92d350ee 100644 --- a/src/Marten/Schema/SubClassMapping.cs +++ b/src/Marten/Schema/SubClassMapping.cs @@ -114,7 +114,7 @@ private WhereFragment toBasicWhere() public IDocumentStorage BuildStorage(IDocumentSchema schema) { var parentStorage = Parent.As().BuildStorage(schema); - return typeof(SubClassDocumentStorage<,>).CloseAndBuildAs(parentStorage, DocumentType, + return typeof(SubClassDocumentStorage<,>).CloseAndBuildAs(parentStorage, this, DocumentType, Parent.DocumentType); } @@ -148,6 +148,11 @@ public IncludeJoin JoinToInclude(JoinType joinType, IQueryableDo return Parent.JoinToInclude(joinType, other, members, callback); } + public Type TypeFor(string alias) + { + return Parent.TypeFor(alias); + } + private static string GetTypeMartenAlias(Type documentType) { return GetTypeMartenAlias(new MappedType(documentType));