diff Source/Mapping/ObjectListMapper.cs @ 0:f990fcb411a9

Копия текущей версии из github
author cin
date Thu, 27 Mar 2014 21:46:09 +0400
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Source/Mapping/ObjectListMapper.cs	Thu Mar 27 21:46:09 2014 +0400
@@ -0,0 +1,93 @@
+using System.Collections;
+
+using BLToolkit.Reflection;
+
+namespace BLToolkit.Mapping
+{
+	public class ObjectListMapper : IMapDataSourceList, IMapDataDestinationList
+	{
+		public ObjectListMapper(IList list, ObjectMapper objectMapper)
+		{
+			_list   = list;
+			_mapper = objectMapper;
+		}
+
+		private readonly IList        _list;
+		private          ObjectMapper _mapper;
+		private          int          _currentItem;
+
+		#region IMapDataSourceList Members
+
+		void IMapDataSourceList.InitMapping(InitContext initContext)
+		{
+			initContext.DataSource = _mapper;
+		}
+
+		bool IMapDataSourceList.SetNextDataSource(InitContext initContext)
+		{
+			if (_currentItem >= _list.Count)
+				return false;
+
+			initContext.SourceObject = _list[_currentItem];
+			_currentItem++;
+
+			return true;
+		}
+
+		void IMapDataSourceList.EndMapping(InitContext initContext)
+		{
+		}
+
+		#endregion
+
+		#region IMapDataDestinationList Members
+
+		void IMapDataDestinationList.InitMapping(InitContext initContext)
+		{
+			ISupportMapping sm = _list as ISupportMapping;
+
+			if (sm != null)
+			{
+				sm.BeginMapping(initContext);
+
+				if (initContext.ObjectMapper != null && _mapper != initContext.ObjectMapper)
+					_mapper = initContext.ObjectMapper;
+			}
+		}
+
+		IMapDataDestination IMapDataDestinationList.GetDataDestination(InitContext initContext)
+		{
+			return _mapper;
+		}
+
+		private object _currentObject;
+
+		private void AddCurrent()
+		{
+			if (_currentObject != null)
+			{
+				_list.Add(_currentObject);
+				_currentObject = null;
+			}
+		}
+
+		object IMapDataDestinationList.GetNextObject(InitContext initContext)
+		{
+			AddCurrent();
+
+			return _currentObject = _mapper.CreateInstance(initContext);
+		}
+
+		void IMapDataDestinationList.EndMapping(InitContext initContext)
+		{
+			AddCurrent();
+
+			ISupportMapping sm = _list as ISupportMapping;
+
+			if (sm != null)
+				sm.EndMapping(initContext);
+		}
+
+		#endregion
+	}
+}