Skip to content
Merged
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
17 changes: 17 additions & 0 deletions src/Marten.Testing/Linq/query_with_inheritance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,5 +142,22 @@ public void get_all_subclasses_of_an_interface()
theSession.Query<IPapaSmurf>().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<IPapaSmurf>().ToList();
list.Count().ShouldBe(3);
list.Where(s => s.Ability == "Invent").Count().ShouldBe(1);
}

}
}
12 changes: 9 additions & 3 deletions src/Marten/Schema/Hierarchies/SubClassDocumentStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ public class SubClassDocumentStorage<T, TBase>
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);
Expand Down Expand Up @@ -98,8 +100,11 @@ public T Resolve(int startingIndex, DbDataReader reader, IIdentityMap map)
var id = reader[startingIndex + 1];

var version = reader.GetFieldValue<Guid>(3);
var typeAlias = reader.GetString(startingIndex + 2);

return map.Get<TBase>(id, typeof(T), json, version) as T;
var actualType = _mapping.TypeFor(typeAlias);

return map.Get<TBase>(id, actualType, json, version) as T;
}

public async Task<T> ResolveAsync(int startingIndex, DbDataReader reader, IIdentityMap map,
Expand All @@ -109,8 +114,9 @@ public async Task<T> ResolveAsync(int startingIndex, DbDataReader reader, IIdent
var id = await reader.GetFieldValueAsync<object>(startingIndex + 1, token).ConfigureAwait(false);

var version = await reader.GetFieldValueAsync<Guid>(3, token).ConfigureAwait(false);
var typeAlias = await reader.GetFieldValueAsync<string>(startingIndex + 2, token).ConfigureAwait(false);

return map.Get<TBase>(id, typeof(T), json, version) as T;
return map.Get<TBase>(id, _mapping.TypeFor(typeAlias), json, version) as T;
}


Expand Down
7 changes: 6 additions & 1 deletion src/Marten/Schema/SubClassMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ private WhereFragment toBasicWhere()
public IDocumentStorage BuildStorage(IDocumentSchema schema)
{
var parentStorage = Parent.As<IDocumentMapping>().BuildStorage(schema);
return typeof(SubClassDocumentStorage<,>).CloseAndBuildAs<IDocumentStorage>(parentStorage, DocumentType,
return typeof(SubClassDocumentStorage<,>).CloseAndBuildAs<IDocumentStorage>(parentStorage, this, DocumentType,
Parent.DocumentType);
}

Expand Down Expand Up @@ -148,6 +148,11 @@ public IncludeJoin<TOther> JoinToInclude<TOther>(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));
Expand Down