public static List <TDTOEntity> Select(XpoDatabase database, CriteriaOperator criteria = null, int pageSize = -1, int pageIndex = -1, params SortProperty[] sortProperties) { List <TDTOEntity> result = new List <TDTOEntity>(); using (UnitOfWork wrk = database.GetUnitOfWork()) { XPCollection <TXPOEntity> items = new XPCollection <TXPOEntity>(wrk, criteria, sortProperties); if (pageIndex > -1) { if (pageSize > -1) { items.SkipReturnedObjects = pageSize * pageIndex; } items.TopReturnedObjects = pageSize; } foreach (TXPOEntity item in items) { TDTOEntity dto = Activator.CreateInstance(typeof(TDTOEntity), (item as TXPOEntity)) as TDTOEntity; if (dto != null) { result.Add(dto); } } } return(result); }
public static void Insert(XpoDatabase database, IEnumerable <TDTOEntity> items, int savingFlags, Func <TDTOEntity, Exception[]> validateInsert, out TKey[] keyValues) { // Validate your input here !!!! if (validateInsert != null) { foreach (var item in items) { Exception[] validationResult = validateInsert(item); if (validationResult != null) { throw new Exception(String.Format("Insert Validation of {0} item failed:\n{1}", typeof(TDTOEntity).FullName, item.Key, String.Join("\n", (from e in validationResult select e.Message)))); } } } TKey[] ids = null; using (UnitOfWork wrk = database.GetUnitOfWork()) { wrk.ObjectsSaved += (sender, e) => { ids = (from n in e.Objects.OfType <TXPOEntity>() select n.Key).ToArray(); }; foreach (var item in items) { TXPOEntity dbItem = Activator.CreateInstance(typeof(TXPOEntity), wrk) as TXPOEntity; dbItem.Assign(item, savingFlags); } wrk.CommitChanges(); keyValues = ids; } }
public static TDTOEntity Select(XpoDatabase database, CriteriaOperator criteria, int loadingFlags = 0, bool raiseExceptionNotFound = false) { TDTOEntity result = null; using (UnitOfWork wrk = database.GetUnitOfWork()) { TXPOEntity item = wrk.FindObject <TXPOEntity>(criteria); if ((item == null) && raiseExceptionNotFound) { throw new Exception(String.Format("{0} Item not found on '{1}' not found", typeof(TXPOEntity).FullName, criteria.ToString())); } result = Activator.CreateInstance(typeof(TDTOEntity), (item as TXPOEntity), loadingFlags) as TDTOEntity; } return(result); }
public static IQueryable <TDTOEntity> Select(XpoDatabase database, Expression <Func <TXPOEntity, bool> > filter = null, int pageSize = -1, int pageIndex = -1, params SortProperty[] sortProperties) { IQueryable <TDTOEntity> result; using (UnitOfWork wrk = database.GetUnitOfWork()) { var tmp = wrk.Query <TXPOEntity>().Where(filter); if ((pageIndex > -1) && (pageSize > 0)) { tmp = tmp.Skip(pageSize * pageIndex).Take(pageSize); } result = tmp.Select((xpo, i) => Activator.CreateInstance(typeof(TDTOEntity), (xpo as TXPOEntity)) as TDTOEntity); } return(result); }
public static void Update(XpoDatabase database, IEnumerable <TDTOEntity> items, int savingFlags, Func <TDTOEntity, Exception[]> validateUpdate, bool insertOnNotFound = false, bool raiseExceptionOnNotFound = false) { // Validate your input here !!!! if (validateUpdate != null) { foreach (var item in items) { Exception[] validationResult = validateUpdate(item); if (validationResult != null) { throw new Exception(String.Format("Update Validation of {0} item({1}) failed:\n{2}", typeof(TDTOEntity).FullName, item.Key, String.Join("\n", (from e in validationResult select e.Message)))); } } } using (UnitOfWork wrk = database.GetUnitOfWork()) { foreach (var item in items) { TXPOEntity dbItem = wrk.GetObjectByKey <TXPOEntity>(item.Key); if (dbItem == null) { if (insertOnNotFound) { dbItem = Activator.CreateInstance(typeof(TXPOEntity), wrk) as TXPOEntity; } else if (raiseExceptionOnNotFound) { throw new Exception(String.Format("{0} Item with Id = '{1}' not found for updating", typeof(TXPOEntity).FullName, item.Key)); } } if (dbItem != null) { dbItem.Assign(item, savingFlags); } } wrk.CommitChanges(); } }
public static void Delete(XpoDatabase database, IEnumerable <TDTOEntity> items, Func <TDTOEntity, Exception[]> validateDelete) { // Validate your input here !!!! if (validateDelete != null) { foreach (var item in items) { Exception[] validationResult = validateDelete(item); if (validationResult != null) { throw new Exception(String.Format("Delete Validation of {0} item({1}) failed:\n{2}", typeof(TDTOEntity).FullName, item.Key, String.Join("\n", (from e in validationResult select e.Message)))); } } } using (UnitOfWork wrk = database.GetUnitOfWork()) { wrk.Delete(wrk.GetObjectsByKey(wrk.GetClassInfo <TXPOEntity>(), (from o in items select o.Key).ToArray(), false)); wrk.CommitChanges(); } }
public static Session GetSession(bool transactional = false) { return(transactional ? _ctx.GetUnitOfWork() : _ctx.GetSession()); }