Posts

Showing posts from November, 2020

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...