Click here to Skip to main content
15,892,298 members
Articles / General Programming / Architecture

Binding Web Pages with nHydrate

,
Rate me:
Please Sign up or sign in to vote.
4.71/5 (5 votes)
1 Jun 2011Ms-PL12 min read 20.9K   256   14  
Bind your UI controls to generated objects generically
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.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)



Written By
Software Developer (Senior) Hewlett Packard
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions