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

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.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.ComponentModel;
using Demo.DataModel;

namespace Demo.WebApp
{

    #region interfaces
    public interface ICustomerFilter //: IPagingController
    {
        string SearchText { get; set; }
    }

    public interface ICustomerList : ICustomerFilter
    {
        IList<Customer> SelectCustomerList();
        event EventHandler OnCustomerListChanged;
        void SelectCustomer(string key);
        void NewCustomer();
    }

    public interface ICustomerEdit
    {
        void Reset();
        event EventHandler OnCustomerChanged;

        Customer SelectCustomer();
        void UpdateCustomer(Customer Customer);
        void DeleteCustomer(Customer Customer);
        void InsertCustomer(Customer Customer);
    }
    #endregion

    public class CustomerController : ICustomerFilter, ICustomerList, ICustomerEdit
    {
        ICustomerService service = new CustomerService();

        #region ICustomerService
        public ICustomerService CustomerService
        {
            get;
            set;
        }
        #endregion

        #region ICustomerFilter Members
        public string SearchText { get; set; }
        #endregion

        #region IPagingController Members

        private int pageNo = 1;

        public int PageNo
        {
            get { return pageNo; }
            set
            {
                pageNo = value; CustomerList = null;
            }
        }

        private int pageSize = Constants.DefaultGridPageSize;

        public int PageSize
        {
            get { return pageSize; }
            set { pageSize = value; }
        }

        private int recordCount = 0;

        public int RecordCount
        {
            get { return recordCount; }
            set { recordCount = value; }
        }

        private string sortOrder;

        public string SortOrder
        {
            get { return sortOrder; }
            set { sortOrder = value; }
        }

        public event EventHandler OnCustomerListChanged = delegate { };

        #endregion

        #region ICustomerList Members
        private IList<Customer> _CustomerList;

        public IList<Customer> CustomerList
        {
            get
            {
                if (_CustomerList == null)
                {
                    var items = string.IsNullOrEmpty(SearchText) ?
                        service.GetAllCustomers() :
                        service.GetSearchCustomers(SearchText);

                    _CustomerList = (from c in items
                                     select c).ToList();

                }
                return _CustomerList;
            }
            set
            {
                _CustomerList = value; OnCustomerListChanged(this, EventArgs.Empty);
            }
        }

        public IList<Customer> SelectCustomerList()
        {
            return CustomerList;
        }

        public void SelectCustomer(string key)
        {
            var items = from item in CustomerList
                        where item.Id == key
                        select item;

            CustomerSelected = items.Count() > 0 ? items.First() : null;
        }

        public void NewCustomer()
        {
            //TODO:
            CustomerSelected = new Customer();
        }

        #endregion

        #region ICustomerEdit Members
        public void Reset()
        {
            CustomerList = null;
            CustomerSelected = null;
        }

        public void UpdateCustomer(Customer Customer)
        {
            //TODO:
            Reset();
        }

        public void DeleteCustomer(Customer Customer)
        {
            //TODO:
            Reset();
        }

        public void InsertCustomer(Customer Customer)
        {
            //TODO:
            Reset();
        }

        public Customer SelectCustomer()
        {
            return CustomerSelected;
        }

        private Customer _CustomerSelected;

        public Customer CustomerSelected
        {
            get { return _CustomerSelected; }
            set { _CustomerSelected = value; OnCustomerChanged(this, EventArgs.Empty); }
        }

        public event EventHandler OnCustomerChanged = delegate { };

        #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)

About the Author

Yiyi Sun
Architect
Canada Canada
No Biography provided

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