From efcc1db1359e77270a48d1cf37dcc9cd21e0abaa Mon Sep 17 00:00:00 2001 From: Alexis Nowikowski Date: Fri, 7 Apr 2017 23:58:18 +0200 Subject: [PATCH] Fixed issue #12 (#13) --- .../LazyCollection.cs | 49 +++++++------------ .../LazyReference.cs | 21 +++++--- 2 files changed, 32 insertions(+), 38 deletions(-) diff --git a/src/Microsoft.EntityFrameworkCore.LazyLoading/LazyCollection.cs b/src/Microsoft.EntityFrameworkCore.LazyLoading/LazyCollection.cs index 5d8e15c..50dc443 100644 --- a/src/Microsoft.EntityFrameworkCore.LazyLoading/LazyCollection.cs +++ b/src/Microsoft.EntityFrameworkCore.LazyLoading/LazyCollection.cs @@ -15,7 +15,7 @@ public sealed class LazyCollection : IList private readonly DbContext _ctx; private readonly string _collectionName; private readonly object _parent; - private readonly IList _entries = new List(); + private IList _entries = new List(); public LazyCollection(DbContext ctx, object parent, string collectionName) { @@ -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() as IConcurrencyDetector; + try + { + var concurrencyDetector = + _ctx.GetService() 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() .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 IEnumerable.GetEnumerator() @@ -161,11 +154,5 @@ public override string ToString() EnsureLoaded(); return _entries.ToString(); } - - public override int GetHashCode() - { - EnsureLoaded(); - return _entries.GetHashCode(); - } } } diff --git a/src/Microsoft.EntityFrameworkCore.LazyLoading/LazyReference.cs b/src/Microsoft.EntityFrameworkCore.LazyLoading/LazyReference.cs index e76cedd..54afe49 100644 --- a/src/Microsoft.EntityFrameworkCore.LazyLoading/LazyReference.cs +++ b/src/Microsoft.EntityFrameworkCore.LazyLoading/LazyReference.cs @@ -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() as IConcurrencyDetector; + try + { + var concurrencyDetector = + _ctx.GetService() 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; }