Skip to content

Commit 13342b3

Browse files
committed
Replace usage of ICollection with IEnumerable where possible.
1 parent b398eeb commit 13342b3

23 files changed

+108
-124
lines changed

src/NHibernate.Test/QueryTest/DetachedQueryFixture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ public bool ShouldIgnoredUnknownNamedParameters
532532
get { return shouldIgnoredUnknownNamedParameters; }
533533
}
534534

535-
public Dictionary<string, ICollection> NamedUntypeListParams
535+
public Dictionary<string, IEnumerable> NamedUntypeListParams
536536
{
537537
get { return namedUntypeListParams; }
538538
}

src/NHibernate/Cache/StandardQueryCache.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public IList Get(QueryKey key, ICacheAssembler[] returnTypes, bool isNaturalKeyL
9999
var timestamp = (long)cacheable[0];
100100

101101
if (Log.IsDebugEnabled)
102-
Log.DebugFormat("Checking query spaces for up-to-dateness [{0}]", StringHelper.CollectionToString((ICollection)spaces));
102+
Log.DebugFormat("Checking query spaces for up-to-dateness [{0}]", StringHelper.CollectionToString(spaces));
103103

104104
if (!isNaturalKeyLookup && !IsUpToDate(spaces, timestamp))
105105
{

src/NHibernate/Collection/Generic/PersistentGenericBag.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class PersistentGenericBag<T> : PersistentBag, IList<T>
3737
public PersistentGenericBag() {}
3838
public PersistentGenericBag(ISessionImplementor session) : base(session) {}
3939

40-
public PersistentGenericBag(ISessionImplementor session, ICollection<T> coll) : base(session, coll as ICollection)
40+
public PersistentGenericBag(ISessionImplementor session, IEnumerable<T> coll) : base(session, coll as ICollection)
4141
{
4242
gbag = coll as IList<T>;
4343
if (gbag == null)

src/NHibernate/Collection/Generic/PersistentGenericIdentifierBag.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public PersistentIdentifierBag() {}
4242

4343
public PersistentIdentifierBag(ISessionImplementor session) : base(session) {}
4444

45-
public PersistentIdentifierBag(ISessionImplementor session, ICollection<T> coll) : base(session, coll as ICollection)
45+
public PersistentIdentifierBag(ISessionImplementor session, IEnumerable<T> coll) : base(session, coll as ICollection)
4646
{
4747
gvalues = coll as IList<T>;
4848
if (gvalues == null)

src/NHibernate/Collection/PersistentList.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ public override bool EntryExists(object entry, int i)
225225

226226
public override bool Equals(object obj)
227227
{
228-
ICollection that = obj as ICollection;
228+
var that = obj as ICollection;
229229
if (that == null)
230230
{
231231
return false;

src/NHibernate/Criterion/ProjectionList.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,10 @@ public string[] GetColumnAliases(string alias, int loc)
120120

121121
public string[] GetColumnAliases(int position, ICriteria criteria, ICriteriaQuery criteriaQuery)
122122
{
123-
IList result = new List<object>(this.Length);
124-
for (int i = 0; i < this.Length; i++)
123+
var result = new List<object>(Length);
124+
for (var i = 0; i < Length; i++)
125125
{
126-
string[] colAliases = ProjectionList.GetColumnAliases(position, criteria, criteriaQuery, this[i]);
126+
var colAliases = GetColumnAliases(position, criteria, criteriaQuery, this[i]);
127127
ArrayHelper.AddAll(result, colAliases);
128128
position += colAliases.Length;
129129
}
@@ -132,7 +132,7 @@ public string[] GetColumnAliases(int position, ICriteria criteria, ICriteriaQuer
132132

133133
public string[] GetColumnAliases(string alias, int position, ICriteria criteria, ICriteriaQuery criteriaQuery)
134134
{
135-
for (int i = 0; i < this.Length; i++)
135+
for (int i = 0; i < Length; i++)
136136
{
137137
string[] result = GetColumnAliases(alias, position, criteria, criteriaQuery, this[i]);
138138
if (result != null) return result;

src/NHibernate/Criterion/RestrictionsExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public static ICriterion ProcessIsInArray(MethodCallExpression methodCallExpress
136136
public static ICriterion ProcessIsInCollection(MethodCallExpression methodCallExpression)
137137
{
138138
ExpressionProcessor.ProjectionInfo projection = ExpressionProcessor.FindMemberProjection(methodCallExpression.Arguments[0]);
139-
ICollection values = (ICollection)ExpressionProcessor.FindValue(methodCallExpression.Arguments[1]);
139+
var values = (ICollection)ExpressionProcessor.FindValue(methodCallExpression.Arguments[1]);
140140
return projection.Create<ICriterion>(s => Restrictions.In(s, values), p => Restrictions.In(p, values));
141141
}
142142

src/NHibernate/Engine/StatefulPersistenceContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ public void Clear()
255255
li.UnsetSession();
256256
}
257257

258-
ICollection collectionEntryArray = IdentityMap.ConcurrentEntries(collectionEntries);
258+
var collectionEntryArray = IdentityMap.ConcurrentEntries(collectionEntries);
259259
foreach (DictionaryEntry entry in collectionEntryArray)
260260
{
261261
((IPersistentCollection)entry.Key).UnsetSession(Session);

src/NHibernate/Engine/TypedValue.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public TypedValue(IType type, object value, EntityMode entityMode)
2121
{
2222
this.type = type;
2323
this.value = value;
24-
ICollection values = value as ICollection;
24+
var values = value as ICollection;
2525
if (!type.IsCollectionType && values != null && !type.ReturnedClass.IsArray)
2626
comparer = new ParameterListComparer(entityMode);
2727
else
@@ -71,7 +71,7 @@ public ParameterListComparer(EntityMode entityMode)
7171
public bool Equals(TypedValue x, TypedValue y)
7272
{
7373
if (y == null) return false;
74-
if (!x.type.ReturnedClass.Equals(y.type.ReturnedClass))
74+
if (x.type.ReturnedClass != y.type.ReturnedClass)
7575
return false;
7676
return IsEquals(x.type, x.value as ICollection, y.value as ICollection);
7777
}
@@ -135,7 +135,7 @@ public DefaultComparer(EntityMode entityMode)
135135
public bool Equals(TypedValue x, TypedValue y)
136136
{
137137
if (y == null) return false;
138-
if (!x.type.ReturnedClass.Equals(y.type.ReturnedClass))
138+
if (x.type.ReturnedClass != y.type.ReturnedClass)
139139
return false;
140140
return x.type.IsEqual(y.value, x.value, entityMode);
141141
}

src/NHibernate/Hql/Classic/QueryTranslator.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -854,16 +854,16 @@ private void RenderSql()
854854

855855
// HQL functions in whereTokens, groupByTokens, havingTokens and orderByTokens aren't rendered
856856
RenderFunctions(whereTokens);
857-
sql.SetWhereTokens((ICollection)whereTokens);
857+
sql.SetWhereTokens(whereTokens);
858858

859859
RenderFunctions(groupByTokens);
860-
sql.SetGroupByTokens((ICollection)groupByTokens);
860+
sql.SetGroupByTokens(groupByTokens);
861861

862862
RenderFunctions(havingTokens);
863-
sql.SetHavingTokens((ICollection)havingTokens);
863+
sql.SetHavingTokens(havingTokens);
864864

865865
RenderFunctions(orderByTokens);
866-
sql.SetOrderByTokens((ICollection)orderByTokens);
866+
sql.SetOrderByTokens(orderByTokens);
867867

868868
fetchedCollections.AddOrderBy(sql);
869869

src/NHibernate/IDetachedQuery.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public interface IDetachedQuery
108108
/// <param name="name">The name of the parameter</param>
109109
/// <param name="vals">A collection of values to list</param>
110110
/// <param name="type">The Hibernate type of the values</param>
111-
IDetachedQuery SetParameterList(string name, ICollection vals, IType type);
111+
IDetachedQuery SetParameterList(string name, IEnumerable vals, IType type);
112112

113113
/// <summary>
114114
/// Bind multiple values to a named query parameter, guessing the Hibernate
@@ -117,7 +117,7 @@ public interface IDetachedQuery
117117
/// </summary>
118118
/// <param name="name">The name of the parameter</param>
119119
/// <param name="vals">A collection of values to list</param>
120-
IDetachedQuery SetParameterList(string name, ICollection vals);
120+
IDetachedQuery SetParameterList(string name, IEnumerable vals);
121121

122122
/// <summary>
123123
/// Bind the property values of the given object to named parameters of the query,

src/NHibernate/IMultiQuery.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ public interface IMultiQuery
177177
/// <param name="vals">A collection of values to list</param>
178178
/// <param name="type">The Hibernate type of the values</param>
179179
/// <returns>The instance for method chain.</returns>
180-
IMultiQuery SetParameterList(string name, ICollection vals, IType type);
180+
IMultiQuery SetParameterList(string name, IEnumerable vals, IType type);
181181

182182
/// <summary>
183183
/// Bind multiple values to a named query parameter, guessing the Hibernate
@@ -187,7 +187,7 @@ public interface IMultiQuery
187187
/// <param name="name">The name of the parameter</param>
188188
/// <param name="vals">A collection of values to list</param>
189189
/// <returns>The instance for method chain.</returns>
190-
IMultiQuery SetParameterList(string name, ICollection vals);
190+
IMultiQuery SetParameterList(string name, IEnumerable vals);
191191

192192
/// <summary>
193193
/// Bind an instance of a <see cref="string" /> to a named parameter

src/NHibernate/Impl/AbstractDetachedQuery.cs

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using NHibernate.Engine;
55
using NHibernate.Proxy;
66
using NHibernate.Transform;
7+
using NHibernate.Type;
78

89
namespace NHibernate.Impl
910
{
@@ -29,7 +30,7 @@ public abstract class AbstractDetachedQuery : IDetachedQuery, IDetachedQueryImpl
2930
// Untyped Parameters
3031
protected readonly Dictionary<int, object> posUntypeParams = new Dictionary<int, object>(4);
3132
protected readonly Dictionary<string, object> namedUntypeParams = new Dictionary<string, object>();
32-
protected readonly Dictionary<string, ICollection> namedUntypeListParams = new Dictionary<string, ICollection>(2);
33+
protected readonly Dictionary<string, IEnumerable> namedUntypeListParams = new Dictionary<string, IEnumerable>(2);
3334

3435
// Optional parameters are used for parameters values from bean.
3536
// The IQuery implementation use the actualNamedParameters to know which property it need.
@@ -112,13 +113,13 @@ public void SetLockMode(string alias, LockMode lockMode)
112113
lockModes[alias] = lockMode;
113114
}
114115

115-
public IDetachedQuery SetParameter(int position, object val, Type.IType type)
116+
public IDetachedQuery SetParameter(int position, object val, IType type)
116117
{
117118
posParams[position] = new TypedValue(type, val, EntityMode.Poco);
118119
return this;
119120
}
120121

121-
public IDetachedQuery SetParameter(string name, object val, Type.IType type)
122+
public IDetachedQuery SetParameter(string name, object val, IType type)
122123
{
123124
if (string.IsNullOrEmpty(name))
124125
throw new ArgumentNullException("name", "Is null or empty.");
@@ -140,15 +141,15 @@ public IDetachedQuery SetParameter(string name, object val)
140141
return this;
141142
}
142143

143-
public IDetachedQuery SetParameterList(string name, ICollection vals, Type.IType type)
144+
public IDetachedQuery SetParameterList(string name, IEnumerable vals, IType type)
144145
{
145146
if (string.IsNullOrEmpty(name))
146147
throw new ArgumentNullException("name", "Is null or empty.");
147148
namedListParams[name] = new TypedValue(type, vals, EntityMode.Poco);
148149
return this;
149150
}
150151

151-
public IDetachedQuery SetParameterList(string name, ICollection vals)
152+
public IDetachedQuery SetParameterList(string name, IEnumerable vals)
152153
{
153154
if (string.IsNullOrEmpty(name))
154155
throw new ArgumentNullException("name", "Is null or empty.");
@@ -437,7 +438,7 @@ protected void SetQueryProperties(IQuery q)
437438
q.SetLockMode(mode.Key, mode.Value);
438439

439440
// Set AbstractQueryImpl property before set parameters
440-
AbstractQueryImpl aqi = q as AbstractQueryImpl;
441+
var aqi = q as AbstractQueryImpl;
441442
if (aqi != null)
442443
aqi.SetIgnoreUknownNamedParameters(shouldIgnoredUnknownNamedParameters);
443444

@@ -446,32 +447,32 @@ protected void SetQueryProperties(IQuery q)
446447
// difference between IQuery and DetachedQuery behaviour.
447448
// In IQuery we don't know who override a param value; in DetachedQuery the direct use of
448449
// a named parameter setter override the param value set by SetProperties(POCO)
449-
foreach (object obj in optionalUntypeParams)
450+
foreach (var obj in optionalUntypeParams)
450451
q.SetProperties(obj);
451452

452453
// Set untyped positional parameters
453-
foreach (KeyValuePair<int, object> pup in posUntypeParams)
454+
foreach (var pup in posUntypeParams)
454455
q.SetParameter(pup.Key, pup.Value);
455456

456457
// Set untyped named parameters
457-
foreach (KeyValuePair<string, object> nup in namedUntypeParams)
458+
foreach (var nup in namedUntypeParams)
458459
q.SetParameter(nup.Key, nup.Value);
459460

460461
// Set untyped named parameters list
461-
foreach (KeyValuePair<string, ICollection> nulp in namedUntypeListParams)
462+
foreach (var nulp in namedUntypeListParams)
462463
q.SetParameterList(nulp.Key, nulp.Value);
463464

464465
// Set typed positional parameters
465-
foreach (KeyValuePair<int, TypedValue> pp in posParams)
466+
foreach (var pp in posParams)
466467
q.SetParameter(pp.Key, pp.Value.Value, pp.Value.Type);
467468

468469
// Set typed named parameters
469-
foreach (KeyValuePair<string, TypedValue> np in namedParams)
470+
foreach (var np in namedParams)
470471
q.SetParameter(np.Key, np.Value.Value, np.Value.Type);
471472

472473
// Set typed named parameters List
473-
foreach (KeyValuePair<string, TypedValue> nlp in namedListParams)
474-
q.SetParameterList(nlp.Key, (ICollection)nlp.Value.Value, nlp.Value.Type);
474+
foreach (var nlp in namedListParams)
475+
q.SetParameterList(nlp.Key, (IEnumerable) nlp.Value.Value, nlp.Value.Type);
475476
}
476477

477478
private void Reset()
@@ -545,32 +546,32 @@ public void CopyTo(IDetachedQuery destination)
545546
/// </remarks>
546547
public void SetParametersTo(IDetachedQuery destination)
547548
{
548-
foreach (object obj in optionalUntypeParams)
549+
foreach (var obj in optionalUntypeParams)
549550
destination.SetProperties(obj);
550551

551552
// Set untyped positional parameters
552-
foreach (KeyValuePair<int, object> pup in posUntypeParams)
553+
foreach (var pup in posUntypeParams)
553554
destination.SetParameter(pup.Key, pup.Value);
554555

555556
// Set untyped named parameters
556-
foreach (KeyValuePair<string, object> nup in namedUntypeParams)
557+
foreach (var nup in namedUntypeParams)
557558
destination.SetParameter(nup.Key, nup.Value);
558559

559560
// Set untyped named parameters list
560-
foreach (KeyValuePair<string, ICollection> nulp in namedUntypeListParams)
561+
foreach (var nulp in namedUntypeListParams)
561562
destination.SetParameterList(nulp.Key, nulp.Value);
562563

563564
// Set typed positional parameters
564-
foreach (KeyValuePair<int, TypedValue> pp in posParams)
565+
foreach (var pp in posParams)
565566
destination.SetParameter(pp.Key, pp.Value.Value, pp.Value.Type);
566567

567568
// Set typed named parameters
568-
foreach (KeyValuePair<string, TypedValue> np in namedParams)
569+
foreach (var np in namedParams)
569570
destination.SetParameter(np.Key, np.Value.Value, np.Value.Type);
570571

571572
// Set typed named parameters List
572-
foreach (KeyValuePair<string, TypedValue> nlp in namedListParams)
573-
destination.SetParameterList(nlp.Key, (ICollection)nlp.Value.Value, nlp.Value.Type);
573+
foreach (var nlp in namedListParams)
574+
destination.SetParameterList(nlp.Key, (IEnumerable) nlp.Value.Value, nlp.Value.Type);
574575
}
575576

576577
void IDetachedQueryImplementor.OverrideInfoFrom(IDetachedQueryImplementor origin)

src/NHibernate/Impl/AbstractQueryImpl.cs

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -640,24 +640,19 @@ public IQuery SetProperties(IDictionary map)
640640
string[] @params = NamedParameters;
641641
for (int i = 0; i < @params.Length; i++)
642642
{
643-
string namedParam = @params[i];
644-
object obj = map[namedParam];
643+
var namedParam = @params[i];
644+
var obj = map[namedParam];
645645
if (obj == null)
646646
{
647647
continue;
648648
}
649-
System.Type retType = obj.GetType();
650-
if (typeof(ICollection).IsAssignableFrom(retType))
651-
{
652-
SetParameterList(namedParam, (ICollection)obj);
653-
}
654-
else if (retType.IsArray)
649+
if (obj is IEnumerable && !(obj is string))
655650
{
656-
SetParameterList(namedParam, (object[])obj);
651+
SetParameterList(namedParam, (IEnumerable) obj);
657652
}
658653
else
659654
{
660-
SetParameter(namedParam, obj, DetermineType(namedParam, retType));
655+
SetParameter(namedParam, obj, DetermineType(namedParam, obj.GetType()));
661656
}
662657
}
663658
return this;
@@ -672,16 +667,12 @@ public IQuery SetProperties(object bean)
672667
string namedParam = @params[i];
673668
try
674669
{
675-
IGetter getter = ReflectHelper.GetGetter(clazz, namedParam, "property");
676-
System.Type retType = getter.ReturnType;
677-
object obj = getter.Get(bean);
678-
if (typeof(ICollection).IsAssignableFrom(retType))
679-
{
680-
SetParameterList(namedParam, (ICollection)obj);
681-
}
682-
else if (retType.IsArray)
670+
var getter = ReflectHelper.GetGetter(clazz, namedParam, "property");
671+
var retType = getter.ReturnType;
672+
var obj = getter.Get(bean);
673+
if (typeof(IEnumerable).IsAssignableFrom(retType) && retType != typeof(string))
683674
{
684-
SetParameterList(namedParam, (Object[])obj);
675+
SetParameterList(namedParam, (IEnumerable) obj);
685676
}
686677
else
687678
{

0 commit comments

Comments
 (0)