public async Task <bool> SaveAsync(IDataTransaction transaction) { var columns = new List <PropertyInfo>(); while (Dirty.TryTake(out var e)) { columns.Add(e); } IDataTransaction tr = null; try { tr = transaction ?? _data.GetTransaction(); if (Target is IEntity <int> ei && ei.Id < 0) { var t = await tr.AddAsync <T>(e => Target.CopyPrimitivesTo(e)); if (transaction == null) { tr.Done(); } ei.Id = (int)t.Id; return(true); } if (await tr.UpdateAsync(Target, columns.Select(GetColumnName))) { if (transaction == null) { tr.Done(); } IsDirty = false; return(true); } return(false); } catch (Exception ex) { foreach (var p in columns) { Dirty.Add(p); } throw new DataException("Save failure", ex); } finally { if (transaction == null) { tr?.Dispose(); } } }
public IRestCollection GetRestCollection(Stem <TItem> stem) { IDataTransaction transaction = _dataSource.CreateTransaction(); stem.OnDispose += delegate { transaction.Dispose(); }; IEngineRepository <TItem> repository = _dataSource.GetRepository <TItem>(transaction); var wrapper = new DataEventWrapper <TItem>(transaction, repository); wrapper.TryWrapEvents(new StemDataChangeEvents <TItem>(stem)); var subContext = new StemsEngineSubContext <TItem>(stem); var context = new FullEngineContext <TItem>(wrapper.Transaction, wrapper.Repository, subContext); return(new EngineRestCollection <TItem>(context)); }
public void Dispose() { _transaction.Dispose(); }