Removed untyped contructor from ComponentRegistration and add a protected setter.
[castle.git] / ActiveRecord / Castle.ActiveRecord / Framework / SupportingUtils.cs
blob49546e5249ea6e490f6606dedf2f3fbc275008e9
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
2 //
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
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
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
17 using System;
18 using System.Collections;
19 using Iesi.Collections;
21 /// <summary>
22 /// Contains utility methods for dealing with ActiveRecord objects
23 /// and collections.
24 /// Useful for external frameworks.
25 /// </summary>
26 public abstract class SupportingUtils
28 /// <summary>
29 /// Obsolete method, use ActiveRecordMediator or ActiveRecordMediator{T} instead
30 /// </summary>
31 [Obsolete("Use ActiveRecordMediator instead")]
32 public static IList FindAll(Type type)
34 return ActiveRecordMediator.FindAll(type);
37 /// <summary>
38 /// Obsolete method, use ActiveRecordMediator or ActiveRecordMediator{T} instead
39 /// </summary>
40 [Obsolete("Use ActiveRecordMediator instead")]
41 public static object FindByPK(Type type, object id)
43 return ActiveRecordMediator.FindByPrimaryKey(type, id);
46 /// <summary>
47 /// Obsolete method, use ActiveRecordMediator or ActiveRecordMediator{T} instead
48 /// </summary>
49 [Obsolete("Use ActiveRecordMediator instead")]
50 public static object FindByPK(Type type, object id, bool throwOnNotFound)
52 return ActiveRecordMediator.FindByPrimaryKey(type, id, throwOnNotFound);
55 #region BuildArray
57 /// <summary>
58 /// Create an array from an IList.
59 /// </summary>
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);
69 return array;
72 /// <summary>
73 /// Converts the results stored in an <see cref="IEnumerable"/> to an
74 /// strongly-typed array.
75 /// </summary>
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);
85 /// <summary>
86 /// Converts the results stored in an <see cref="IEnumerable"/> to an
87 /// strongly-typed array.
88 /// </summary>
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.
96 /// </param>
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))
117 newList.Add(el);
121 list = newList;
124 ICollection col = list as ICollection;
126 if (col == null)
128 ArrayList newList = new ArrayList();
130 foreach(object item in list)
132 newList.Add(item);
135 col = newList;
138 Array typeSafeArray = Array.CreateInstance(type, col.Count);
140 col.CopyTo(typeSafeArray, 0);
142 return typeSafeArray;
145 #endregion
147 #region BuildObjectArray
149 /// <summary>
150 /// Converts the results stored in an <see cref="IEnumerable"/> to an
151 /// strongly-typed array.
152 /// </summary>
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" />.
156 /// </param>
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))
177 newList.Add(el);
181 Array a = Array.CreateInstance(type, newList.Count);
182 newList.CopyTo(a, 0);
183 return a;
186 #endregion
188 #region BuildObjectArray
190 /// <summary>
191 /// Converts the results stored in an <see cref="IEnumerable"/> to an
192 /// strongly-typed array.
193 /// </summary>
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);
203 #endregion
205 #region BuildArray
208 /// <summary>
209 /// Converts the results stored in an <see cref="IEnumerable"/> to a
210 /// strongly-typed array.
211 /// </summary>
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" />.
218 /// </typeparam>
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);
225 /// <summary>
226 /// Converts the results stored in an <see cref="IEnumerable"/> to an
227 /// strongly-typed array.
228 /// </summary>
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.
236 /// </param>
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);
245 #endregion