1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle
.ActiveRecord
.Framework
18 using System
.Collections
;
19 using Iesi
.Collections
;
22 /// Contains utility methods for dealing with ActiveRecord objects
24 /// Useful for external frameworks.
26 public abstract class SupportingUtils
29 /// Obsolete method, use ActiveRecordMediator or ActiveRecordMediator{T} instead
31 [Obsolete("Use ActiveRecordMediator instead")]
32 public static IList
FindAll(Type type
)
34 return ActiveRecordMediator
.FindAll(type
);
38 /// Obsolete method, use ActiveRecordMediator or ActiveRecordMediator{T} instead
40 [Obsolete("Use ActiveRecordMediator instead")]
41 public static object FindByPK(Type type
, object id
)
43 return ActiveRecordMediator
.FindByPrimaryKey(type
, id
);
47 /// Obsolete method, use ActiveRecordMediator or ActiveRecordMediator{T} instead
49 [Obsolete("Use ActiveRecordMediator instead")]
50 public static object FindByPK(Type type
, object id
, bool throwOnNotFound
)
52 return ActiveRecordMediator
.FindByPrimaryKey(type
, id
, throwOnNotFound
);
58 /// Create an array from an IList.
60 /// <param name="targetType">Type of the item in the array.</param>
61 /// <param name="list">The list.</param>
62 /// <returns></returns>
63 public static Array
BuildArray(Type targetType
, IList list
)
65 Array array
= Array
.CreateInstance(targetType
, list
.Count
);
67 list
.CopyTo(array
, 0);
73 /// Converts the results stored in an <see cref="IEnumerable"/> to an
74 /// strongly-typed array.
76 /// <param name="type">The type of the new array</param>
77 /// <param name="list">The source list</param>
78 /// <param name="distinct">If true, only distinct results will be inserted in the array</param>
79 /// <returns>The strongly-typed array</returns>
80 public static Array
BuildArray(Type type
, IEnumerable list
, bool distinct
)
82 return BuildArray(type
, list
, -1, distinct
);
86 /// Converts the results stored in an <see cref="IEnumerable"/> to an
87 /// strongly-typed array.
89 /// <param name="type">The type of the new array</param>
90 /// <param name="list">The source list</param>
91 /// <param name="entityIndex">
92 /// If the HQL clause selects more than one field, or a join is performed
93 /// without using <c>fetch join</c>, the contents of the result list will
94 /// be of type <c>object[]</c>. Specify which index in this array should be used to
95 /// compose the new result array. Use <c>-1</c> to ignore this parameter.
97 /// <param name="distinct">If true, only distinct results will be inserted in the array</param>
98 /// <returns>The strongly-typed array</returns>
99 public static Array
BuildArray(Type type
, IEnumerable list
, int entityIndex
, bool distinct
)
101 // we only need to perform an additional processing if an
102 // entityIndex was specified, or if distinct was chosen.
103 if (distinct
|| entityIndex
!= -1)
105 Set
set = (distinct
? new ListSet() : null);
107 ICollection collection
= list
as ICollection
;
109 IList newList
= collection
!= null ? new ArrayList(collection
.Count
) : new ArrayList();
111 foreach(object item
in list
)
113 object el
= (entityIndex
== -1 ? item
: ((object[]) item
)[entityIndex
]);
115 if (set == null || set.Add(el
))
124 ICollection col
= list
as ICollection
;
128 ArrayList newList
= new ArrayList();
130 foreach(object item
in list
)
138 Array typeSafeArray
= Array
.CreateInstance(type
, col
.Count
);
140 col
.CopyTo(typeSafeArray
, 0);
142 return typeSafeArray
;
147 #region BuildObjectArray
150 /// Converts the results stored in an <see cref="IEnumerable"/> to an
151 /// strongly-typed array.
153 /// <param name="type">
154 /// The class of the object which will be created for each row contained in
155 /// the supplied <paramref name="list" />.
157 /// <param name="list">The source list</param>
158 /// <param name="distinct">If true, only distinct results will be inserted in the array</param>
159 /// <returns>The strongly-typed array</returns>
160 /// <remarks>A good alternative is to use the new <see cref="ImportAttribute"/></remarks>
161 public static Array
BuildObjectArray(Type type
, IEnumerable list
, bool distinct
)
163 // we only need to perform an additional processing if
164 // distinct was chosen.
165 Set
set = (distinct
? new ListSet() : null);
167 ICollection coll
= list
as ICollection
;
168 IList newList
= coll
!= null ? new ArrayList(coll
.Count
) : new ArrayList();
170 foreach(object item
in list
)
172 object[] p
= (item
is object[] ? (object[]) item
: new object[] {item}
);
173 object el
= Activator
.CreateInstance(type
, p
);
175 if (set == null || set.Add(el
))
181 Array a
= Array
.CreateInstance(type
, newList
.Count
);
182 newList
.CopyTo(a
, 0);
188 #region BuildObjectArray
191 /// Converts the results stored in an <see cref="IEnumerable"/> to an
192 /// strongly-typed array.
194 /// <typeparam name="T">The type of the new array</typeparam>
195 /// <param name="list">The source list</param>
196 /// <param name="distinct">If true, only distinct results will be inserted in the array</param>
197 /// <returns>The strongly-typed array</returns>
198 public static T
[] BuildObjectArray
<T
>(IEnumerable list
, bool distinct
)
200 return (T
[]) BuildObjectArray(typeof(T
), list
, distinct
);
209 /// Converts the results stored in an <see cref="IEnumerable"/> to a
210 /// strongly-typed array.
212 /// <param name="list">The source list</param>
213 /// <param name="distinct">If true, only distinct results will be inserted in the array</param>
214 /// <returns>The strongly-typed array</returns>
215 /// <typeparam name="T">
216 /// The class of the object which will be created for each row contained in
217 /// the supplied <paramref name="list" />.
219 /// <remarks>A good alternative is to use the new <see cref="ImportAttribute"/></remarks>
220 public static T
[] BuildArray
<T
>(IEnumerable list
, bool distinct
)
222 return (T
[]) BuildArray(typeof(T
), list
, distinct
);
226 /// Converts the results stored in an <see cref="IEnumerable"/> to an
227 /// strongly-typed array.
229 /// <typeparam name="T">The type of the new array</typeparam>
230 /// <param name="list">The source list</param>
231 /// <param name="entityIndex">
232 /// If the HQL clause selects more than one field, or a join is performed
233 /// without using <c>fetch join</c>, the contents of the result list will
234 /// be of type <c>object[]</c>. Specify which index in this array should be used to
235 /// compose the new result array. Use <c>-1</c> to ignore this parameter.
237 /// <param name="distinct">If true, only distinct results will be inserted in the array</param>
238 /// <returns>The strongly-typed array</returns>
240 public static T
[] BuildArray
<T
>(IEnumerable list
, int? entityIndex
, bool distinct
)
242 return (T
[]) BuildArray(typeof(T
), list
, entityIndex
?? -1, distinct
);