#region Apache Notice
/*****************************************************************************
* $Revision: 374175 $
* $LastChangedDate: 2006-08-15 20:36:48 +0200 (mar., 15 aot 2006) $
* $LastChangedBy: gbayon $
*
* iBATIS.NET Data Mapper
* Copyright (C) 2006/2005 - The Apache Software Foundation
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
********************************************************************************/
#endregion
using System;
using System.Collections;
using IBatisNet.DataMapper.MappedStatements;
using IBatisNet.Common.Utilities.Objects.Members;
using IBatisNet.Common.Logging;
using System.Reflection;
namespace IBatisNet.DataMapper.Proxy{
/// <summary>
/// A lazy list
/// </summary>
[Serializable]
public class LazyList : IList
{
#region Fields
private object _param = null;
private object _target = null;
private ISetAccessor _setAccessor = null;
private ISqlMapper _sqlMap = null;
private string _statementId = string.Empty;
private bool _loaded = false;
private object _loadLock = new object();
private IList _list = null;
private static readonly ILog _logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
#endregion
/// <summary>
/// Resolve the lazy loading.
/// </summary>
/// <param name="methodName">Name of the method.</param>
private void Load(string methodName)
{
if (_logger.IsDebugEnabled)
{
_logger.Debug("Proxyfying call to " + methodName);
}
lock (_loadLock)
{
if (_loaded == false)
{
if (_logger.IsDebugEnabled)
{
_logger.Debug("Proxyfying call, query statement " + _statementId);
}
_list = _sqlMap.QueryForList(_statementId, _param);
_loaded = true;
_setAccessor.Set(_target, _list);
}
}
if (_logger.IsDebugEnabled)
{
_logger.Debug("End of proxyfied call to " + methodName);
}
}
/// <summary>
/// Initializes a new instance of the <see cref="T:LazyList"/> class.
/// </summary>
/// <param name="mappedSatement">The mapped satement.</param>
/// <param name="param">The param.</param>
/// <param name="target">The target.</param>
/// <param name="setAccessor">The set accessor.</param>
public LazyList(IMappedStatement mappedSatement, object param,
object target, ISetAccessor setAccessor)
{
_list = new ArrayList();
_param = param;
_statementId = mappedSatement.Id;
_sqlMap = mappedSatement.SqlMap;
_target = target;
_setAccessor = setAccessor;
}
#region IList Members
/// <summary>
/// Adds an item to the <see cref="T:System.Collections.IList"></see>.
/// </summary>
/// <param name="value">The <see cref="T:System.Object"></see> to add to the <see cref="T:System.Collections.IList"></see>.</param>
/// <returns>
/// The position into which the new element was inserted.
/// </returns>
/// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.IList"></see> is read-only.-or- The <see cref="T:System.Collections.IList"></see> has a fixed size. </exception>
public int Add(object value)
{
Load("Add");
return _list.Add(value);
}
/// <summary>
/// Removes all items from the <see cref="T:System.Collections.IList"></see>.
/// </summary>
/// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.IList"></see> is read-only. </exception>
public void Clear()
{
Load("Clear");
_list.Clear();
}
/// <summary>
/// Determines whether the <see cref="T:System.Collections.IList"></see> contains a specific value.
/// </summary>
/// <param name="value">The <see cref="T:System.Object"></see> to locate in the <see cref="T:System.Collections.IList"></see>.</param>
/// <returns>
/// true if the <see cref="T:System.Object"></see> is found in the <see cref="T:System.Collections.IList"></see>; otherwise, false.
/// </returns>
public bool Contains(object value)
{
Load("Contains");
return _list.Contains(value);
}
/// <summary>
/// Determines the index of a specific item in the <see cref="T:System.Collections.IList"></see>.
/// </summary>
/// <param name="value">The <see cref="T:System.Object"></see> to locate in the <see cref="T:System.Collections.IList"></see>.</param>
/// <returns>
/// The index of value if found in the list; otherwise, -1.
/// </returns>
public int IndexOf(object value)
{
Load("IndexOf");
return _list.IndexOf(value);
}
/// <summary>
/// Inserts an item to the <see cref="T:System.Collections.IList"></see> at the specified index.
/// </summary>
/// <param name="index">The zero-based index at which value should be inserted.</param>
/// <param name="value">The <see cref="T:System.Object"></see> to insert into the <see cref="T:System.Collections.IList"></see>.</param>
/// <exception cref="T:System.ArgumentOutOfRangeException">index is not a valid index in the <see cref="T:System.Collections.IList"></see>. </exception>
/// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.IList"></see> is read-only.-or- The <see cref="T:System.Collections.IList"></see> has a fixed size. </exception>
/// <exception cref="T:System.NullReferenceException">value is null reference in the <see cref="T:System.Collections.IList"></see>.</exception>
public void Insert(int index, object value)
{
Load("Insert");
_list.Insert(index, value);
}
/// <summary>
/// Gets a value indicating whether the <see cref="T:System.Collections.IList"></see> has a fixed size.
/// </summary>
/// <value></value>
/// <returns>true if the <see cref="T:System.Collections.IList"></see> has a fixed size; otherwise, false.</returns>
public bool IsFixedSize
{
get { return false; }
}
/// <summary>
/// Gets a value indicating whether the <see cref="T:System.Collections.IList"></see> is read-only.
/// </summary>
/// <value></value>
/// <returns>true if the <see cref="T:System.Collections.IList"></see> is read-only; otherwise, false.</returns>
public bool IsReadOnly
{
get { return false; }
}
/// <summary>
/// Removes the first occurrence of a specific object from the <see cref="T:System.Collections.IList"></see>.
/// </summary>
/// <param name="value">The <see cref="T:System.Object"></see> to remove from the <see cref="T:System.Collections.IList"></see>.</param>
/// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.IList"></see> is read-only.-or- The <see cref="T:System.Collections.IList"></see> has a fixed size. </exception>
public void Remove(object value)
{
Load("Remove");
_list.Remove(value);
}
/// <summary>
/// Removes the <see cref="T:System.Collections.IList"></see> item at the specified index.
/// </summary>
/// <param name="index">The zero-based index of the item to remove.</param>
/// <exception cref="T:System.ArgumentOutOfRangeException">index is not a valid index in the <see cref="T:System.Collections.IList"></see>. </exception>
/// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.IList"></see> is read-only.-or- The <see cref="T:System.Collections.IList"></see> has a fixed size. </exception>
public void RemoveAt(int index)
{
Load("RemoveAt");
_list.RemoveAt(index);
}
/// <summary>
/// Gets or sets the <see cref="T:Object"/> at the specified index.
/// </summary>
/// <value></value>
public object this[int index]
{
get
{
Load("this");
return _list[index];
}
set
{
Load("this");
_list[index] = value;
}
}
#endregion
#region ICollection Members
/// <summary>
/// Copies the elements of the <see cref="T:System.Collections.ICollection"></see> to an <see cref="T:System.Array"></see>, starting at a particular <see cref="T:System.Array"></see> index.
/// </summary>
/// <param name="array">The one-dimensional <see cref="T:System.Array"></see> that is the destination of the elements copied from <see cref="T:System.Collections.ICollection"></see>. The <see cref="T:System.Array"></see> must have zero-based indexing.</param>
/// <param name="index">The zero-based index in array at which copying begins.</param>
/// <exception cref="T:System.ArgumentNullException">array is null. </exception>
/// <exception cref="T:System.ArgumentOutOfRangeException">index is less than zero. </exception>
/// <exception cref="T:System.ArgumentException">array is multidimensional.-or- index is equal to or greater than the length of array.-or- The number of elements in the source <see cref="T:System.Collections.ICollection"></see> is greater than the available space from index to the end of the destination array. </exception>
/// <exception cref="T:System.InvalidCastException">The type of the source <see cref="T:System.Collections.ICollection"></see> cannot be cast automatically to the type of the destination array. </exception>
public void CopyTo(Array array, int index)
{
Load("CopyTo");
_list.CopyTo(array, index);
}
/// <summary>
/// Gets the number of elements contained in the <see cref="T:System.Collections.ICollection"></see>.
/// </summary>
/// <value></value>
/// <returns>The number of elements contained in the <see cref="T:System.Collections.ICollection"></see>.</returns>
public int Count
{
get
{
Load("Count");
return _list.Count;
}
}
/// <summary>
/// Gets a value indicating whether access to the <see cref="T:System.Collections.ICollection"></see> is synchronized (thread safe).
/// </summary>
/// <value></value>
/// <returns>true if access to the <see cref="T:System.Collections.ICollection"></see> is synchronized (thread safe); otherwise, false.</returns>
public bool IsSynchronized
{
get { return false; }
}
/// <summary>
/// Gets an object that can be used to synchronize access to the <see cref="T:System.Collections.ICollection"></see>.
/// </summary>
/// <value></value>
/// <returns>An object that can be used to synchronize access to the <see cref="T:System.Collections.ICollection"></see>.</returns>
public object SyncRoot
{
get { return this; }
}
#endregion
#region IEnumerable Members
/// <summary>
/// Returns an enumerator that iterates through a collection.
/// </summary>
/// <returns>
/// An <see cref="T:System.Collections.IEnumerator"></see> object that can be used to iterate through the collection.
/// </returns>
public IEnumerator GetEnumerator()
{
Load("Add");
return _list.GetEnumerator();
}
#endregion
}
}
|