using System; using System.Collections.Generic; using System.Linq; using System.Web; using Acme.EFExample.EFDAL; using Widgetsphere.EFCore.DataAccess; namespace Acme.EFExample.Website.Objects { public abstract class BasePersistablePage : BasePage { protected BasePersistablePage() { this.Mapper = new ORMMap(); } #region Page Events protected override void OnInit(EventArgs e) { base.OnInit(e); using (var context = new EFExampleEntities()) { SetupBindings(context); } //Add dynamic validators to the page this.Mapper.GetValidators().ToList().ForEach(x => this.Page.Form.Controls.Add(x)); this.Mapper.DataBind(); } #endregion #region Properties public ORMMap Mapper { get; private set; } #endregion #region Events public event EventHandler DataSavedSucess; public event EventHandler<DataErrorEventArgs> DataSavedFailed; protected virtual void OnDataSavedSucess(System.EventArgs e) { if (this.DataSavedSucess != null) this.DataSavedSucess(this, e); } protected virtual void OnDataSavedFailed(DataErrorEventArgs e) { if (this.DataSavedFailed != null) this.DataSavedFailed(this, e); } #endregion #region Methods protected abstract void SetupBindings(EFExampleEntities context); protected virtual NHEntityObject CreateObject(EFExampleEntities context) { return null; } public virtual void SaveData(object sender, EventArgs e) { try { if (!this.IsValid) { this.OnDataSavedFailed(new DataErrorEventArgs()); return; } using (var context = new EFExampleEntities()) { SetupBindings(context); this.Mapper.DataPersist(); context.SaveChanges(); } this.OnDataSavedSucess(new System.EventArgs()); } catch (Exception ex) { var validator = new System.Web.UI.WebControls.CustomValidator(); validator.IsValid = false; validator.ErrorMessage = ex.Message; this.Validators.Add(validator); this.OnDataSavedFailed(new DataErrorEventArgs() { ErrorMessage = ex.Message }); } } #endregion } }
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.
This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)