study nildana's blog

The official blog of all the blogging that is done by the folks at study nildana

Abstract and sealed classes

 

Abstract class

Abstract class are marked by the keyword ‘abstract’ in their class definition which are typically used define the base class in the hierarchy. Speciality of this abstract class is that no object of this class can be instantiated but can make derivations of this.

An abstract class can contain either abstract method o non abstract method. Abstract methods do not have any implementation in the abstract class, but the same has to be provided in the derived class. It is not necessary that abstract class should contain only abstract members, but it can also contain non abstract member.

 

Sealed class

Sealed classed are marked by the keyword ‘sealed’ in their class definition which are typically used to restrict the inheritance feature of object oriented programming. Sealed class can never be used as a base class, hence it cannot be a abstract class.

Understanding namespaces

One of those things that bugged me a lot (and even today as I write this) are namespaces. You could be designing the model of your project or the controller or some other method functionality or class or whatever it is, you will find this word ‘namespace’ stamped right at the top (after all the libraries including code is written) of your c sharp file.

namespace can be an odd concept to wrap your head around, but when you get what they are doing, you will be happy that they are there. One every day way of looking at namespaces, is to think of your workspace. Now, as I look around my own workspace or desk space, I see a lot of things. I see my laptop, which has its large external monitor connected. I see my water bottle (ah! The water bottle again) and my headphones when my surroundings get too loud (they frequently do). I also see my trusty wireless keyboard and wireless mouse from Microsoft. I see my bag, and notebooks and my post its. I also a huge jumble of cables that connect to my many phones and other USB devices.

Now, my wireless keyboard and the water bottle have nothing in common. Same goes the phone on my desk and the employee card in my bag. However, they all have one thing in common – me, and my work space. All these objects belong to me and they get to live in my workspace, and that means they are all related.

Similarly, in a project, a namespace is like a virtual work space in which your project lives and breathes. Thanks to the way object oriented programming divides, not everybody can access each other. In fact, many of the things you put in your project may never interact with each other. For instance, the models you design in your project will never know where the database is located, where the actual data is stored. Yet, they are both part of your namespace.

This is also the reason why, when you add something, you will notice that the code file automatically includes that in your namespace. This is similar to you buying a new pen and keeping it on your desk. The pen is a new adding (like that new controller or the model class) and now belongs to your work space (just like that controller now belongs to your project namespace).

Also, this is an interesting proposition when it comes to namespaces. It is actually possible (and essential when you are working on truly large projects) to have namespaces that extend beyond the current project solution that you are working on. That means, you could create two different projects in visual studio, and then have both of them have the same namespace, although they are not on the same project at all. That is when you start watching matrix in a room filled with mirrors, and lose all idea about what is real what is not.

Good luck with that :)

MVC simple project

So far, you have been reading up a lot of stuff about MVC. Assuming you are following the set sequence at our reading order link (find it here), you have all the essential knowledge to figure out the part about creating a new project in visual studio.

Under visual studio ( usually run visual studio as administrator) , you will go with File > New > Project > Web . When you do this, you will be presented with a number of web development options (including those related to Azure development). Here, go ahead and choose ASP DOT NET MVC. Before you continue with the actual project creation, there are some other components on this page. Let’s explain them a bit.

Application Insights

Even if you are not familiar with it, analytics is a big part of any application that will be deployed. Analytics allows those who are managing the application to figure out which sections and which methods, which pages, which apis (and the list just goes on and on) are being accessed by the user during application usage.

Google Analytics is probably the most used analytics package and it works with almost any web enabled app. Application Insights (a cute name for sure) is the Microsoft version of analytics stuff. So, when you are creating a new project, you could leave this box checked, to have the necessary libraries included in your project. If you are planning not to do analytics (which you probably don’t wish to when you are just learning MVC or your client does not care for analytics right now) or planning to use something else (like Google Analytics), you should uncheck this box.

Git Repository

I have written about version controls (this is separate from software versions) and git in some of my posts. You can find them here.

As with application insights, visual studio allows you to attach the necessary git files into your project from the word go. If you are using version control in your software development, you will probably want to leave it checked. If not (actually, I cannot imagine any software developer not using version control but you know, to each his own) then leave it unchecked.

For some reason, I don’t like to use GUI tools for version control. So, I actually leave the ‘git’ option unchecked. Then, I use the gitbash command line tool to do the version control, after the project has been created. So, that is how roll. You might be different.

Other Stuff

Of course, you will also have the option to choose the project location on your computer. I usually leave it where it is.

You should also give it a meaningful name. I never leave the default project name, as it makes me look lazy and it comes bundled with all the problems associated with not having a meaningful name.

Moving on

Okay then, once you taken care of the above, you will be presented with a big box with lots of options. Obviously, you will select MVC. Along with there are some more options here.

Change Authentication

Making things easier is a running theme when it comes to visual studio, as it should be with any IDE that wants to be cool (and visual studio is already the living incarnation of coolness). Keeping that in mind, Microsoft allows you to include a pretty powerful authentication system and bundle it into your project from the word go.

There are four options here. Usually I go with ‘Individual Authentication’ for two reasons. Mostly because most of my projects have used individual authentication system (where you have the standard username and password system that is created by the user). Further, it also provides Facebook and Google and other login systems. Which is also cool.

Another reason why I like to leave ‘Individual Authentication’ on, is because it gives a ready to use connection string (which you will find in web config) for future use. I know it’s silly but well you know, I just like it that way.

Azure

You will also notice that there is an Azure hosting option provided. As always, this can be useful, just like the authentication system. However, I have noticed that doing the Azure thing even before the project is built is kind of unnecessary. Also, sometimes visual studio does not work all that well with your Azure management portal. In addition to that, I really doubt that your office or manager or IT will give you direct access to Azure.

So, most of the time you will leave it unchecked. However, if you are checking out Azure, then you will keep it checked, and let it do its thing.

Unit Tests

Yeah, leave it unchecked for now.

Moving on

Okay, now that you checked and unchecked what you want, the project will be created for you. Depending on your computer, it may take a minute or up to 5 minutes. So, be patient and let it do its thing.

Once it loads, before you do anything else, just build the project and run it. Then, do your other development stuff. If you have enabled authentication, in the website that is running, register and login. If that works, that means the connection string is working which in turn means, the database is working fine and it also means entity framework is installed and is working.

MVC in asp dot net

Hopefully you reached after reading some theory I have written about the MVC design pattern itself. In this write-up, I am going to focus on how asp dot net provides the MVC platform.

MVC pattern is one of the three ways you can build an asp dot net website. You can find out more about the other here, here and here. The asp dot net MVC is most suitable for building full-fledged web applications which sometimes go with the name of ‘single page applications’. As is understood from the pattern, when you create a MVC project in visual studio, you already get a very clearly defined Model, View and Controller divisions that is ready to use.

The MVC in asp dot net would be nothing without the kind of support that comes built in with project creation via visual studio. In many ways, visual studio is the real hero when it comes to building projects on any dot net project. Okay, let me stop my love for visual studio and get back to explaining how this work.

In asp dot net, upon creating a project (actual project creation is explained here) you are immediately presented with a set of pre-declared folders that mirror M and V and C. Further, visual studio also includes all the necessary libraries need to kick the start the web development right away. That means, the project already has been ‘boostrapped’ to start working out of the box. This is extremely cool.

Now that’s about what visual studio. The actual dot net implements the MVC with its own components. When it comes to model, MVC strongly recommends and I also recommend, going ahead and using the ‘Entity Framework’ package. I have written more about Entity Framework here, but in simple terms, the Entity Framework provides you with a set of features that allows you to truly decouple the Model part from the project. Further, it also decouples the Model itself from the database.

Model is split into two parts. On one side (the side that faces that code) you define your models as classes. On the other side, (the side that faces the database) you connect to the database of your choice via connection string and the above mentioned Entity Framework. I will also add here that Entity Framework seamlessly converts your model classes into the database tables. As to why this is cool and awesome, you can read more about this post I have written explaining even more stuff about Model in MVC asp dot net here.

On the View side, asp dot net uses a combination of Razor Syntax with HTML stuff. Razor syntax allows you to use c sharp type code to modify and play with data in the View. The HTML stuff does the standard web rendering. Of course, the MVC implementation here already includes JQuery and Bootstrap. This means, your project here is already mobile ready, and ready for mobile optimization from the word go. Even more stuff about the View is written here.

On the controller side, it is completely c sharp. Most of this will be familiar to you. The controller works with the models on one side, and views on the another side. Even more stuff about the controller is written here.

Overall, I am personally very pleased with how MVC is implemented in asp dot net and how cool it works with asp dot net.

What is asp dot net core

Ah! The new stuff that Microsoft is offering lately. I am not going to claim to be an expert on this. In fact, I don’t think I can lay claim to being an expert on anything. However, I read stuff and I understand some stuff and decide to write about it.

Okay, so, a little bit of story. Dot net is the key platform that Microsoft has been milking ever since it became big. You see, windows was everywhere (just like windows are there in every house, you know those wooden windows? Eh? Enjoying the pun…he he…sure) and that means, applications were designed to run on windows. Windows comes built in with dot net platform.

However, the onslaught of mobiles (and when windows phone died), suddenly put microsoft on the back foot. People are no longer using windows computer to get stuff done. They are doing it on their mobile phones and tablets and whatever else that they have that can run a browser. So, suddenly everyday people are no longer need dot net, and that means, less money for Microsoft.

In addition to this, when it comes to servers (that actually serve the web pages that people visit) have always run on competing technologies such as Linux. Those folks have never used dot net core. As the widows is used less, less dot net platform is used and the cycle keeps circling itself.

That is where asp dot net core comes into picture. For the first time ever, asp dot net is now available outside of windows. That means, dot net developers can leverage on their existing dot net skills, knowledge and expertise and have them run on non-windows platforms such as Mac and Linux an of course Windows.

How does this work really? Microsoft has the asp dot net core branding which includes the asp dot net core, and asp dot net core apps. In this core package, microsoft has put in the libraries that will run all three platforms windows, linux and mac. So, it is the ‘build once run everywhere’ promise again. Of course, dot net core has just come out of beta and has a version number that is greater than 1. So, the promise is delivered to some extent. There is that.

For me, I am yet to try out dot net core. If dot net core really can help me deploy applications on Linux, that will be pretty cool. However, it all depends on what kind of libraries are currently available. If most of the essential stuff is available, then that would be my go to library for building asp dot net applications.

what is dot net

Perhaps the very first question that one should ask when entering the world of programming in asp.net is this. What exactly is .NET or dot net? What is the relationship between this dot net and c sharp? Further, what is the connection between these two things and c sharp.

For those who are experienced, these questions are redundant. Of course, I have met developers who have gone years without really understanding what the relationship is. This does not mean, that these developers who don’t know this relationship are bad. Rather, it simply means that they already know it, but perhaps they did not realize that they knew it. It’s like when you are breathing. You have been breathing air all your life, but only when you reach school do you realize that you are in fact breathing oxygen which is essential for your survival.

Alrighty then. Let’s start with .NET or dot net. Dot net is a platform that is designed by Microsoft. To understand what platforms do, let’s look at something that we use every day. Take the chair you are sitting on. No matter what type of chair you are sitting on, you almost always know how to use it. It could be a chair in the train that will take you to Mysore. It could be the metal chair at the bus stand. It could be those foldable chairs that they put in wedding halls sometimes. One look at any type of chair, you know how to use it. You don’t really need anybody to teach you, how to use a chair, once you have learnt it at some point in your life.

A platform is kind of like that. In the above example, if you we were to think of the chair as a ‘platform’, then we are its users. Since the ‘chair platform’ shares the same common features all over the world and in all circumstances, we are able to use it. With minor modifications if any. Once you have learnt to use a chair at your home, for example, you are assured that when you travel to some other city or country, you will still know how to use a chair.

In essence, a platform guarantees certain facilities. As long as something was built keeping these facilities in mind, the builder can be assured that, okay, the users can actually use it.

Another example, but a little more technology based would be the USB pen drive. Here, we can think of three ‘components’. One of the components would be ‘USB pen drive makers’ or let’s simply call them makers. The second component, are everyday people like you and I, who buy these pen drives. Let’s call us simply buyers. Finally, the third component are ‘USB ports’ that are there in every computer and laptop. So, we have some kind of a ‘USB eco system’ thing going on here.

Someone somewhere has standardized the ‘USB platform’. This standard specifically informs the makers that as long as they follow the standard manufacturing design of USB, any pen drive they build can be used on any computer or laptop. Similarly, the buyers can be sure that as long as they are buying a pen drive, and it has USB pin, it will definitely work with their laptop and computer.

Thanks to this standard platform, both buyers and makers, can happily live in this ‘USB ecosystem’. Extending this to our own question from the top of this blog, dot net is the ‘standard platform’. The platform itself is standardized by Microsoft, since Microsoft owns the .NET platform. Dot net is part of the ‘software ecosystem’ and the makers are ‘software developers’. The users are anybody who uses any software that is built this dot net platform, and hence are part of the dot net ecosystem.

Here, Microsoft, the owner of the standard, is making a ‘promise’ to its main parties. The developers and users.

To the developers, Microsoft promises something like this. Look developers, as long as you are using the dot net platform to develop your apps, we will promise you that your application will be usable by any user who is also running the ‘compatible’ dot net platform.

To the users, Microsoft promises something like this. Look users, buy a software that was built by a dot net developer, and we promise that it will run on your dot net system that is ‘compatible’.

You will notice that I have been careful enough to use the word ‘compatible’ in both the promise paragraphs. The significance of this ‘compatibility’ is explained in a future post. Till then, what you need to understand is that dot net is a platform, around which the dot net ecosystem lives.

We are looking at dot net ecosystem from a developer perspective that will lead to becoming an asp dot net developer. Read on to find out about the role played by c sharp in dot net ecosystem. Further, the role played by asp dot net.

The C in MVC

Earlier I have written about the design pattern here, and its implementation in asp dot net. Here, I am going to take a deeper look at what the Controller.

As the name itself gives it away, the controller is what ‘controls’ the entire MVC web project. One way to look at the controller is to think of a project manager who is ensure that his two subordinates – Model and View, are working flawlessly.

The controller will send ‘views’ to the View. The controller takes information that is sent by the View. Then, the controller will process the information. This processing, if it involves models, it will take care of those as well. For instance, if the information that came from the View is actually a query for data, then the controller will perform the necessary queries. This would involve speaking with the model component of the project. This will also involve interacting with the databases which involves all the usual database activities like creating stuff, reading stuff, updating stuff and deleting stuff.

Then, when the database responds, it will then generate the new views, and pass it along to the actual View. The controller is also responsible for implementing all the security stuff as well. Of course, authentication systems are also implemented in the controller. To put it simply, the controller is the brain of the project.

Another fantastic role that the controller plays is furthering the ‘decoupling’ agenda of the software design. While the View does access the Models defined, the View component and the Model component are almost entirely independent of each other. The View interacts mostly with the Controller when it wants to show something. When the View gets something from the user, it passes it on to the controller.

The Model (specifically that part with the database) interacts only with the controller. In fact, the Model (the database part) has no clue what data has been shown and where and how. All it knows is that the controller is asking for some data, and the model’s job is to return and then keep quiet. In fact, this is especially useful when you are building web apis on asp dot net. In a web api project, you respond by sending out JSON responses. In a MVC project, you respond by sending out Views. Otherwise, the MVC single page application and the web api project, are essentially the same, come to think of it.

 

Design Pattern – MVC

This particular protocol does not have anything specifically connected to dot net. However, it does explain the concept of MVC.

Before we dig deeper into what an MVC is, first we need to understand what design patterns are. What do they do and why do people (developers) use them?

Design patterns are commonly accepted designs that can speed up software development. Usually, a popular design would have been researched properly before being proposed. Further, a lot of peers in the software industry would have given their input and fix things that could be problematic. Eventually, design patterns become accepted and they are used in everyday developer life.

An extremely simple design pattern would be the relationship between a HTML file and the web page. To elaborate, this HTML file contains all the HTML and script markup. No other files and folders are referenced. The HTML file contains all the code that is required by the web browser to render the page. The browser does not have to look anywhere else. All it needs is in that HTML file and the work is done.

Of course, the above simple design pattern will never be used in an actual website. Even the most basic site will at least refer to external script libraries and styling libraries. But you get what we are trying to understand here. Design patterns.

One such design pattern is MVC. The obligatory expansion is Model View Controller. As the expansion clearly indicates, there are three major components to MVC, and each of them plays an essential role when developing the application. The key to the MVC pattern (and something that is common across design patterns in general) is de-coupling.

So, what exactly is de-coupling? Breaking up people who are together? Well, sort of.

You see, the software development is sort of like building a castle with lego bricks. There are so many moving parts working together and somehow getting things to work. This is annoying sometimes but it is necessary. Another way to look at any software, is a chain made of links, and each link from a different company or platform or manufacturer and similar endless variations.

Since it is a given that software will almost always be a series of links, it is essential that we ensure the links are connected well. The connection must be strong. At the same, developers must also work with the assumption that as time goes, just like real life links, the software links also grow old and rust. That means, a time will come when a given link will have to be replaced.

Now, the issue of replacement is where challenges arise, and this is where I will jump into explaining the part about ‘coupling’ and ‘decoupling’.

When these links are working together, they are essentially attached to each other. However, if they are tightly linked, then the two components almost end up becoming one. Imagine a pen and its cap, and the cap is so tight that you cannot remove it. If the cap cannot be removed, then the whole pen becomes useless, and you are now forced to buy a new cap. The same problem can happen with software. A ‘tight coupling’ happens when two separate components (like that pen and the cap) are so tightly connected to each other, they cannot be separated from each other. When everything is working fine, this is actually good. However, a day will come when one of these two components or links have to be replaced. Then, to your horror, you find that they are so tightly coupled that replacing one component means, replacing the other as well.

A ’loose coupling’ is the opposite of this. In this situation, yes two components are once again linked with each other. However, when one of them develops a problem or needs to be replaced, the two are easily separated. Then, after the replacement is installed, they can be connected again the software works as before. In other words, the replacement activity does not break the system.

With design patterns and MVC, that is the goal here. To keep things linked but also allow repairs and replacements as necessary. At the highest level, there are three components that are present here. You have the ‘Model’, ‘View’ and the ‘Controller’. The Model relates to everything related to data. The View relates to everything the users see on the browser. The controller is what sits in the center, and co-ordinates everything. The idea is that, if necessary you can replace and repair the View or the Controller or the Model (as long as you keep the bridges that link them together intact) and still have the system running without problems.

Hopefully, you have a rough idea about what MVC design pattern is. I have talked about MVC in asp dot net here.

What is this localhost? And that number that comes after those words?

Whenever you run a website (after building) in visual studio, you will notice two things. On the bottom right corner of your PC, you will see a new icon appearing. And, the address bar of your browser will show something like ‘http://localhost:6969

I have observer even experienced developers wondering (or outright ignoring) what these two things are. Hence, making some notes here.

The blue icon

Okay the blue icon is actually IIS (Internet Information Services) running on your PC. The IIS is what makes your computer act like a server for the duration of the visual studio running your website. For extremely obvious reasons (like how your PC is not supposed to be web server), it won’t run all the time. However, your computer must pretend to be a web server if you are going to run web apps, and that is why IIS runs and then promptly quits when you stop running your web app from visual studio.

Of course, it is possible to make IIS run all the time on your desktop or laptop but that would be a story for another day.

The localhost thing and that number

Okay, so, you know what the blue icon does. Now that localhost thing. Well, when IIS runs, it has to run on IP address (because for the web, the IP address is the home). That IP address when your site is running locally is the localhost. It makes sense, as you can see ‘local’ plus ‘host’ equals localhost.

And that number is just the port number. Port numbers are ‘peep holes’ through which a server listens for incoming requests. On the web, there are many types of web traffic. For instance, FTP is one type of web request. Browsers make another type of web request. API calls make another type of web request. Mail systems have their own port numbers, and so do database requests.

The number you see after localhost is the port number on which your locally running IIS is listening to web requests.

The M in MVC

Earlier, I have written about the MVC implementation in asp dot net. Now, let me go a little deeper into it in this post.

The Model part of the implementation is something that I love about asp dot net MVC. To really appreciate how this is awesome, let’s look at how websites would normally work with a database. Say you have a website that is keeping track of students at a college. You would have to design the tables first. Work out the external relationships between the tables. Work out the primary keys and foreign keys. Work out the scripts that work with these tables that include all the CRUD operations.

So, as you can see, it is a lot of work.

Now, an additional challenge is when, as the project grows, you realize that you have to change some or all of the tables. Now, that will be a back breaker because, if you had some hundred scripts, then probably all of them are affected by the change in the table design. That means, it’s up to you to not only change the tables, you also need to ensure that the new changes don’t break the database. Then, you also need to update the scripts that you are using everywhere.

Now, I might be exaggerating things a bit, there is no denying that yes, the work involved will be heavy. The M in MVC brings to the table the Entity Framework. Now, do you remember the decoupling of things I was talking about in when discussing MVC design pattern (find it here, if you have forgotten)? Well, the M in MVC allows you to tightly couple the database (thereby creating a problem). However, MVC strongly recommends that you use Entity Framework.

Thanks to EF, your MVC app need not have a tight coupling with the database. You know how you design variables before you use them in your application? Similarly, you design the classes that will be used for storing data in your application. Then, you had over these classes to EF and wash your hands of it. The EF will then, using its own built in technologies will take care of doing the CRUD with the database. You don’t have to worry about designing the database. You don’t have to worry about writing the SQL scripts. You tell EF what to do, and the EF will take care of actually interfacing with the database. And that is awesome!

Now, there is even more awesomeness at your disposal. Think of that above mentioned scenario where you have to change some of the data models (which means changing the tables in the database as well). Again, you make the changes and EF will automatically update the database according to your changes. Not only that, EF will also you to come back to the previous schema, in case things did not go as planned.

Further, EF also gives you a really cool Seed method which allows you to automatically populate the database with default data. Again, without you having to write SQL scripts. You will be using LINQ for that, which I will discuss at a later point of time.