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

Pretty URLs, Separation of Layers and O/R Mapping in Web Forms ASP.NET 2.0

Rate me:
Please Sign up or sign in to vote.
4.47/5 (14 votes)
12 Sep 200732 min read 87K   493   87  
Implementing multi-tier architecture in a web application using ASP.NET 2.0
using System;                       //neeeded for Convert.ChaneType
using System.Collections;           // for Idict
using System.Collections.Generic;   //needed

using NHibernate.MapClasses;        //needed

using MVC.Models;                    // needed

using Utils;             //for ActionProperty attribute

namespace MVC.Controllers
{


    public class LoginController : ILoginController
    {


        //-------------View Section--------------------------------------------------

        /// <summary>
        /// refernce to the view, passed in on  creation
        /// </summary>
        private MVC.Views.IGeneralView _view;             //controller can post a message to the view
        /// <summary>
        /// Property used to set,get a view
        /// </summary>
        public MVC.Views.IGeneralView View
        {
            get { return _view; }
            set { _view = value; }
        }

        //--------------------------Models Section--------------------------------

        /// <summary>
        /// references to the IDataModels, retrived 
        /// on creation from the factory
        /// </summary>
        private MVC.Models.ILoginModel _dataLoginUsersModel;      //controller has to know about data models


        //------------this sections contains the data needed to create a view on GET/POST request------------
        private User  _singleUser;
        private UserExtended _singleExtendedUser;
        private IList<User> _listofusers;

        public IList<User> ListOfUsers()
        {
            return _listofusers;
        }
        public User SingleUser()
        {
            return _singleUser;
        }
        public UserExtended SingleExtendedUser()
        {
            return _singleExtendedUser;
        }

        //---------------------------------------------------------------------------------------------
        public LoginController()            // this constructor is used to create a controller
        {                                    // in the module  at the beginning of the 
                                             // reguest

           
            // During creation contoller retrives  instances of the all models
            // from the factory 

            _dataLoginUsersModel = MVC.Factories.Factory.GetLoginUsersModel();
               
        }
        //------These methods are called from the code behind on PostBack----------------------------------
        //--- Some methods  here are used for the CRUD operations by ObjectDataSource control(ODS)---------
                

        
        public void CompareUser(IDictionary values) //string Name, string Hashed_Password
        {
            string Name = (string)values["Name"];
            string Hashed_Password = (string)values["Hashed_Password"];
            Hashed_Password = Utils.Utils.Encrypt(Hashed_Password);

            int affrows = _dataLoginUsersModel.CompareUser(Name, Hashed_Password);
            if (affrows > 0)
            {
                _view.Alert("You are logged in now");
                Utils.Utils.CreateUserSession();
            }
            else
                _view.Alert("You are not authorized user!");
        }



        public void DeleteUser(int Id)
        {
            int affrows = _dataLoginUsersModel.DeleteUser(Id);
            if (affrows == 0)
            {
                _view.Alert("Deleting failed (most likely you wanted to delete superuser)!");
            }
            else
            {
                _view.Alert("Deleting succeded!");
                _listofusers = _dataLoginUsersModel.ListUsers();
            }
        }
            
        
    
    
            
        

        public void AddUser(IDictionary values)
        {
            string Name = (string)values["Name"];
            string Hashed_Password = (string)values["Hashed_Password"];
            string retype_hashed_password = (string)values["retype_hashed_password"];

            if (Hashed_Password == retype_hashed_password)
            {
                Hashed_Password = Utils.Utils.Encrypt(Hashed_Password);
                
                int affrows = _dataLoginUsersModel.AddUser(Name, Hashed_Password);
                if (affrows > 0)
                    _view.Alert("User was added.");
                else
                    _view.Alert("User could not be added!");
            }
            else
            { 
                _view.Alert(" Passwords do not match"); 
            }

            
           
        }
        
        //----------------End of Methods----------------------------------------------------------


        //--------------------------this part determines the view which will be served
       




        // All actions listed below are bookmarkable

        [ActionProperty]
        public  string login
        {
            get { return "login"; }
            set
            {
                if (Utils.Utils.IsGetRequest)
                {
                    _singleUser = new User(); //dummy user to get to work FormsView
                }
            }
        }

        [ActionProperty]
        public  string add_user
        {
            get { return "add_user"; }
            set   
            {
                if (Utils.Utils.IsGetRequest)
                {
                    _singleExtendedUser = new UserExtended(); //dummy user to get to work FormsView
                }
            } 
        }

        [ActionProperty]
        public string list_users
        {
            get { return "list_users"; }
            set //we need this on POST  to recreate control,b/c viewstate is disabled
            {
                _listofusers = _dataLoginUsersModel.ListUsers();
            }
                
            
        }
               
           


    }//class



}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer iPay, Elizabethtown,Ky
United States United States
After defending his PhD thesis in 2005 (computational nuclear physics) at Vanderbilt in Nashville, the author decided to pursue a career in software development. As a long time open source advocate, he started with writing web applications using Linux-Apache-MySql-P (LAMP) framework. After that experience, he decided to embrace Microsoft technologies.
Currently working as a web developer in .NET platform in the online payments company.

Comments and Discussions