diff Source/Mapping/DataTableMapper.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/DataTableMapper.cs	Thu Mar 27 21:46:09 2014 +0400
@@ -0,0 +1,75 @@
+using System.Data;
+
+using BLToolkit.Reflection;
+
+namespace BLToolkit.Mapping
+{
+	public class DataTableMapper : IMapDataSourceList, IMapDataDestinationList
+	{
+		public DataTableMapper(DataTable dataTable, DataRowMapper mapper)
+		{
+			_table  = dataTable;
+			_mapper = mapper;
+		}
+
+		private readonly DataTable     _table;
+		private readonly DataRowMapper _mapper;
+		private int                    _currentRow;
+
+		#region IMapDataSourceList Members
+
+		void IMapDataSourceList.InitMapping(InitContext initContext)
+		{
+			initContext.DataSource = _mapper;
+		}
+
+		bool IMapDataSourceList.SetNextDataSource(InitContext initContext)
+		{
+			if (_currentRow >= _table.Rows.Count)
+				return false;
+
+			DataRow row = _table.Rows[_currentRow++];
+
+			if (row.RowState == DataRowState.Deleted)
+				return ((IMapDataSourceList)this).SetNextDataSource(initContext);
+
+			_mapper.DataRow          = row;
+			initContext.SourceObject = row;
+
+			return true;
+		}
+
+		void IMapDataSourceList.EndMapping(InitContext initContext)
+		{
+		}
+
+		#endregion
+
+		#region IMapDataDestinationList Members
+
+		void IMapDataDestinationList.InitMapping(InitContext initContext)
+		{
+		}
+
+		IMapDataDestination IMapDataDestinationList.GetDataDestination(InitContext initContext)
+		{
+			return _mapper;
+		}
+
+		object IMapDataDestinationList.GetNextObject(InitContext initContext)
+		{
+			DataRow row = _table.NewRow();
+
+			_mapper.DataRow = row;
+			_table.Rows.Add(row);
+
+			return row;
+		}
+
+		void IMapDataDestinationList.EndMapping(InitContext initContext)
+		{
+		}
+
+		#endregion
+	}
+}