1
0
Fork 0
mirror of https://github.com/Sonarr/Sonarr synced 2024-12-27 02:07:41 +00: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:
kay.one 2013-05-21 22:30:54 -07:00
parent f5a68782ce
commit 28b47b415f
4 changed files with 56 additions and 34 deletions

View file

@ -70,14 +70,15 @@ namespace Marr.Data
internal class LazyLoaded<TParent, TChild> : LazyLoaded<TChild> internal class LazyLoaded<TParent, TChild> : LazyLoaded<TChild>
{ {
private TParent _parent; private TParent _parent;
private Func<IDataMapper, TParent, TChild> _query;
private Func<IDataMapper> _dbCreator; private Func<IDataMapper> _dbCreator;
internal LazyLoaded(Func<IDataMapper, TParent, TChild> query) private readonly Func<IDataMapper, TParent, TChild> _query;
: base() private readonly Func<TParent, bool> _condition;
internal LazyLoaded(Func<IDataMapper, TParent, TChild> query, Func<TParent, bool> condition = null)
{ {
_query = query; _query = query;
_condition = condition;
} }
public LazyLoaded(TChild val) public LazyLoaded(TChild val)
@ -107,11 +108,16 @@ namespace Marr.Data
{ {
if (!_isLoaded) if (!_isLoaded)
{ {
using (IDataMapper db = _dbCreator()) if (_condition != null && _condition(_parent))
{ {
_child = _query(db, _parent); using (IDataMapper db = _dbCreator())
_isLoaded = true; {
_child = _query(db, _parent);
}
} }
_child = default(TChild);
_isLoaded = true;
} }
} }

View file

@ -62,12 +62,13 @@ namespace Marr.Data.Mapping
/// </summary> /// </summary>
/// <typeparam name="TChild"></typeparam> /// <typeparam name="TChild"></typeparam>
/// <param name="query"></param> /// <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> /// <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(); AssertCurrentPropertyIsSet();
Relationships[_currentPropertyName].LazyLoaded = new LazyLoaded<TEntity, TChild>(query); Relationships[_currentPropertyName].LazyLoaded = new LazyLoaded<TEntity, TChild>(query, condition);
return this; return this;
} }

View file

@ -4,6 +4,7 @@ using System.Linq;
using FizzWare.NBuilder; using FizzWare.NBuilder;
using FluentAssertions; using FluentAssertions;
using NUnit.Framework; using NUnit.Framework;
using NzbDrone.Core.MediaFiles;
using NzbDrone.Core.Qualities; using NzbDrone.Core.Qualities;
using NzbDrone.Core.Test.Framework; using NzbDrone.Core.Test.Framework;
using NzbDrone.Core.Tv; using NzbDrone.Core.Tv;
@ -72,29 +73,41 @@ namespace NzbDrone.Core.Test.Datastore
loadedSeries.Covers.Value.Should().HaveSameCount(covers); loadedSeries.Covers.Value.Should().HaveSameCount(covers);
}*/ }*/
// [Test] [Test]
// public void one_to_one() public void one_to_one()
// { {
// var episode = Builder<Episode>.CreateNew() var episodeFile = Builder<EpisodeFile>.CreateNew()
// .With(c => c.Id = 0) .BuildNew();
// .Build();
// Db.Insert(episodeFile);
// Db.Insert(episode);
// var episode = Builder<Episode>.CreateNew()
// .With(c => c.EpisodeFileId = episodeFile.Id)
// var history = Builder<History.History>.CreateNew() .BuildNew();
// .With(c => c.Id = 0)
// .With(c => c.EpisodeId = episode.Id) Db.Insert(episode);
// .With(c => c.Quality = new QualityModel())
// .Build();
//
// Db.Insert(history);
//
// var loadedEpisode = Db.Single<History.History>().Episode.Value; var loadedEpisodeFile = Db.Single<Episode>().EpisodeFile.Value;
//
// loadedEpisode.Should().NotBeNull(); loadedEpisodeFile.Should().NotBeNull();
// loadedEpisode.ShouldHave().AllProperties().But(c => c.SeriesTitle).EqualTo(episode); loadedEpisodeFile.ShouldHave().AllProperties().But(c => c.DateAdded).EqualTo(episodeFile);
// } }
[Test]
public void one_to_one_should_not_query_db_if_foreign_key_is_zero()
{
var episode = Builder<Episode>.CreateNew()
.With(c => c.EpisodeFileId = 0)
.BuildNew();
Db.Insert(episode);
Db.Single<Episode>().EpisodeFile.Value.Should().BeNull();
}
[Test] [Test]

View file

@ -13,8 +13,10 @@ namespace NzbDrone.Core.Datastore
where TChild : ModelBase where TChild : ModelBase
{ {
return relationshipBuilder.For(portalExpression.GetMemberName()) return relationshipBuilder.For(portalExpression.GetMemberName())
.LazyLoad((db, parent) => db.Query<TChild>() .LazyLoad(
.SingleOrDefault(c => c.Id == childIdSelector(parent))); query: (db, parent) => db.Query<TChild>().SingleOrDefault(c => c.Id == childIdSelector(parent)),
condition: parent => childIdSelector(parent) > 0
);
} }