Skip to content

Obsolete EnumerableExtensions #1465

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/NHibernate.Test/Async/NHSpecificTest/NH3050/Fixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ where personIds.Contains(person.Id)
var softReferenceCache = (IEnumerable) field.GetValue(cache);

// Since the cache only contains one item, the first one will be our key
var queryPlanCacheKey = ((DictionaryEntry) softReferenceCache.First()).Key;
var queryPlanCacheKey = ((DictionaryEntry) softReferenceCache.Cast<object>().First()).Key;

// Setup an action that will be run on another thread and that will do nothing more than clearing the cache as long
// as the value stored behind the cachekey is not of type ExpandedQueryExpression, which triggers the error.
Expand Down Expand Up @@ -172,4 +172,4 @@ where personIds.Contains(person.Id)
}
}
}
}
}
4 changes: 2 additions & 2 deletions src/NHibernate.Test/NHSpecificTest/NH3050/Fixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ where personIds.Contains(person.Id)
var softReferenceCache = (IEnumerable) field.GetValue(cache);

// Since the cache only contains one item, the first one will be our key
var queryPlanCacheKey = ((DictionaryEntry) softReferenceCache.First()).Key;
var queryPlanCacheKey = ((DictionaryEntry) softReferenceCache.Cast<object>().First()).Key;

// Setup an action that will be run on another thread and that will do nothing more than clearing the cache as long
// as the value stored behind the cachekey is not of type ExpandedQueryExpression, which triggers the error.
Expand Down Expand Up @@ -162,4 +162,4 @@ where personIds.Contains(person.Id)
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

namespace NHibernate.Test.UtilityTest.EnumerableExtensionsTests
{
//Since v5.1
[Obsolete]
[TestFixture]
public class AnyExtensionTests
{
Expand Down Expand Up @@ -72,4 +74,4 @@ public void WhenDisposableListThenCallDispose()
Assert.That(disposeCalled, Is.True);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

namespace NHibernate.Test.UtilityTest.EnumerableExtensionsTests
{
//Since v5.1
[Obsolete]
[TestFixture]
public class FirstExtensionTests
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

namespace NHibernate.Test.UtilityTest.EnumerableExtensionsTests
{
//Since v5.1
[Obsolete]
[TestFixture]
public class FirstOrNullExtensionTests
{
Expand Down
4 changes: 2 additions & 2 deletions src/NHibernate/Async/Engine/Cascade.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ private Task CascadePropertyAsync(object parent, object child, IType type, Casca
{
EntityType entityType = (EntityType)type;
object loadedValue;
if (!componentPathStack.Any())
if (componentPathStack.Count == 0)
{
// association defined on entity
loadedValue = entry.GetLoadedValue(propertyName);
Expand Down Expand Up @@ -306,4 +306,4 @@ private async Task DeleteOrphansAsync(string entityName, IPersistentCollection p
}
}
}
}
}
8 changes: 4 additions & 4 deletions src/NHibernate/Engine/Cascade.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System.Collections;

using System.Collections.Generic;
using NHibernate.Collection;
using NHibernate.Event;
using NHibernate.Persister.Collection;
Expand Down Expand Up @@ -76,7 +76,7 @@ public sealed partial class Cascade
private readonly IEventSource eventSource;
private readonly CascadingAction action;

private readonly Stack componentPathStack = new Stack();
private readonly Stack<string> componentPathStack = new Stack<string>();

public Cascade(CascadingAction action, CascadePoint point, IEventSource eventSource)
{
Expand Down Expand Up @@ -172,7 +172,7 @@ private void CascadeProperty(object parent, object child, IType type, CascadeSty
{
EntityType entityType = (EntityType)type;
object loadedValue;
if (!componentPathStack.Any())
if (componentPathStack.Count == 0)
{
// association defined on entity
loadedValue = entry.GetLoadedValue(propertyName);
Expand Down Expand Up @@ -335,4 +335,4 @@ private void DeleteOrphans(string entityName, IPersistentCollection pc)
}
}
}
}
}
4 changes: 2 additions & 2 deletions src/NHibernate/Impl/AbstractQueryImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,7 @@ public IQuery SetParameterList(string name, IEnumerable vals, IType type)
{
throw new ArgumentNullException("type","Can't determine the type of parameter-list elements.");
}
if(!vals.Any())
if(!vals.Cast<object>().Any())
{
throw new QueryException(string.Format("An empty parameter-list generates wrong SQL; parameter name '{0}'", name));
}
Expand All @@ -740,7 +740,7 @@ public IQuery SetParameterList(string name, IEnumerable vals)
return this;
}

object firstValue = vals.FirstOrNull();
object firstValue = vals.Cast<object>().FirstOrDefault();
SetParameterList(name, vals, firstValue == null ? GuessType(vals.GetCollectionElementType()) : DetermineType(name, firstValue));

return this;
Expand Down
127 changes: 16 additions & 111 deletions src/NHibernate/Util/EnumerableExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,138 +1,43 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace NHibernate.Util
{
//Since v5.1
[Obsolete("This class has no more usages and will be removed in next major version.")]
public static class EnumerableExtensions
{
//Since v5.1
[Obsolete("Please use Enumerable.Any<T>(IEnumerable<T>) instead.")]
public static bool Any(this IEnumerable source)
{
if (source == null)
{
throw new ArgumentNullException("source");
}
using (DisposableEnumerator enumerator = source.GetDisposableEnumerator())
{
if (enumerator.MoveNext())
{
return true;
}
}
return false;
return Enumerable.Any(source.Cast<object>());
}

//Since v5.1
[Obsolete("Please use Enumerable.First<T>(IEnumerable<T>) instead.")]
public static object First(this IEnumerable source)
{
if (source == null)
{
throw new ArgumentNullException("source");
}
IList collection = source as IList;
if (collection != null)
{
if (collection.Count > 0)
{
return collection[0];
}
}
else
{
using (DisposableEnumerator enumerator = source.GetDisposableEnumerator())
{
if (enumerator.MoveNext())
{
return enumerator.Current;
}
}
}
throw new InvalidOperationException("Sequence contains no elements");
return Enumerable.First(source.Cast<object>());
}

//Since v5.1
[Obsolete("Please use Enumerable.FirstOrDefault<T>(IEnumerable<T>) instead.")]
public static object FirstOrNull(this IEnumerable source)
{
if (source == null)
{
throw new ArgumentNullException("source");
}
IList collection = source as IList;
if (collection != null)
{
if (collection.Count > 0)
{
return collection[0];
}
}
else
{
using (DisposableEnumerator enumerator = source.GetDisposableEnumerator())
{
if (enumerator.MoveNext())
{
return enumerator.Current;
}
}
}
return null;
return Enumerable.FirstOrDefault(source.Cast<object>());
}

//Since v5.1
[Obsolete("Please use a loop instead.")]
public static void ForEach<T>(this IEnumerable<T> query, Action<T> method)
{
foreach (T item in query)
foreach (var item in query)
{
method(item);
}
}

private static DisposableEnumerator GetDisposableEnumerator(this IEnumerable source)
{
return new DisposableEnumerator(source);
}

#region Nested type: DisposableEnumerator

internal class DisposableEnumerator : IDisposable, IEnumerator
{
private readonly IEnumerator wrapped;

public DisposableEnumerator(IEnumerable source)
{
wrapped = source.GetEnumerator();
}

#region IDisposable Members

public void Dispose()
{
var disposable = wrapped as IDisposable;
if (disposable != null)
{
disposable.Dispose();
}
}

#endregion

#region IEnumerator Members

public bool MoveNext()
{
return wrapped.MoveNext();
}

public void Reset()
{
wrapped.Reset();
}

public object Current
{
get { return wrapped.Current; }
}

#endregion
}

#endregion
}
}
}