public async void LazyLoadCollection() { IEnumerable <LazyObject> paramObjs = Faker.RandomCollection(LazyObject.Random); // Setup DB & LazyLoader SQLiteConnectionAsync db = new SQLiteConnectionAsync(new LazyObjectDb()); DbLazyLoad <LazyObject> lazyLoader = await DbLazyLoad <LazyObject> .GetOrCreateInstanceAsync(db).ConfigureAwait(true); await db.InsertAllAsync(paramObjs).ConfigureAwait(true); // Test shallow loading // ToList() is necessary, as db.Table<T> return a custom Enumerable // implementation which apparently doesn't allow for true references IEnumerable <LazyObject> fetchedObjs = await db.Table <LazyObject>().ShallowLoad(lazyLoader).ToListAsync().ConfigureAwait(true); fetchedObjs.Should().OnlyContain(lo => lo.Value == null && lo.Id > 0); fetchedObjs.ShouldAllBeEquivalentTo( paramObjs, config => config.Including(ctx => ctx.SelectedMemberPath.EndsWith(".Id")) .Including(ctx => ctx.SelectedMemberPath.EndsWith(".Idx"))); // Test further loading IEnumerable <LazyObject> furtherObjs = await db.Table <LazyObject>().FurtherLoad(lazyLoader).ToListAsync().ConfigureAwait(true); furtherObjs.Should().OnlyContain(lo => lo.Value != null); lazyLoader.UpdateFromFurtherLoad(fetchedObjs, furtherObjs, lo => lo.Id); fetchedObjs.ShouldAllBeEquivalentTo( furtherObjs, config => config.Including(ctx => ctx.SelectedMemberPath.EndsWith(".Value"))); fetchedObjs.ShouldAllBeEquivalentTo( paramObjs, config => config.Including(ctx => ctx.SelectedMemberPath.EndsWith(".Id")) .Including(ctx => ctx.SelectedMemberPath.EndsWith(".Idx"))); // Test further unloading foreach (LazyObject fetchedObj in fetchedObjs) { lazyLoader.LazyUnload(fetchedObj); } fetchedObjs.Should().OnlyContain(lo => lo.Value == null && lo.Id > 0); }
/// <summary> /// Initializes a new instance of the <see cref="AsyncDbListBase{T}" /> class. /// </summary> /// <param name="db">Database instance</param> /// <param name="lazyLoad">Whether to use lazy loading</param> protected AsyncDbListBase(IDatabaseAsync db, bool lazyLoad = false) { Status = ReviewStatus.New; LockObject = new object(); LoadCompletionSource = null; Db = db; if (lazyLoad) { LazyLoader = DbLazyLoad <T> .GetOrCreateInstanceAsync(db).Result; } _index = -1; _current = null; Objects = null; }