Skip to content

Commit 524d06a

Browse files
authored
Obsolete EnumerableExtensions (#1465)
1 parent dfe5dec commit 524d06a

File tree

9 files changed

+35
-124
lines changed

9 files changed

+35
-124
lines changed

src/NHibernate.Test/Async/NHSpecificTest/NH3050/Fixture.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ where personIds.Contains(person.Id)
127127
var softReferenceCache = (IEnumerable) field.GetValue(cache);
128128

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

132132
// Setup an action that will be run on another thread and that will do nothing more than clearing the cache as long
133133
// as the value stored behind the cachekey is not of type ExpandedQueryExpression, which triggers the error.
@@ -172,4 +172,4 @@ where personIds.Contains(person.Id)
172172
}
173173
}
174174
}
175-
}
175+
}

src/NHibernate.Test/NHSpecificTest/NH3050/Fixture.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ where personIds.Contains(person.Id)
117117
var softReferenceCache = (IEnumerable) field.GetValue(cache);
118118

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

122122
// Setup an action that will be run on another thread and that will do nothing more than clearing the cache as long
123123
// as the value stored behind the cachekey is not of type ExpandedQueryExpression, which triggers the error.
@@ -162,4 +162,4 @@ where personIds.Contains(person.Id)
162162
}
163163
}
164164
}
165-
}
165+
}

src/NHibernate.Test/UtilityTest/EnumerableExtensionsTests/AnyExtensionTests.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
namespace NHibernate.Test.UtilityTest.EnumerableExtensionsTests
77
{
8+
//Since v5.1
9+
[Obsolete]
810
[TestFixture]
911
public class AnyExtensionTests
1012
{
@@ -72,4 +74,4 @@ public void WhenDisposableListThenCallDispose()
7274
Assert.That(disposeCalled, Is.True);
7375
}
7476
}
75-
}
77+
}

src/NHibernate.Test/UtilityTest/EnumerableExtensionsTests/FirstExtensionTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
namespace NHibernate.Test.UtilityTest.EnumerableExtensionsTests
77
{
8+
//Since v5.1
9+
[Obsolete]
810
[TestFixture]
911
public class FirstExtensionTests
1012
{

src/NHibernate.Test/UtilityTest/EnumerableExtensionsTests/FirstOrNullExtensionTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
namespace NHibernate.Test.UtilityTest.EnumerableExtensionsTests
77
{
8+
//Since v5.1
9+
[Obsolete]
810
[TestFixture]
911
public class FirstOrNullExtensionTests
1012
{

src/NHibernate/Async/Engine/Cascade.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ private Task CascadePropertyAsync(object parent, object child, IType type, Casca
125125
{
126126
EntityType entityType = (EntityType)type;
127127
object loadedValue;
128-
if (!componentPathStack.Any())
128+
if (componentPathStack.Count == 0)
129129
{
130130
// association defined on entity
131131
loadedValue = entry.GetLoadedValue(propertyName);
@@ -306,4 +306,4 @@ private async Task DeleteOrphansAsync(string entityName, IPersistentCollection p
306306
}
307307
}
308308
}
309-
}
309+
}

src/NHibernate/Engine/Cascade.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using System.Collections;
2-
2+
using System.Collections.Generic;
33
using NHibernate.Collection;
44
using NHibernate.Event;
55
using NHibernate.Persister.Collection;
@@ -76,7 +76,7 @@ public sealed partial class Cascade
7676
private readonly IEventSource eventSource;
7777
private readonly CascadingAction action;
7878

79-
private readonly Stack componentPathStack = new Stack();
79+
private readonly Stack<string> componentPathStack = new Stack<string>();
8080

8181
public Cascade(CascadingAction action, CascadePoint point, IEventSource eventSource)
8282
{
@@ -172,7 +172,7 @@ private void CascadeProperty(object parent, object child, IType type, CascadeSty
172172
{
173173
EntityType entityType = (EntityType)type;
174174
object loadedValue;
175-
if (!componentPathStack.Any())
175+
if (componentPathStack.Count == 0)
176176
{
177177
// association defined on entity
178178
loadedValue = entry.GetLoadedValue(propertyName);
@@ -335,4 +335,4 @@ private void DeleteOrphans(string entityName, IPersistentCollection pc)
335335
}
336336
}
337337
}
338-
}
338+
}

src/NHibernate/Impl/AbstractQueryImpl.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,7 @@ public IQuery SetParameterList(string name, IEnumerable vals, IType type)
719719
{
720720
throw new ArgumentNullException("type","Can't determine the type of parameter-list elements.");
721721
}
722-
if(!vals.Any())
722+
if(!vals.Cast<object>().Any())
723723
{
724724
throw new QueryException(string.Format("An empty parameter-list generates wrong SQL; parameter name '{0}'", name));
725725
}
@@ -740,7 +740,7 @@ public IQuery SetParameterList(string name, IEnumerable vals)
740740
return this;
741741
}
742742

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

746746
return this;
Lines changed: 16 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -1,138 +1,43 @@
11
using System;
22
using System.Collections;
33
using System.Collections.Generic;
4+
using System.Linq;
45

56
namespace NHibernate.Util
67
{
8+
//Since v5.1
9+
[Obsolete("This class has no more usages and will be removed in next major version.")]
710
public static class EnumerableExtensions
811
{
12+
//Since v5.1
13+
[Obsolete("Please use Enumerable.Any<T>(IEnumerable<T>) instead.")]
914
public static bool Any(this IEnumerable source)
1015
{
11-
if (source == null)
12-
{
13-
throw new ArgumentNullException("source");
14-
}
15-
using (DisposableEnumerator enumerator = source.GetDisposableEnumerator())
16-
{
17-
if (enumerator.MoveNext())
18-
{
19-
return true;
20-
}
21-
}
22-
return false;
16+
return Enumerable.Any(source.Cast<object>());
2317
}
2418

19+
//Since v5.1
20+
[Obsolete("Please use Enumerable.First<T>(IEnumerable<T>) instead.")]
2521
public static object First(this IEnumerable source)
2622
{
27-
if (source == null)
28-
{
29-
throw new ArgumentNullException("source");
30-
}
31-
IList collection = source as IList;
32-
if (collection != null)
33-
{
34-
if (collection.Count > 0)
35-
{
36-
return collection[0];
37-
}
38-
}
39-
else
40-
{
41-
using (DisposableEnumerator enumerator = source.GetDisposableEnumerator())
42-
{
43-
if (enumerator.MoveNext())
44-
{
45-
return enumerator.Current;
46-
}
47-
}
48-
}
49-
throw new InvalidOperationException("Sequence contains no elements");
23+
return Enumerable.First(source.Cast<object>());
5024
}
5125

26+
//Since v5.1
27+
[Obsolete("Please use Enumerable.FirstOrDefault<T>(IEnumerable<T>) instead.")]
5228
public static object FirstOrNull(this IEnumerable source)
5329
{
54-
if (source == null)
55-
{
56-
throw new ArgumentNullException("source");
57-
}
58-
IList collection = source as IList;
59-
if (collection != null)
60-
{
61-
if (collection.Count > 0)
62-
{
63-
return collection[0];
64-
}
65-
}
66-
else
67-
{
68-
using (DisposableEnumerator enumerator = source.GetDisposableEnumerator())
69-
{
70-
if (enumerator.MoveNext())
71-
{
72-
return enumerator.Current;
73-
}
74-
}
75-
}
76-
return null;
30+
return Enumerable.FirstOrDefault(source.Cast<object>());
7731
}
7832

33+
//Since v5.1
34+
[Obsolete("Please use a loop instead.")]
7935
public static void ForEach<T>(this IEnumerable<T> query, Action<T> method)
8036
{
81-
foreach (T item in query)
37+
foreach (var item in query)
8238
{
8339
method(item);
8440
}
8541
}
86-
87-
private static DisposableEnumerator GetDisposableEnumerator(this IEnumerable source)
88-
{
89-
return new DisposableEnumerator(source);
90-
}
91-
92-
#region Nested type: DisposableEnumerator
93-
94-
internal class DisposableEnumerator : IDisposable, IEnumerator
95-
{
96-
private readonly IEnumerator wrapped;
97-
98-
public DisposableEnumerator(IEnumerable source)
99-
{
100-
wrapped = source.GetEnumerator();
101-
}
102-
103-
#region IDisposable Members
104-
105-
public void Dispose()
106-
{
107-
var disposable = wrapped as IDisposable;
108-
if (disposable != null)
109-
{
110-
disposable.Dispose();
111-
}
112-
}
113-
114-
#endregion
115-
116-
#region IEnumerator Members
117-
118-
public bool MoveNext()
119-
{
120-
return wrapped.MoveNext();
121-
}
122-
123-
public void Reset()
124-
{
125-
wrapped.Reset();
126-
}
127-
128-
public object Current
129-
{
130-
get { return wrapped.Current; }
131-
}
132-
133-
#endregion
134-
}
135-
136-
#endregion
13742
}
138-
}
43+
}

0 commit comments

Comments
 (0)