List <T> IXapPoco.SelectList <T>(T obj) { try { List <T> lst = new List <T>(); pocoMap = PocoMapService.Instance.GetPocoMap(obj); if (string.IsNullOrWhiteSpace(_dbConnectionContext.TSql)) { _dbConnectionContext.TSql = pocoMap.SelectListProcedure; } _dataProvider = DbFactory.Instance.Db(_dbConnectionContext); DoInsertUpdate(obj, PocoOperationType.SelectList); XapDataReader dr = _dataProvider.ExecuteReader(); while (dr.Read()) { T newObj = Activator.CreateInstance <T>(); SetListPropertiesFromDataReader <T>(newObj, dr); lst.Add(newObj); } return(lst); } catch (Exception ex) { throw new XapException($"Error performing select list for {typeof(T).FullName}", ex); } finally { _dataProvider.CloseConnection(); } }
void IXapPoco.Delete <T>(T obj) { try { pocoMap = PocoMapService.Instance.GetPocoMap(obj); if (string.IsNullOrWhiteSpace(_dbConnectionContext.TSql)) { _dbConnectionContext.TSql = pocoMap.DeleteProcedure; } _dataProvider = DbFactory.Instance.Db(_dbConnectionContext); DoInsertUpdate(obj, PocoOperationType.Delete); _dataProvider.ExecuteNonQuery(); } catch (Exception ex) { throw new XapException($"Error performing delete for {typeof(T).FullName}", ex); } }
internal IXapPocoMap GetPocoMap <T>(T obj) { IXapPocoMap pocoMap = pocoMaps.GetItem(typeof(T).FullName); if (pocoMap != null) { return(pocoMap); } DbExecution dbExecution = SharedMethods.GetCustomAttribute <DbExecution>(obj); if (dbExecution == null) { return(null); } pocoMap = PocoMap.Create(); pocoMap.ObjectName = obj.GetType().FullName; pocoMap.InsertProcedure = dbExecution.InsertProcedure; pocoMap.SelectProcedure = dbExecution.SelectProcedure; pocoMap.SelectListProcedure = dbExecution.SelectListProcedure; pocoMap.UpdateProcedure = dbExecution.UpdateProcedure; pocoMap.DeleteProcedure = dbExecution.DeleteProcedure; foreach (PropertyInfo prop in PropertyService.Instance.GetInterfaceProperties <T>(obj).GetProperties()) { object[] attributes = prop.GetCustomAttributes(typeof(DbBinding), true); if (attributes.Length == 1) { IXapPocoField pocoField = PocoField.Create(); pocoField.DbColumn = ((DbBinding)attributes[0]).DbColumn; pocoField.FieldName = prop.Name; pocoField.DoesInsert = ((DbBinding)attributes[0]).DoesInsert; pocoField.DoesSelect = ((DbBinding)attributes[0]).DoesSelect; pocoField.DoesSelectList = ((DbBinding)attributes[0]).DoesSelectList; pocoField.DoesUpdate = ((DbBinding)attributes[0]).DoesUpdate; pocoField.DoesDelete = ((DbBinding)attributes[0]).DoesDelete; pocoField.IsIdentity = ((DbBinding)attributes[0]).IsIdentityField; pocoField.DataType = ((DbBinding)attributes[0]).DataType; pocoMap.AddField(pocoField); } } pocoMaps.AddItem(pocoMap.ObjectName, pocoMap); return(pocoMap); }
T IXapPoco.Insert <T>(T obj) { try { pocoMap = PocoMapService.Instance.GetPocoMap(obj); if (string.IsNullOrWhiteSpace(_dbConnectionContext.TSql)) { _dbConnectionContext.TSql = pocoMap.InsertProcedure; } _dataProvider = DbFactory.Instance.Db(_dbConnectionContext); DoInsertUpdate(obj, PocoOperationType.Insert); int idField = _dataProvider.ExecuteNonQuery(); obj.GetType()?.GetProperty(pocoMap.GetIdentityField()).SetValue(obj, idField); return(obj); } catch (Exception ex) { throw new XapException($"Error performing insert for {typeof(T).FullName}"); } }
T IXapPoco.Select <T>(T obj) { try { pocoMap = PocoMapService.Instance.GetPocoMap(obj); if (string.IsNullOrWhiteSpace(_dbConnectionContext.TSql)) { _dbConnectionContext.TSql = pocoMap.SelectProcedure; } _dataProvider = DbFactory.Instance.Db(_dbConnectionContext); DoInsertUpdate(obj, PocoOperationType.Select); XapDataReader dr = _dataProvider.ExecuteReader(); SetPropertiesFromDataReader <T>(obj, dr); return(obj); } catch (Exception ex) { throw new XapException($"Error performing select for {typeof(T).FullName}", ex); } finally { _dataProvider.CloseConnection(); } }