NET Centric Computing - Old Questions

1. Describe the MVC pattern. Create class to showcase constructor, properties, indexers and encapsulation behavior of object oriented language.

10 marks | Asked in Model Question

The Model-View-Controller (MVC) is an architectural pattern that separates an application into three main logical components: the model, the view, and the controller. Each of these components are built to handle specific development aspects of an application.


Model: Model represents the structure of data, the format and the constraints with which it is stored. It maintains the data of the application. Essentially, it is the database part of the application.

View: View is what is presented to the user. Views utilize the Model and present data in a form in which the user wants. A user can also be allowed to make changes to the data presented to the user. They consist of static and dynamic pages which are rendered or sent to the user when the user requests them.

Controller: Controller controls the requests of the user and then generates appropriate response which is fed to the viewer. Typically, the user interacts with the View, which in turn generates the appropriate request, this request will be handled by a controller. The controller renders the appropriate view with the model data as a response.

Second Part:

namespace CSharpExamples

{

    class Customer

    {

        private string name;

        // Properties

        public int CustomerID { get; set; }

                //This will encapsulate the private variable name. name can only accessed from this property from other classes.

        public string Name

        {

            get

            {

                return name;

            }

            set

            {

                name = value;

            }

        }

        //Indexer

        private string[] purchasedItem = new string[10];

        public string this[int i]

        {

            get

            {

                return purchasedItem[i];

            }

            set

            {

                purchasedItem[i] = value;

            }

        }

        public int Length

        {

            get { return purchasedItem.Length; }

        }

        // Constructor

        public Customer(int ID, string name)

        {

            Name = name;

            CustomerID = ID;

        }

        // .. Additional methods, events, etc.

}

  // Test class

   class Program

    {

        static void Main(string[] args)

        {

            //Using User defined constructor

            Customer cust = new Customer(1, "Jayanta");

 

            //Adding purchased items to demonstrate Indexer

            for (int i = 0; i < 10; i++)

            {

                cust[i] = "Item" + i;

            }

            Console.WriteLine("The customer Name is " + cust.Name + " And ID is " + cust.CustomerID);

            Console.WriteLine(cust.Name + " Purchased following items");

            for (int j = 0; j < cust.Length; j++)

            {

                Console.WriteLine(cust[j]);

            }

            Console.ReadLine();

        }

    }

}