Permalink
Browse files

Resolving types automatically when querying tyepof(object)

  • Loading branch information...
sebastienros committed Nov 7, 2017
1 parent a929119 commit b7fad0bb0e2188faf8b6c3dafd9afa0a5aa88c07
Showing with 41 additions and 6 deletions.
  1. +2 −3 src/YesSql.Core/Data/WorkerQueryKey.cs
  2. +16 −3 src/YesSql.Core/Session.cs
  3. +23 −0 test/YesSql.Tests/CoreTests.cs
@@ -13,7 +13,7 @@ namespace YesSql.Data
public class WorkerQueryKey : IEquatable<WorkerQueryKey>
{
private readonly string _prefix;
private readonly List<int> _ids;
private readonly int[] _ids;
private readonly Dictionary<string, object> _parameters;
private int? _hashcode;
@@ -31,8 +31,7 @@ public WorkerQueryKey(string prefix, int[] ids)
}
_prefix = prefix;
_ids = new List<int>(ids);
_ids.Sort();
_ids = ids;
}
public WorkerQueryKey(string prefix, Dictionary<string, object> parameters)
View
@@ -267,15 +267,15 @@ private async Task DeleteEntityAsync(object obj)
return Get<T>(documents.ToArray());
}
public IEnumerable<T> Get<T>(Document[] documents) where T : class
public IEnumerable<T> Get<T>(IList<Document> documents) where T : class
{
if (documents == null || !documents.Any())
{
return Enumerable.Empty<T>();
}
var result = new List<T>();
var accessor = _store.GetIdAccessor(typeof(T), "Id");
// Are all the objects already in cache?
@@ -287,7 +287,20 @@ private async Task DeleteEntityAsync(object obj)
}
else
{
var item = (T)Store.Configuration.ContentSerializer.Deserialize(d.Content, typeof(T));
T item;
// If no type is specified, use the one from the document
if (typeof(T) == typeof(object))
{
var itemType = Type.GetType(d.Type) ?? typeof(object);
accessor = _store.GetIdAccessor(itemType, "Id");
item = (T)Store.Configuration.ContentSerializer.Deserialize(d.Content, itemType);
}
else
{
item = (T)Store.Configuration.ContentSerializer.Deserialize(d.Content, typeof(T));
}
if (accessor != null)
{
@@ -1432,6 +1432,29 @@ public void ShouldSaveBigDocuments()
}
}
[Fact]
public async Task ShouldResolveTypes()
{
using (var session = _store.CreateSession())
{
var bill = new Person
{
Firstname = "Bill"
};
session.Save(bill);
}
using (var session = _store.CreateSession())
{
var bill = await session.Query().Any().FirstOrDefaultAsync();
Assert.NotNull(bill);
Assert.IsType<Person>(bill);
Assert.Equal("Bill", ((Person)bill).Firstname);
}
}
[Fact]
public async Task ShouldSavePolymorphicProperties()
{

0 comments on commit b7fad0b

Please sign in to comment.