Click here to Skip to main content
15,880,608 members
Articles / Web Development / ASP.NET

NHibernate Templates for Smart Code Generator

Rate me:
Please Sign up or sign in to vote.
4.77/5 (20 votes)
11 Dec 2007CPOL5 min read 202.6K   1.9K   130  
Describes how to generate NHibernate objects and ASPX pages using Smart Code
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Northwind.Core.DataInterfaces;
using Northwind.Data;
using Northwind.Core.Domain;

namespace Northwind.Web
{
    public partial class ShipperEdit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                fillComboBoxes();
                fillForm();
            }
        }


        private void fillComboBoxes()
        {
            IDaoFactory daoFactory = new NHibernateDaoFactory();
        }

        private void fillForm()
        {
            if ( Request.QueryString["ShipperID"] != null  )
            {
                System.Int32 ID = Convert.ToInt32(uiShipperID.Value);

                uiShipperID.Value = Request.QueryString["ShipperID"];
                IDaoFactory daoFactory = new NHibernateDaoFactory();
                IShipperDao dao = daoFactory.GetShipperDao();
                Shipper entity = dao.GetById(ID, false );

                uiCompanyName.Text = entity.CompanyName;
                uiPhone.Text = entity.Phone;
                uiIsNew.Checked = false;
            }
        }

        protected void Update(object sender, System.EventArgs e)
        {
            Shipper entity = null;

            IDaoFactory daoFactory = new NHibernateDaoFactory();
            IShipperDao dao = daoFactory.GetShipperDao();

            System.Int32 ID = Convert.ToInt32(uiShipperID.Value);
            if (! uiIsNew.Checked )
            {
                entity = dao.GetById(ID, false );
                }
                else
                {
                    entity = new Shipper(ID);
                }

            entity.CompanyName = uiCompanyName.Text;
            entity.Phone = uiPhone.Text;

            if (uiIsNew.Checked)
            {
                dao.Save(entity);
                uiShipperID.Value = Convert.ToString(entity.ID);
            }
            else
            {
                dao.SaveOrUpdate(entity);
            }
            uiIsNew.Checked = false;
        }

        protected void Delete(object sender, System.EventArgs e)
        {
            if (! uiIsNew.Checked)
            {
                System.Int32 ID = Convert.ToInt32(uiShipperID.Value);
                IDaoFactory daoFactory = new NHibernateDaoFactory();
                IShipperDao dao = daoFactory.GetShipperDao();
                Shipper entity = dao.GetById(ID, false );

                dao.Delete(entity);

                Response.Redirect("ShipperList.aspx");
            }
        }


    }
}

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 Code Project Open License (CPOL)


Written By
Web Developer
United States United States
Danilo is the creator of SmartRules, a Business Rules Engine. He is an industry consultant working primarily with companies interested in implementing dynamic rules programming concepts to add flexibility to their architectures on web, CE, and desktop platforms. He operates his own website, Kontac, where you will find more information.

To contact Danilo, email him at danilo.mendez@gmail.com.

Comments and Discussions