CSLA.NET 6.0.0
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
ObjectAdapter.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="ObjectAdapter.cs" company="Marimer LLC">
3// Copyright (c) Marimer LLC. All rights reserved.
4// Website: https://cslanet.com
5// </copyright>
6// <summary>An ObjectAdapter is used to convert data in an object </summary>
7//-----------------------------------------------------------------------
8using System;
9using System.Collections;
10using System.Collections.Generic;
11using System.Data;
12using System.ComponentModel;
13using System.Reflection;
14using Csla.Properties;
15
16namespace Csla.Data
17{
18
23 public class ObjectAdapter
24 {
35 public void Fill(DataSet ds, object source)
36 {
37 string className = source.GetType().Name;
38 Fill(ds, className, source);
39 }
40
51 public void Fill(DataSet ds, string tableName, object source)
52 {
53 DataTable dt;
54 bool exists;
55
56 dt = ds.Tables[tableName];
57 exists = (dt != null);
58
59 if (!exists)
60 dt = new DataTable(tableName);
61
62 Fill(dt, source);
63
64 if (!exists)
65 ds.Tables.Add(dt);
66 }
67
73 public void Fill(DataTable dt, object source)
74 {
75 if (source == null)
76 throw new ArgumentException(Resources.NothingNotValid);
77
78 // get the list of columns from the source
79 List<string> columns = GetColumns(source);
80 if (columns.Count < 1) return;
81
82 // create columns in DataTable if needed
83 foreach (string column in columns)
84 if (!dt.Columns.Contains(column))
85 dt.Columns.Add(column);
86
87 // get an IList and copy the data
88 CopyData(dt, GetIList(source), columns);
89 }
90
91 #region DataCopyIList
92
93 private IList GetIList(object source)
94 {
95 if (source is IListSource)
96 return ((IListSource)source).GetList();
97 else if (source is IList)
98 return source as IList;
99 else
100 {
101 // this is a regular object - create a list
102 ArrayList col = new ArrayList();
103 col.Add(source);
104 return col;
105 }
106 }
107
108 [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
109 private void CopyData(
110 DataTable dt, IList ds, List<string> columns)
111 {
112 // load the data into the DataTable
113 dt.BeginLoadData();
114 for (int index = 0; index < ds.Count; index++)
115 {
116 DataRow dr = dt.NewRow();
117 foreach (string column in columns)
118 {
119 try
120 {
121 dr[column] = GetField(ds[index], column);
122 }
123 catch (Exception ex)
124 {
125 dr[column] = ex.Message;
126 }
127 }
128 dt.Rows.Add(dr);
129 }
130 dt.EndLoadData();
131 }
132
133 #endregion
134
135 #region GetColumns
136
137 private List<string> GetColumns(object source)
138 {
139 List<string> result;
140 // first handle DataSet/DataTable
141 object innerSource;
142 IListSource iListSource = source as IListSource;
143 if (iListSource != null)
144 innerSource = iListSource.GetList();
145 else
146 innerSource = source;
147
148 DataView dataView = innerSource as DataView;
149 if (dataView != null)
150 result = ScanDataView(dataView);
151 else
152 {
153 // now handle lists/arrays/collections
154 IEnumerable iEnumerable = innerSource as IEnumerable;
155 if (iEnumerable != null)
156 {
157 Type childType = Utilities.GetChildItemType(
158 innerSource.GetType());
159 result = ScanObject(childType);
160 }
161 else
162 {
163 // the source is a regular object
164 result = ScanObject(innerSource.GetType());
165 }
166 }
167 return result;
168 }
169
170 private List<string> ScanDataView(DataView ds)
171 {
172 List<string> result = new List<string>();
173 for (int field = 0; field < ds.Table.Columns.Count; field++)
174 result.Add(ds.Table.Columns[field].ColumnName);
175 return result;
176 }
177
178 private List<string> ScanObject(Type sourceType)
179 {
180 List<string> result = new List<string>();
181
182 if (sourceType != null)
183 {
184 // retrieve a list of all public properties
185 PropertyInfo[] props = sourceType.GetProperties();
186 if (props.Length >= 0)
187 for (int column = 0; column < props.Length; column++)
188 if (props[column].CanRead)
189 result.Add(props[column].Name);
190
191 // retrieve a list of all public fields
192 FieldInfo[] fields = sourceType.GetFields();
193 if (fields.Length >= 0)
194 for (int column = 0; column < fields.Length; column++)
195 result.Add(fields[column].Name);
196 }
197 return result;
198 }
199
200 #endregion
201
202 #region GetField
203
204 private static string GetField(object obj, string fieldName)
205 {
206 string result;
207 DataRowView dataRowView = obj as DataRowView;
208 if (dataRowView != null)
209 {
210 // this is a DataRowView from a DataView
211 result = dataRowView[fieldName].ToString();
212 }
213 else if (obj is ValueType && obj.GetType().IsPrimitive)
214 {
215 // this is a primitive value type
216 result = obj.ToString();
217 }
218 else
219 {
220 string tmp = obj as string;
221 if (tmp != null)
222 {
223 // this is a simple string
224 result = (string)obj;
225 }
226 else
227 {
228 // this is an object or Structure
229 try
230 {
231 Type sourceType = obj.GetType();
232
233 // see if the field is a property
234 PropertyInfo prop = sourceType.GetProperty(fieldName);
235
236 if ((prop == null) || (!prop.CanRead))
237 {
238 // no readable property of that name exists -
239 // check for a field
240 FieldInfo field = sourceType.GetField(fieldName);
241 if (field == null)
242 {
243 // no field exists either, throw an exception
244 throw new DataException(
246 " " + fieldName);
247 }
248 else
249 {
250 // got a field, return its value
251 result = field.GetValue(obj).ToString();
252 }
253 }
254 else
255 {
256 // found a property, return its value
257 result = prop.GetValue(obj, null).ToString();
258 }
259 }
260 catch (Exception ex)
261 {
262 throw new DataException(
264 " " + fieldName, ex);
265 }
266 }
267 }
268 return result;
269 }
270
271 #endregion
272
273 }
274}
An ObjectAdapter is used to convert data in an object or collection into a DataTable.
void Fill(DataSet ds, object source)
Fills the DataSet with data from an object or collection.
void Fill(DataTable dt, object source)
Fills a DataTable with data values from an object or collection.
void Fill(DataSet ds, string tableName, object source)
Fills the DataSet with data from an object or collection.
A strongly-typed resource class, for looking up localized strings, etc.
static string ErrorReadingValueException
Looks up a localized string similar to Error reading value:.
static string NoSuchValueExistsException
Looks up a localized string similar to No such value exists:.
static string NothingNotValid
Looks up a localized string similar to Argument must not be Nothing.