This is one of my favorite pieces of code I've stolen borrowed from another site. I've had made some minor code changes and additions from the original, but basically I just need to send in a DataReader after running a sql command and it will return a list of objects of that type. (Very similar to Linq to Sql)
You just need to make sure that anything that is return through the query is defined in the object as a write property.
So here is the List Creator code, which does the creation via reflections:
So in my DAL I might have something like:
You just need to make sure that anything that is return through the query is defined in the object as a write property.
So here is the List Creator code, which does the creation via reflections:
Code Snippet
- public class ListCreator where T : new()
- {
- public List FindAll(IDataReader iDataReader)
- {
- List returnList = new List();
- try
- {
- //need a Type and PropertyInfo object to set properties via reflection
- Type tType = new T().GetType();
- PropertyInfo pInfo;
- //x will hold the instance of until it is added to the list
- T x;
- //use reader to populate list of objects
- while (iDataReader.Read())
- {
- x = new T();
- //set property values
- //for this to work, command’s column names must match property names in object
- for (int i = 0; i < iDataReader.FieldCount; i++)
- {
- pInfo = tType.GetProperty(iDataReader.GetName(i));
- pInfo.SetValue(x, (iDataReader[i] == DBNull.Value? null:iDataReader[i]), null);
- }
- //once instance of is populated, add to list
- returnList.Add(x);
- }
- }
- catch (Exception ex)
- {
- Logging.Logging.Error(ex.ToString());
- }
- return returnList;
- }
- }
Code Snippet
- public List<PartsOrdering> GetAllPartsOrdering()
- {
- List<PartsOrdering> li = new List<PartsOrdering>();
- try
- {
- using (SqlConnection cn = new SqlConnection(connString))
- {
- ListCreator<PartsOrdering> PartsOrders = new ListCreator<PartsOrdering>();
- SqlCommand cmd = new SqlCommand("dbo.selPartsOrdering", cn);
- cmd.CommandType = CommandType.StoredProcedure;
- cn.Open();
- li = PartsOrders.FindAll(ExecuteReader(cmd));
- }
- }
- catch (Exception ex)
- {
- Logging.Error(ex.ToString());
- }
- return li;
- }
Code Snippet
- [Serializable]
- public class PartsOrdering : IFormattable
- {
- #region Properties
- [DefaultValue(-1)]
- public int PartsOrderingID
- {
- get;
- set;
- }
- [DefaultValue("")]
- public string ToolID
- {
- get;
- set;
- }
- [DefaultValue("")]
- public string PartNumber
- {
- get;
- set;
- }
- [DefaultValue("")]
- public string Description
- {
- get;
- set;
- }
- [DefaultValue(0)]
- public int Quantity
- {
- get;
- set;
- }
- [DefaultValue(1)]
- public int PartsOrderingLocationID
- {
- get;
- set;
- }
- [DefaultValue(1)]
- public int PartsOrderingUrgencyID
- {
- get;
- set;
- }
- [DefaultValue("")]
- public string Comment
- {
- get;
- set;
- }
- public DateTime RequestedDateTime
- {
- get;
- set;
- }
- [DefaultValue("")]
- public string RequestedUser
- {
- get;
- set;
- }
- public string LocationName
- {
- get
- {
- List<PartsOrderingLocation> locations = new PartsOrderingLocation().List();
- return locations.Where(n => n.PartsOrderingLocationID.Equals(this.PartsOrderingLocationID)).Select(n => n.Name).First();
- }
- set
- {
- List<PartsOrderingLocation> locations = new PartsOrderingLocation().List();
- this.PartsOrderingLocationID = locations.Where(n => n.Name.Equals(value)).Select(n => n.PartsOrderingLocationID).First();
- }
- }
- public string UrgencyName
- {
- get
- {
- List<PartsOrderingUrgency> locations = new PartsOrderingUrgency().List();
- return locations.Where(n => n.PartsOrderingUrgencyID.Equals(this.PartsOrderingUrgencyID)).Select(n => n.Name).First();
- }
- set
- {
- List<PartsOrderingUrgency> locations = new PartsOrderingUrgency().List();
- this.PartsOrderingUrgencyID = locations.Where(n => n.Name.Equals(value)).Select(n => n.PartsOrderingUrgencyID).First();
- }
- }
- public int UrgencyRank
- {
- get
- {
- List<PartsOrderingUrgency> locations = new PartsOrderingUrgency().List();
- return locations.Where(n => n.PartsOrderingUrgencyID.Equals(this.PartsOrderingUrgencyID)).Select(n => n.Rank).First();
- }
- }
- [DefaultValue(-1)]
- public int PartsOrderingGroupID
- {
- get;
- set;
- }
- [DefaultValue(true)]
- public bool IsActive
- {
- get;
- set;
- }
- }