mirror of
https://github.com/lidarr/lidarr.git
synced 2025-08-14 10:47:08 -07:00
added condition to lazy loading of relationship objects
no db calls will be made if foreign key is 0.
This commit is contained in:
parent
f5a68782ce
commit
28b47b415f
4 changed files with 56 additions and 34 deletions
|
@ -70,14 +70,15 @@ namespace Marr.Data
|
|||
internal class LazyLoaded<TParent, TChild> : LazyLoaded<TChild>
|
||||
{
|
||||
private TParent _parent;
|
||||
private Func<IDataMapper, TParent, TChild> _query;
|
||||
|
||||
private Func<IDataMapper> _dbCreator;
|
||||
|
||||
internal LazyLoaded(Func<IDataMapper, TParent, TChild> query)
|
||||
: base()
|
||||
private readonly Func<IDataMapper, TParent, TChild> _query;
|
||||
private readonly Func<TParent, bool> _condition;
|
||||
|
||||
internal LazyLoaded(Func<IDataMapper, TParent, TChild> query, Func<TParent, bool> condition = null)
|
||||
{
|
||||
_query = query;
|
||||
_condition = condition;
|
||||
}
|
||||
|
||||
public LazyLoaded(TChild val)
|
||||
|
@ -107,11 +108,16 @@ namespace Marr.Data
|
|||
{
|
||||
if (!_isLoaded)
|
||||
{
|
||||
using (IDataMapper db = _dbCreator())
|
||||
if (_condition != null && _condition(_parent))
|
||||
{
|
||||
_child = _query(db, _parent);
|
||||
_isLoaded = true;
|
||||
using (IDataMapper db = _dbCreator())
|
||||
{
|
||||
_child = _query(db, _parent);
|
||||
}
|
||||
}
|
||||
|
||||
_child = default(TChild);
|
||||
_isLoaded = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -62,12 +62,13 @@ namespace Marr.Data.Mapping
|
|||
/// </summary>
|
||||
/// <typeparam name="TChild"></typeparam>
|
||||
/// <param name="query"></param>
|
||||
/// <param name="condition">condition in which a child could exist. eg. avoid call to db if foreign key is 0 or null</param>
|
||||
/// <returns></returns>
|
||||
public RelationshipBuilder<TEntity> LazyLoad<TChild>(Func<IDataMapper, TEntity, TChild> query)
|
||||
public RelationshipBuilder<TEntity> LazyLoad<TChild>(Func<IDataMapper, TEntity, TChild> query, Func<TEntity, bool> condition = null)
|
||||
{
|
||||
AssertCurrentPropertyIsSet();
|
||||
|
||||
Relationships[_currentPropertyName].LazyLoaded = new LazyLoaded<TEntity, TChild>(query);
|
||||
Relationships[_currentPropertyName].LazyLoaded = new LazyLoaded<TEntity, TChild>(query, condition);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue