Friday, May 15, 2015

Building a Blog - Pt1 Builiding a Database (MVC, Models and Classes)

In the previous two posts, I've written about setting the Git on the Github.com and then connecting to it using Visual Studio Express 2013 for the Web. If you are on this post first time, I'd suggest you to read the very first post about Building a Blog, I have written, so you can get more picture of what is this all about.

In this post I have decided to make it in two parts, where in this part or the Pt1 I will say a bit more what MVC is and what Classes are as we will need them for Pt2.

MVC

I guess it is a good time now to explain a bit about what MVC is. MVC stands for Model, View and Controller. They together make a software architectural pattern for implementing user interfaces called MVC.

View - is what the end user will see. Meaning the HTML page or in our case CSHTML. You can think of View as any other HTML page. Meaning you can use CSS styles to it, Javascript, Jquery and all other cool stuff. Purpose of View is to present data that is stored in the Model, to the user.

Controller - is what controls the data between the View and the Model. Say you want to get data from the View stored in the database, or data from the database to the view, the Controller is where you do the POST and GET. Or any other logic you need to manipulate the data. And by database I mean model.

Model - How to put this and to not confuse you because words like "business logic" and "passive" to me, make confusion, because for me logic is being made by the Controller, and Model holds the data so I don't get that someone can write business logic and logics at all when speaking about Model of MVC architecture. For now I can say this. Model stands for the data. So you should think of it that if you have any data (like database data), Model that you have made, will fetch that data from the database when called upon.

In short: Model stands for the data,  the View represents how the Model is visualized and Controller manipulates the state of the Model.

I strongly suggest you if you want to read in more details what MVC is to do so. I am writing here basics and trying to make things the way I have understood them. And finally a diagram to help out understand it as much as possible:

MVC Workflow


Back to real deal. Since I want, for now, to make a Blog that has Posts, I need to make Models for that. Blog model and Posts model. You should know that there is a different approach in doing this in MVC, and I will be using Code first approach, there is also Database First approach, but I will use Code first as it is easier for me to set database that way. Even though I usually do database first, I want to use Code first for this purpose as I don't do it usually simply because I want to understand it that way as well. So personal preference, if you are already used to the database first that is okay and you can do that approach as well, but I will stick to Code First as it might be more appealing to new programmers.

Classes

So... Let's create a new Class in our Model folder. But! I can't go to that step yet as I need to give an explanation what a Class is. That is why I wanted to make this Pt.1 of Database building as there is few things needed to be understood before actually making that first Model. Sure, you can skip this if you already know what a Class is. This will be anyhow very basic for now.


When I first time heard about Class, I've been told "it is like real life object" and it is also one of many things that gave me a headache. Because, after they told me "it is like a House" I was like "really...?" and then they start saying "house has a door and windows, furniture" and I go again like Mental Expression - because they keep telling what the House, as a building have, and don't make a connection to what it has to do with Class in a programming language! So I'll try to explain it the way I understood it after a couple of projects dealing with them. I have also to say that Classes are fundamental in Object-Oriented Programming.

You can indeed think of a Class like an object, but I'd rather say first - think of it as an object you see on your screen. Let's say you have a webpage. It provides some text for users to read, but you want on that webpage to add... A basket. So basket is an object right? I mean you can imagine it, you saw some people carry baskets in the shop most likely... So if you are getting the picture you have an object that represents a basket. So what do we want that basket to be used for? Since I already said it is a basket on the webpage, let it be a shopping basket for users to put some products into it. So we have an object that is shopping basket. Therefore, our class is a shopping basket. You define classes as this:

public class ShoppingBasket
{

}

In order for our class, or basket to receive something, you need to give it properties. You can't in programing expect that anything will work if you didn't write to the program to do so. So even if we created our class, it won't do anything until we tell that class what it needs to have. So our basket should have something that identifies which basket it is. Like you don't have one basket in a shop that 15 people use, it would lead to riots... Not to mention if it is a wedding dress shop. So you have 15 different baskets for each person. But since in programming, we need to track shopping baskets and who is using them, we need to give them some identification. So we give them an ID number:

public class ShoppingBasket
{
    private int ID { get; set; }    
}


NOTE: I would really want to explain what is private, and what does get; and set; do, but this post would be much longer than it is aimed to be. I must point out, private is right of access in object oriented programing of the given property, in our case ID property. If you really want to know OOP (object oriented programming) then you should dedicate some time to understand it. For now I will stick to what is needed for our Blog Project. 


What else do we need to know about our basket object? Who is using it right? I mean in real life shop you won't go to the person and say "give me your wallet so I can check your name and know who is using my baskets!" and do it with a bandana on your head. That is why for me comparing classes to the real life objects don't really make sense to begin explaining with and about. Instead, since we are thinking about the object on the screen, that represents a shopping basket on the webpage for users that want to buy something, we need to know which users are using the basket, so that later on when they bring their basket on a check out, we know who is the person buying and which products. Even in real life shop, when the basket is getting to the cashier it doesn't arrive there by itself. Usually has a human companion. So having that in mind, we add properties like these:

public class ShoppingBasket
{
    private int ID { get; set; }
    private string UserName { get; set; }
    private string Products { get; set; }
}


Okey, so this far what we have done, is far from perfect and what class actually is but you do have a starting point and this is mostly what we will need for now. There are a lot of things in order to our class serve the purpose it should, like List of  products and then making the class being type of List and many other things that are all most OOP. And I don't want to confuse you about that nor make you feel you waste your time anymore, my point was to show you a very very basic understanding of what a Class is in programming. When it comes to comparing Classes of real life objects, it is true you can think of real things as programming objects. For example, if you are making an application that is being used on a bus station? So you would need to think that Bus is an object, it has this and that amount of seats, then, that this and that Bus is driving to this and that town and such, but I didn't want to talk about this way of seeing objects before you see the purpose of it in the programming.

In the next post we will use classes you've just read about to make our Models that will store data to the database. Not the ShoppingBasket it self, but the Blog and Post Class.