コード例 #1
0
        public virtual void Create(T item)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }

            if (BeforeSavingRecord != null)
            {
                BeforeSavingRecord.Invoke(this, new EntitySavingEventArgs <T>()
                {
                    SavedEntity = item
                });
            }
            _dbContext.Set <T>().Add(item);

            if (SavingRecord != null)
            {
                SavingRecord.Invoke(this, new EntitySavingEventArgs <T>()
                {
                    SavedEntity = item
                });
            }


            _dbContext.SaveChanges();

            if (RecordSaved != null)
            {
                RecordSaved.Invoke(this, new EntitySavingEventArgs <T>()
                {
                    SavedEntity = item
                });
            }
        }
コード例 #2
0
        public bool Save()
        {
            // get the textboxes with changes that haven't fired Validated event
            var unvalidatedTextboxes = _textChanges.Where(kp => kp.Value && !_validated[kp.Key]).Select(kp => kp.Key).ToList();

            foreach (var tb in unvalidatedTextboxes)
            {
                _textBoxValidators[tb].Validated.Invoke(this, new EventArgs());
            }

            if (_suspend)
            {
                return(true);
            }
            _suspend = true;

            try
            {
                if (IsDirty)
                {
                    SavingRecord?.Invoke(this, new EventArgs());
                    _db.Save(_record);
                    IsDirty = false;
                    RecordSaved?.Invoke(this, new EventArgs());
                    ValidationPanel?.SetStatus(RecordStatus.Valid, "Record saved");
                }
                return(true);
            }
            catch (Exception exc)
            {
                ValidationPanel?.SetStatus(RecordStatus.Invalid, exc.Message);

                if (MessageBox.Show($"The record could not be saved: {exc.Message}\r\nClick OK to try again or Cancel to lose your changes.", "Save Error", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
                {
                    UndoChanges();
                    return(true);
                }
                return(false);
            }
            finally
            {
                _suspend = false;
            }
        }
コード例 #3
0
 public virtual void Create(T item)
 {
     if (item == null)
     {
         throw new ArgumentNullException("item");
     }
     BeforeSavingRecord?.Invoke(this, new EntitySavingEventArgs <T>()
     {
         SavedEntity = item
     });
     _dbContext.Set(typeof(T)).Add(item);
     SavingRecord?.Invoke(this, new EntitySavingEventArgs <T>()
     {
         SavedEntity = item
     });
     _dbContext.SaveChanges();
     RecordSaved?.Invoke(this, new EntitySavingEventArgs <T>()
     {
         SavedEntity = item
     });
 }
コード例 #4
0
ファイル: RowManagerBase.cs プロジェクト: vebin/Postulate
        public void Save(IDbConnection connection, TRecord record, out SaveAction action, object parameters = null, string userName = null)
        {
            action = (record.IsNewRecord()) ? SaveAction.Insert : SaveAction.Update;
            SavingRecord?.Invoke(connection, action, record);

            if (record.IsNewRecord())
            {
                record.Id = Insert(connection, record, parameters, userName);
            }
            else
            {
                string ignoreProps;
                if (HasChangeTracking(out ignoreProps))
                {
                    CaptureChanges(connection, record, ignoreProps, userName);
                }
                Update(connection, record, parameters, userName);
            }

            RecordSaved?.Invoke(connection, action, record);
        }
コード例 #5
0
        public virtual async Task <T> CreateAsync(T item)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }

            if (BeforeSavingRecord != null)
            {
                BeforeSavingRecord.Invoke(this, new EntitySavingEventArgs <T>()
                {
                    SavedEntity = item
                });
            }
            await _dbContext.Set <T>().AddAsync(item);

            if (SavingRecord != null)
            {
                SavingRecord.Invoke(this, new EntitySavingEventArgs <T>()
                {
                    SavedEntity = item
                });
            }


            await _dbContext.SaveChangesAsync();

            if (RecordSaved != null)
            {
                RecordSaved.Invoke(this, new EntitySavingEventArgs <T>()
                {
                    SavedEntity = item
                });
            }

            return(item);
        }