Skip to content

Commit

Permalink
Fixed issue #12 (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
darxis authored Apr 7, 2017
1 parent c8c7d28 commit efcc1db
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 38 deletions.
49 changes: 18 additions & 31 deletions src/Microsoft.EntityFrameworkCore.LazyLoading/LazyCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public sealed class LazyCollection<T> : IList<T>
private readonly DbContext _ctx;
private readonly string _collectionName;
private readonly object _parent;
private readonly IList<T> _entries = new List<T>();
private IList<T> _entries = new List<T>();

public LazyCollection(DbContext ctx, object parent, string collectionName)
{
Expand All @@ -26,48 +26,41 @@ public LazyCollection(DbContext ctx, object parent, string collectionName)

private void EnsureLoaded()
{
if (!_loaded && !_loading)
if (_loaded || _loading)
{
_loading = true;
return;
}

_loading = true;

var concurrencyDetector = _ctx.GetService<EntityFrameworkCore.Internal.IConcurrencyDetector>() as IConcurrencyDetector;
try
{
var concurrencyDetector =
_ctx.GetService<EntityFrameworkCore.Internal.IConcurrencyDetector>() as IConcurrencyDetector;
if (concurrencyDetector == null)
{
_loading = false;
throw new LazyLoadingConfigurationException($"Service of type '{typeof(EntityFrameworkCore.Internal.IConcurrencyDetector).FullName}' must be replaced by a service of type '{typeof(IConcurrencyDetector).FullName}' in order to use LazyLoading");
throw new LazyLoadingConfigurationException(
$"Service of type '{typeof(EntityFrameworkCore.Internal.IConcurrencyDetector).FullName}' must be replaced by a service of type '{typeof(IConcurrencyDetector).FullName}' in order to use LazyLoading");
}

if (concurrencyDetector.IsInOperation())
{
_loading = false;
return;
}

var entries = _ctx
_entries = _ctx
.Entry(_parent)
.Collection(_collectionName)
.Query()
.OfType<T>()
.ToList();

/*if (typeof(ILazyLoading).IsAssignableFrom(typeof(T)))
{
foreach (var entry in entries)
{
((ILazyLoading)entry).UseLazyLoading(_ctx);
}
}*/

_entries.Clear();

foreach (var entry in entries)
{
_entries.Add(entry);
}

_loaded = true;
}
finally
{
_loading = false;
}

_loaded = true;
}

IEnumerator<T> IEnumerable<T>.GetEnumerator()
Expand Down Expand Up @@ -161,11 +154,5 @@ public override string ToString()
EnsureLoaded();
return _entries.ToString();
}

public override int GetHashCode()
{
EnsureLoaded();
return _entries.GetHashCode();
}
}
}
21 changes: 14 additions & 7 deletions src/Microsoft.EntityFrameworkCore.LazyLoading/LazyReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,32 @@ public void SetValue(T value)

public T GetValue(object parent, string referenceName)
{
if (_ctx != null && !_isLoaded && !_isLoading)
if (_ctx == null || _isLoaded || _isLoading)
{
_isLoading = true;
return _value;
}

_isLoading = true;

var concurrencyDetector = _ctx.GetService<EntityFrameworkCore.Internal.IConcurrencyDetector>() as IConcurrencyDetector;
try
{
var concurrencyDetector =
_ctx.GetService<EntityFrameworkCore.Internal.IConcurrencyDetector>() as IConcurrencyDetector;
if (concurrencyDetector == null)
{
_isLoading = false;
throw new LazyLoadingConfigurationException($"Service of type '{typeof(EntityFrameworkCore.Internal.IConcurrencyDetector).FullName}' must be replaced by a service of type '{typeof(IConcurrencyDetector).FullName}' in order to use LazyLoading");
throw new LazyLoadingConfigurationException(
$"Service of type '{typeof(EntityFrameworkCore.Internal.IConcurrencyDetector).FullName}' must be replaced by a service of type '{typeof(IConcurrencyDetector).FullName}' in order to use LazyLoading");
}

if (concurrencyDetector.IsInOperation())
{
_isLoading = false;
return _value;
}

_ctx.Entry(parent).Reference(referenceName).Load();

}
finally
{
_isLoading = false;
}

Expand Down

0 comments on commit efcc1db

Please sign in to comment.