Fixing the loader

This commit is contained in:
Zoe Roux 2021-02-19 22:22:57 +01:00
parent 3e0c0db79c
commit 56c7339816
3 changed files with 23 additions and 13 deletions

View File

@ -65,6 +65,10 @@ namespace Kyoo.Controllers
where T : class, IResource
where T2 : class;
Task Load<T, T2>([NotNull] T obj, Expression<Func<T, IEnumerable<T2>>> member)
where T : class, IResource
where T2 : class;
// Library Items relations
Task<ICollection<LibraryItem>> GetItemsFromLibrary(int id,
Expression<Func<LibraryItem, bool>> where = null,

View File

@ -239,9 +239,17 @@ namespace Kyoo.Controllers
where T : class, IResource
where T2 : class
{
return Task.CompletedTask;
// TODO figure out why setting this method as abstract prevent the app from loading this assembly.
throw new NotImplementedException();
}
public virtual Task Load<T, T2>(T obj, Expression<Func<T, IEnumerable<T2>>> member)
where T : class, IResource
where T2 : class
{
throw new NotImplementedException();
}
public Task<ICollection<Library>> GetLibraries(Expression<Func<Library, bool>> where = null,
Sort<Library> sort = default,
Pagination page = default)

View File

@ -1,10 +1,8 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
namespace Kyoo.Controllers
{
@ -39,22 +37,22 @@ namespace Kyoo.Controllers
_database = database;
}
public override Task Load<T, T2>(T obj, Expression<Func<T, IEnumerable<T2>>> member)
{
if (obj == null)
throw new ArgumentNullException(nameof(obj));
if (!Utility.IsPropertyExpression(member) || member == null)
throw new ArgumentException($"{nameof(member)} is not a property.");
return _database.Entry(obj).Collection(member).LoadAsync();
}
public override Task Load<T, T2>(T obj, Expression<Func<T, T2>> member)
{
if (obj == null)
throw new ArgumentNullException(nameof(obj));
if (!Utility.IsPropertyExpression(member) || member == null)
throw new ArgumentException($"{nameof(member)} is not a property.");
EntityEntry<T> entry = _database.Entry(obj);
if (!typeof(IEnumerable).IsAssignableFrom(typeof(T2)))
return entry.Reference(member).LoadAsync();
// TODO This is totally the wrong thing. We should run entry.Collection<T>(collectionMember).LoadAsync()
// TODO where collectionMember would be member with T2 replaced by it's inner type (IEnumerable<T3>)
Type collectionType = Utility.GetGenericDefinition(typeof(T2), typeof(IEnumerable<>));
return Utility.RunGenericMethod<CollectionEntry>(entry, "Collection", collectionType, member).LoadAsync();
return _database.Entry(obj).Reference(member).LoadAsync();
}
}
}