Getting started with ASP.NET Core 1.0 (formerly ASP.NET 5.0)

The article talks about .NET Core simplifying programming, yet still not being simple enough for some developers due to its OOP nature. Ends with sample code in ASP.NET/C# Core containing funny comments exposing some of OOP complexities.

Getting started with ASP.NET Core 1.0 (formerly ASP.NET 5.0)

I love C# and I’m excited about Microsoft finally removing most of the unnecessary Microsoft-specific crap out of .NET and catching up with modern technologies in their new .NET Core framework. Now we can create leaner, faster and more efficient apps that utilize only those components we need, and in a raw mode where we have full control of our app’s functionality. Yet, C# or ASP.NET in general, are not even mentioned in some of the recent surveys about web technologies. Neither is Java. And I suspect I know why, because I’m one of the people who believes C# still could be simpler. Way simpler. Basically in some technologies, say JavaScript or PHP, you’re able to utilize some object-oriented programming (OOP), but you can also just open an editor and start writing lines of code that do what you need. There is also an ongoing debate on whether OOP is the best approach for everyone:

And although OOP is the required and expected standard of most professional developers, not everyone that can create amazing and successful apps can or needs to grasp OOP. Some guys created apps that have millions of daily visitors and are just laughing at Microsoft/.NET and Oracle/Java whose own websites are sometimes slow, inefficient, difficult to navigate, even plain ugly. And in the current internet age where we try to promote and teach everyone coding, it may not make sense to make programming so difficult that it actually turns most people off. People often don’t want to or can’t learn how to code only because we make coding more complex than it needs to be. Others simply say “screw OOP and screw Java and screw ASP.NET”, and they turn to variety of procedural and scripting languages that actually let them create what they want and not force them into learning things they don’t want to learn and don’t need. I am, or would be, one of such people?—?I am unable to fully grasp OOP concepts, yet with the right tools I can create better and more powerful apps than most developers out there. And I can design and create them faster. This was also one of reasons a while back my company has created tools like CodeCharge and CodeCharge Studio, which would generate OOP code but expose additional code hooks (events) to developers who could simply type in additional code without knowing any OOP. And this is actually commonly used approach, where any app may expose events and APIs to developers who can then write additional code routines that interact with the app. Yet, this also serves as an example of different “classes” of developers: those that create advanced OOP apps and those that can only extend them by plugging-in their own additional functionality. Microsoft perfectly understands this as their own MS Access is an example of an advanced software internally utilizing OOP, yet which allows developers to add their own functions, sub procedures, and macros that execute custom code:https://msdn.microsoft.com/en-us/library/dd897495(v=office.12).aspx

And the question may be why do we use such double standards, exposing simpler level of programming to “regular class” of developers, yet we don’t make it easy to program directly in ASP.NET. Microsoft basically says you can’t just write a line of code like this:

Yet, to program in ASP.NET/C# you basically need to hire a developer who knows OOP and will help you wrap the simple Print instruction in some kind of class and then teach you how to write other classes to do the most basic stuff. Here is how “Hello World” looks like in ASP.NET Core:

Yep, there are 4 lines of code that don’t do anything useful, and yes, this can be learnt. But it’s already unnecessarily complex for someone who doesn’t understand why they can’t just simply print “Hello World” without understanding what means ‘using System’, or namespace, or public, or static, orclass, or void, why they need Main() section, and what else they will need later. And indeed this is just the beginning. After couple lessons in ASP.NET or OOP many people will say ”To hell with ASP.NET, I’m going to find something simpler because I know what I want to do and just need to create it, not screw with some ‘classes’ and deal with Microsoft. Shouldn’t programming be accessible to everyone?! Why is Microsoft even pretending they want people to learn how to code if they can’t make coding itself easy?!”. And possibly this is also one of reasons why simpler technologies are often more successful. At least I happen to believe that C# could enjoy much wider acceptance if it only supported basic, simple, accessible programming.

Back to my use of ASP.NET/C#, recently I started experimenting with .NET Core 1 and creating my own super advanced app that will change the world (because whose app isn’t intended to change the world?) and try to forgo OOP principles. It will be written in a procedural way where possible, mainly using so called public static classes as functions that I will then execute using very basic code. Well, at least until I engage developers who will try to restructure everything into OOP and make this more advanced... Though it can be a fun challenge to see whether I can keep OOP to minimum, or whether our developers will later prevail with converting it to OOP, or maybe we’ll figure out some common ground. Is there a common ground, anyone?

For now I’m starting with creating a basic API service (small program that outputs data from database as JSON text) using .NET Core and trying to understand what I’m doing, because that’s what you need to do with ASP.NET: understand what ta hell you’re doing. And to remember what ta hell I’m doing I’m also heavily commenting my C# code. Below is my application startup file (Startup.cs) with just 5 lines of useful code and about as much of non-useful code, and lots of my comments. There are also a few comments generated by MS Visual Studio when creating the skeleton of my app. The code itself doesn’t actually do much yet except including some .NET features for later use in my app, yet you can see how over-complicated the code structure already looks.

On a serious note, with ASP.NET Core Microsoft is actually simplifying .NET development a bit, so things are getting better and easier, even for people like me. Wishing everyone happy coding with ASP.NET Core!

using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting;
using Microsoft.Extensions.DependencyInjection;
namespace MyAPI
{
public class Startup
{
//MS: Entry point for the application.
//KM: This function doesn't do anything useful except being required by Microsoft to do nothing useful.
//KM: Somehow it starts something somewhere, as if getting here didn't require starting something already.
public static void Main(string[] args) => WebApplication.Run<Startup>(args);
//MS: This method gets called by the runtime. Use this method to add services to the container.
//MS: For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
//KM: More Microsoft BS requiring to create this method just to write a few lines of code to add some services, because we can't just write a few lines of code without stupid classes and methods.
//KM: No idea what's IServiceCollection and why we need it, but going with the flow.
public void ConfigureServices(IServiceCollection services)
{
//MS: Add framework services.
//KM: I guess this enables some MVC features, just no idea why we have to add the same thing again in the "Configure" class below.
services.AddMvc();
//KM: Enable API/Controller access from all clients.
//KM: Haven't tested this yet, but it's supposed to allow this server (or specifically Controller) to accept external requests from any host or IP address.
//KM: Based on http://stackoverflow.com/questions/31942037/how-to-enable-cors-in-asp-net-5
services.AddCors(options => options.AddPolicy("AllowAll", p => p.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader()));
}
//MS: This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
//KM: HTTP request pipeline my ass. Can't you learn plain English for God's sake?
//KM: I'm guessing this class is again needed just to add a few lines of code specifying some .NET features I may need to use.
//KM: No idea what's IApplicationBuilder and why we need it, but going with the flow.
public void Configure(IApplicationBuilder app)
{
//KM: Guessing this is needed to run this app on IIS. KM:TODO:Figure out if we want to use IIS or how to replace it with Kestrel
app.UseIISPlatformHandler();
//KM: I guess this also enables some MVC features
app.UseMvc();
//KM: Include something named "Cors" to allow Controller to accept requests from external IPs. Because life would be too easy if anyone could just run anything they want from anywhere.
//KM: Based on http://stackoverflow.com/questions/31942037/how-to-enable-cors-in-asp-net-5
app.UseCors("AllowAll");
}
//Note: database connection string is stored in Globals.cs
}
}