Click here to Skip to main content
Click here to Skip to main content
Articles » Web Development » ASP.NET » General » Downloads
 
Add your own
alternative version
Go to top

Presentation Model and Dependency Injection

, 26 Apr 2009
ASP.NET MVVM provides a framework to implement the Presentation Model pattern, a.k.a. the Model-View-ViewModel pattern in ASP.NET projects. Developers can take advantages of Dependency Injection and Event Broker to write concise, elegant and business focused code.
using System;
using System.ComponentModel;
using System.Web.UI.WebControls;
using Demo.DataModel;
using Demo.Web.Mvvm;

namespace Demo.WebApp
{
    public partial class CustomerEdit : System.Web.UI.UserControl
    {

        [Inject]
        public ICustomerEdit controller { get; set;}

        protected void Page_Load(object sender, EventArgs e)
        {
            //System.Diagnostics.Debug.Assert(controller != null);
            //controller.OnCustomerChanged += new EventHandler(controller_OnCustomerChanged);
            //Visible = controller.SelectCustomer() != null;
        }

        //protected void Page_Unload(object sender, EventArgs e)
        //{
        //    System.Diagnostics.Debug.Assert(controller != null);
        //    controller.OnCustomerChanged -= new EventHandler(controller_OnCustomerChanged);
        //}

        [EventSubscription]
        void OnCustomerChanged(object sender, EventArgs e)
        {
            try
            {
                if (controller.SelectCustomer() != null)
                {
                    CustomerFormView.DataBind();
                    Visible = true;
                }
                else
                {
                    Visible = false;
                }
            }
            catch (Exception ex)
            {
                ErrorHandler.HandleException(this, ex);
            }            
        }

        protected void CustomerDataSource_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
        {
            e.ObjectInstance = controller;
        }

        protected void CustomerFormView_ItemCommand(object sender, FormViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case Constants.CommandName_Cancel:
                    controller.Reset();
                    MessageBox.Show(Page, Resources.Messages.Edit_Cancelled);
                    break;
            }
        }

        protected void CustomerFormView_ItemDeleted(object sender, FormViewDeletedEventArgs e)
        {
            if (e.Exception != null)
            {
                ErrorHandler.HandleException(this, e.Exception);
                e.ExceptionHandled = true;
            }
            else
            {
                MessageBox.Show(Page, Resources.Messages.Edit_Deleted);
            }
        }

        protected void CustomerFormView_ItemInserted(object sender, FormViewInsertedEventArgs e)
        {
            if (e.Exception != null)
            {
                ErrorHandler.HandleException(this, e.Exception);
                e.ExceptionHandled = true;
            }
            else
            {
                MessageBox.Show(Page, Resources.Messages.Edit_Inserted);
            }
        }

        protected void CustomerFormView_ItemUpdated(object sender, FormViewUpdatedEventArgs e)
        {
            if (e.Exception != null)
            {
                ErrorHandler.HandleException(this, e.Exception);
                e.ExceptionHandled = true;
            }
            else
            {
                MessageBox.Show(Page, Resources.Messages.Edit_Updated);
            }
        }

        protected void CustomerFormView_DataBound(object sender, EventArgs e)
        {
            //TODO:
            //if (controller.SelectCustomer().??? == null)
            //{
            //    CustomerFormView.ChangeMode(FormViewMode.Insert);
            //}
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            controller.Reset();
        }
    }
}

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)

Share

About the Author

Yiyi Sun
Architect
Canada Canada
No Biography provided

| Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 26 Apr 2009
Article Copyright 2009 by Yiyi Sun
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid