Posts

Showing posts with the label writing code

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

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!

Vertical Slices Application Design with MediatR: Part 1

Image
This is the first of my two-part series about designing applications using vertical slices and how the open-source library MediatR can facilitate this design. In this post we will talk about the concept of vertical slices and in the next post we will look at some sample code that implements this design. The second part of this series is now out: Vertical Slices Application Design with MediatR: Part 2

Top Tips for Creating Maintainable Software

Image
If you write code on a regular basis, then no doubt you have come across code that is difficult to understand and, consequently, difficult to change. If you're like me, then you will most probably have written bad code yourself! The good news is that writing good code doesn't have to be a complicated exercise. Today I'm going to share some principles and practices that you can use to write maintainable software.

Be Very, Very Lazy - A New Perspective on the Single Responsibility Principle

Image
Today I'm giving a tip on how we can remember to follow the Single Responsibility Principle. I'm sure many of you will be able to relate to this tip (at least I surely can!) which is: be very, very lazy!

The Good and Bad of Writing Software

Image
The good thing about writing software is that you can do anything you like. The bad thing about writing software is that you can do anything you like.

Some Tips on Organizing Code

Image
One of the signposts pointing to clean code is well-organized code files. Contrary to popular belief, aesthetics and organization are important when it comes to writing code, because having organized and structured code improves readability. A piece of code written once will get read multiple times, so it's worth the time to put a little more effort into improving the code's readability. In this post I will be sharing some tips on how to make more organized and readable.

Developer Mindset - Software as a System

Image
In this post I will share a mindset that would help you build cleaner and more maintainable code. I am calling this as thinking of "Software as a System".

Libraries and Frameworks

Image
Today we are going to take a look at libraries and frameworks. We will describe each of them and take a look at some examples for each. We will also take a look at a practical example of how being able to distinguish between the two will help us in our daily coding.

Why Use ViewModels?

Image
Let's talk about a simple but important topic: why use viewmodels in our web applications. Specifically, the kind of viewmodels that we will be talking about are the ones that are used as parameters or return values in controllers.

Why I Don't Use the Repository Pattern When Working with Entity Framework

Image
Today I'm going to talk about a slightly controversial topic about using the repository pattern in combination with an ORM such as Entity Framework.

Creating a Generic Lookup Service

Image
In a previous post I talked about how we can manage lookup tables with Entity Framework Code First . In that post I suggested using the primary key directly to check for a specific lookup, rather than introducing an arbitrary column that will serve as an identifier. In this post I will talk about how we can make a generic lookup service to simplify how we show lookup values in, say, dropdown lists.

Managing Lookup Tables with Entity Framework Code First

Image
Oftentimes in our applications we will have such things as a "lookup table". I am defining a lookup table as a list of relatively fixed or static choices such as status codes, states or provinces, and so on. In this post I will share how I manage lookup tables using Entity Framework.

Strongly-Typed AppSettings

Image
Application settings for .NET projects provide a way to change the behavior of a program without recompilation. These settings are typically stored as key-value pairs in the web.config or app.config files and can be accessed through the ConfigurationManager class provided by the framework. In this post we will be taking configuration access to the next level by providing a strongly-typed wrapper around the ConfigurationManager class.

Create Meaningful Classes

Image
In a previous post I talked about the importance of good naming when writing code. The title of that post is "Don't Make Me Think... About Your Code". This post will talk about meaningful classes and can be considered as an addition to the "writing code" series.

Don't Make Me Think... About Your Code

Image
"Don't Make Me Think" is the title of the famous UX book by Steve Krug. One of the main ideas in the book is that a user interface should be designed in such a way that users' thinking is kept to a minimum. As much as possible, it should be obvious what actions the user must do to achieve a goal. Answers to questions like "where do I go to checkout?" or "what will happen when I click this button?" should be obvious just from looking at the page.