Tuesday, 9 December 2014

Pin It

Widgets

3 Layered ASP.NET Application Example - Part 3


In this post, we will create BLL for PersonalTaskTracker application.

  • Right click the Solution and add a new project which is Console Application named BLL.
  • Right click the BLL project and add the reference to DAL
Add the following two classes to the BLL project.



Task.cs
using System;
using DAL;

namespace BLL
{
    /// 
    /// The Task class.
    /// 
    public class Task
    {
        /// 
        /// Id of task.
        /// 
        private int _taskId;

        /// 
        /// Summary of task.
        /// 
        private string _summary;

        /// 
        /// Description of task.
        /// 
        private string _description;


        /// 
        /// Id of the status of the task.
        /// 
        private int _statusId;

        /// 
        /// Name of the status of the task.
        /// 
        private string _statusName;

        /// 
        /// Date of creation of the task.
        /// 
        private DateTime _createdOn;


        /// 
        /// get,set property for id of task.
        /// 
        public int TaskID
        {
            get { return _taskId; }
            set { _taskId = value; }
        }

        /// 
        /// get, set property for Summary of task.
        /// 
        public string Summary
        {
            get { return _summary; }
            set { _summary = value; }
        }

        /// 
        /// get, set property for description of task.
        /// 
        public string Description
        {
            get { return _description; }
            set { _description = value; }
        }

        /// 
        /// get, set property for id of the status of the task.
        /// 
        public int StatusID
        {
            get { return _statusId; }
            set { _statusId = value; }
        }

        /// 
        /// get, set property for name of the status of the task.
        /// 
        public string StatusName
        {
            get { return _statusName; }
            set { _statusName = value; }
        }

        /// 
        /// get, set property for date of creation of task.
        /// 
        public DateTime CreatedOn
        {
            get { return _createdOn; }
            set { _createdOn = value; }
        }

        /// 
        /// Method to add a new task.
        /// 
        /// Id of the newly created task.
        public int AddTask()
        {
            TaskManager.Task task = new TaskManager.Task();

            task.Summary = this._summary;
            task.Description = this._description;
            task.StatusId = this._statusId;
            task.CreatedOn = this._createdOn;

            this._taskId = TaskManager.InsertTask(task).TaskId;
            return this._taskId;
        }

        /// 
        /// Method to get task of given id.
        /// 
        /// Id of the task.
        /// The retrieved task.
        public static Task GetTask(int taskId)
        {
            Task task = new Task();
            task._taskId = taskId;
            task.FetchTask();

            return task;
        }

        /// 
        /// Method to fetch data from DAL object to BLL object.
        /// 
        private void FetchTask()
        {
            TaskManager.Task task = TaskManager.GetTask(this._taskId);
            this._taskId = task.TaskId;
            this._summary = task.Summary;
            this._description = task.Description;
            this._statusName = task.StatusName;
            this._createdOn = task.CreatedOn;
            this._statusId = task.StatusId;

        }

        /// 
        /// Method to delete a task.
        /// 
        public void DeleteTask()
        {
            TaskManager.DeleteTask(this._taskId);
        }

        /// 
        /// Method to update a task.
        /// 
        public void UpdateTask()
        {
            TaskManager.Task task = new TaskManager.Task();

            task.TaskId = this._taskId;
            task.Summary = this._summary;
            task.Description = this._description;
            task.StatusId = this._statusId;
            task.CreatedOn = this._createdOn;

            TaskManager.UpdateTask(task);
        }
    }
}

TaskList.cs
using System.Collections.Generic;
using DAL;

namespace BLL
{
    public class TaskList : List<Task>
    {
        public static List<Task> GetAllTasks()
        {
            TaskList taskList = new TaskList();
            taskList.FetchAllTasks();
            return taskList;
        }

        private void FetchAllTasks()
        {
            List<TaskManager.Task> tasks = TaskManager.GetAllTasks();
            foreach (var task in tasks)
            {
                Task tempTask = new Task();
                tempTask.TaskID = task.TaskId;
                tempTask.Summary = task.Summary;
                tempTask.Description = task.Description;
                tempTask.StatusName = task.StatusName;
                tempTask.CreatedOn = task.CreatedOn;

                this.Add(tempTask);
            }
        }
    }
}
We will see about UIin the next post.

No comments: