Posts

Init-only Properties

.NET 5 is out! And with it, C# 9, which adds a lot of new features to the language. In this post, I'm going to talk about one of my favorites, init-only properties . Immutability Before C# 9 Before C# 9, we can make a class immutable by using getter-only auto properties together with a constructor whose arguments are used to populate those properties: public class Product { public Product(string name) { Name = name; } public string Name { get; } } This is an effective way for making classes immutable. We cannot change the property's value after the class has been instantiated: var product = new Product("Cool Product"); product.Name = "Cooler product"; // compile error We even can't change it from within the class: public class Product { public Product(string name) { Name = name; } public string Name { get; } public void SomeMethod() { Name = "Cooler product...

Web API Without Controller

If the mantra of "thin controllers, fat models" is followed to the extreme, it would result in very short controller methods, sometimes even only a couple of lines long. Today I am experimenting with doing away with controllers entirely, and just mapping routes directly to methods that execute the business logic. The code in this post can be found in: https://github.com/ojraqueno/web-api-without-controller Setting Up the New Project First, I created the web API project template using dotnet new : dotnet new webapi -n ControllerLessWebAPI That will create a new API project for me inside the ControllerLessWebAPI folder. I can run that project by going into the directory and running dotnet run : dotnet run It will start the website, which in my case is located in localhost port 5001. If I go to https://localhost:5001/weatherforecast , I can see some random JSON data being returned. Removing the Controller The starter template contains a WeatherControlle...

Getting Started with Python

I usually talk about .NET topics, but today I want to talk about Python. Big shout out to my partner, who is a Python developer, for inspiring me to write about Python. Python Summary For the .NET people out there, here is a quick summary of the Python programming language: Concise and readable syntax, suitable for programmers of all levels Dynamically typed and interpreted, ideal for rapid application development and prototyping Consistenly rated as one of the most popular programming languages in the TIOBE Index and StackOverflow survey Widely used for web and data science applications To install Python, go to https://www.python.org/downloads/ and download the version you want. I would recommend getting the 3+ version. I am using version 3.9.0 for this post. During installation, it might ask you if you want to add Python to the PATH. I would recommend checking this option so that you can run Python from any location in your command prompt or terminal. Pyth...

ASP.NET MVC vs ASP.NET Core: Practical Differences

Image
ASP.NET Core has been around for a while, but not everyone is using the new framework yet. If you're someone who's migrating from ASP.NET MVC to ASP.NET Core, you might be wondering what the differences between the two frameworks are. In this post, I will share the main differences between the two, from the point of view of a developer who has worked with both. Here is an overview: Dependency injection is built-in. Controllers are unified. Client-side assets are centralized. And more - read below! Dependency injection is built-in Dependency injection is a popular pattern in object-oriented languages that helps make code maintainable. With ASP.NET MVC, developers had to make use of 3rd-party NuGet packages to enable dependency injection in their projects. In ASP.NET Core, dependency injection is baked into the framework. There is no need to use any NuGet package, although using 3rd party DI packages is still supported. Dependency injection is supported n...

Solved: Unable to publish to Azure App Service from Visual Studio

Image
I recently created an Azure App Service and tried to publish a new ASP.NET Core web app using its publish profile downloaded. When i did that, I encountered an error: Web deployment task failed. (Could not connect to the remote computer ("[redacted].scm.azurewebsites.net"). On the remote computer, make sure that Web Deploy is installed and that the required process ("Web Management Service") is started. Lear more at: http://go.microsoft.com/fwlink/?LinkId=221672#ERROR_DESTINATION_NOT_REACHABLE.) The requested resource does not exist, or the request URL is incorrect. Error details: Could not connect to the remote computer ("[redacted].scm.azurewebsites.net"). On the remote computer, make sure that Web Deploy is installed and that the required process ("Web Management Service") is started. Lear more at: http://go.microsoft.com/fwlink/?LinkId=221672#ERROR_DESTINATION_NOT_REACHABLE. The remote server returned an error: (404) Not Found. Here...

Immutability

Last time, we looked at pure vs impure functions and we said that functional programming favors pure over impure functions. Using pure functions leads to another concept: immutability . Immutability means that a variable can only have its value set once. After that, the value never changes. So much so that in functional programming, the phrase "assigning to a variable" is often replaced by "binding to a symbol", since the word "variable" is not applicable anymore. Let's consider a non-immutable example: // Declare a class named Car public class Car { public string Color { get; set; } public bool IsStarted { get; set; } } // Instantiate a new instance var myCar = new Car { Color = "Red", IsStarted = true }; // After that, change the values of the car's properties myCar.Color = "Blue"; myCar.IsStarted = false; The properties inside of the Car class are not immutable since they can be changed after...

Pure vs Impure Functions

Image
My partner, who is also a developer, once asked me what the difference between functional and object-oriented programming is. There are many differences, and this post explores one of them: the usage of pure and impure functions. What are pure and impure functions? A pure function does not have side effects. A side effect is a change in state that happens outside of a function that gets persisted even after the function goes out of scope. Impure functions are everything else. Here are some examples to clarify. First, a pure function: // Pure int GetSum(int a, int b) { return a + b; } That is a pure function, because it does not affect any state that is outside the scope of the function. Consider this impure equivalent: // Impure int sum = 0; void ComputeSum(int a, int b) { sum = a + b; } That is an impure function, because it modifies the sum variable, and the modification persists even after the method has gone out of scope. When using an object-or...

My Vue.js Experience

Image
Last year I wrote about Getting Started with Vue.js . I've been using Vue.js extensively from around that time up until now. In this post I will share my experiences with using the framework. It's Still Very, Very Easy With other JS frameworks I've tried, building simple apps and tutorial-type apps were easy. But, as the application's complexity grew, the development effort grew as well. For example, when working with AngularJS, it sometimes wasn't easy to use 3rd party plugins. Because AngularJS was a framework, any 3rd party plugins had to have an AngularJS port for them to work properly. If such a port doesn't exist, extra work is involved in order for them to work with AngularJS (eg. calling $scope.$apply() in appropriate places). That problem simply doesn't exist in Vue.js (at the very least, I haven't experienced that yet). I can use popular JS packages such as axios , lodash , even jQuery and integrate them into my app with absolutely ...

The Tyranny of Horizontal Architectures (and How You Might Escape): Part 2

Image
In part 1 of this series , I talked about the pains that I and my colleagues have experienced when working in code bases that used N-tier data-centric architectures. In this post, I will detail one approach which has allowed me to escape that madness. Lasagna Code We've all heard the term "spaghetti code". But there's also "lasagna code": "The object-oriented version of spaghetti code is, of course, 'lasagna code'. Too many layers." - Roberto Waltman — Programming Wisdom (@CodeWisdom) February 24, 2018 I use that statement to remind myself that layers can sometimes do more harm than good. That also reflects my experience. Of course, it's not really the number of layers that's the problem, but rather, how the dependencies between those layers are set up and what abstractions those layers represent. Consider this familiar diagram of a data-centric N-tier architecture: Or even a diagram of a DDD-inspired but sti...

The Tyranny of Horizontal Architectures (and How You Might Escape): Part 1

Image
You've seen it before. You have been tasked to make a seemingly simple change on a web page. Add a couple of fields, perhaps. Or add new functionality, but still CRUD-like in nature. "Piece of cake," you say. You then proceed to make a change to the HTML. Then to the viewmodel. Then to the business logic layer, which I will also call service layer. Then to the repository layer. Then to the database. (You could also do it in reverse.) All those steps just to add a couple of fields. Which is kind of okay, maybe no big deal. But, as the infomercial says, "Wait, there's more!" Exhibit A: Implementing Functionality Involving Multiple Entities Before we immerse ourselves in the madness, let's set up the domain: an online retail site. In your database, there are Product and Order tables, which have a many-to-many relationship between them. You are following a data-centric N-tier architecture, and therefore there is a repository layer with classes su...

Getting Started with Vue.js

Image
I've been using AngularJS for the longest time. I decided to level up my front-end development knowledge, and part of that is learning about new front-end frameworks. Angular and React are the most popular ones these days, but I also stumbled on Vue. I immediately liked it. In this post, I share how easy it is to get started using Vue. Even easier than AngularJS! It's Very, Very Easy To get started using Vue, follow these steps: 1. Create a new HTML page and add a reference to the Vue script file. You can download a copy or reference it via a CDN: <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> 2. Create an element that Vue will manage. Normally this would be a div . Give it an id: <div id="app"> </div> 3. Inside, create a data-binding target using curly brace syntax or mustaches, similar to what is used in other frameworks like AngularJS: Hello, {{message}}! 4. Through JavaScript, cr...

Azure Quickstart: SQL Azure

Image
In a previous post, we talked about hosting a web app with Azure app services . Now, we are going to see how to setup a SQL database on the Azure platform. Creating an Azure SQL Database To create an Azure SQL database, follow these steps: 1. Click on SQL databases on the hub on the left. That will take you to the SQL databases blade, where you can see your existing databases. 2. Click on the Add button or on the Create sql database button. The blade to create the SQL database will appear: 3. Fill in all of the required fields. Database name : This will be the name of the database, for use in the connection string. Subscription : This is the name of the subscription where this SQL database will be a part of. Resource group : This is the name of the resource group where this SQL database will be a part of. Select source : We are creating a new, empty database, so select Blank database. Server : This is the SQL Server instance where the database will live. One SQL ...

Azure Quickstart: Web Hosting with App Services

Image
There was a time when hosting a web application and the backing infrastructure was a long and rather expensive process. But with the advent of the cloud, this process has become much, much easier. This post is about setting up a web application in Azure. Within minutes, you can get your own web app up in Azure. Let's get started! Creating an Azure Account The first step is to create an account on Azure. To do that, you need the following: A Microsoft account A mobile phone (for identity verification) A credit card (for identity verification) The credit card details are needed only for identity verification purposes. It will not get charged unless you explicitly tell Azure to. Here is the registration link: https://azure.microsoft.com/en-us/free/ Congratulations, you now have an Azure account! Accessing the Azure Portal The Azure portal is the starting page in Azure. From it, you can create web apps, create other resources (there are a lot to choose from), view...

Deploying a Web Application to Local IIS using Visual Studio 2017

Image
In this post we are going to see how to deploy a web application on your development machine. The end goal is to have a web application running on the development machine outside of the context of Visual Studio and which is accessible to other devices on a local network. Install IIS Before we get started, we need to make sure that IIS is installed on our machine. Here are the steps to do that in Windows 10: Open the search dialog by pressing WinKey + S or by clicking the magnifying glass icon. Type in "Turn Windows features on or off" and click on the matching result. You'll see the "Windows Features" window (see sample image below), which has a list of folders with checkboxes beside them. Look for the folder "Internet Information Services" and make sure that the checkbox is filled with either a check or a black square. If it is, you're good to go. If the checkbox is empty, check it and click OK. Windows will install IIS for you. ...

Vertical Slices Application Design with MediatR: Part 2

Image
This is the second post of my two-part series about designing applications using vertical slices and how the open-source library MediatR can facilitate this design. In the first post we talked about the vertical slices design and how it addresses some of the problems in traditional database table-centric designs. In this post we will see how we can implement a vertical slices design using the open source library MediatR. Put on your programmer hat as we'll dive into some code!