Image for post
Image for post
Photo by Nicholas Barbaros on Unsplash

When we need a single instance of an object, of a specific class, to exist in our application, we use the Singleton pattern. Now, the pattern itself is frowned upon in some circles and is seen as an anti-pattern.

Some of the reasons the Singleton is seen as an anti-pattern are:

  • It can be technical debt in disguise. The assumption that there will only be a need for one instance of the class during the lifetime of the application can be falsified in the future. This can lead to tedious refactoring.
  • The introduction of global state into the application.
  • The previous point can make unit testing hard if your singleton changes some global state. …


Image for post
Image for post
Photo by Matt Schwartz on Unsplash

The Aim

I want the objects I use in my TypeScript code to let me set multiple properties on them in one go. My objects must have a method setProps such that when given a properly typed map of properties, the method sets the corresponding properties on the objects.

The Approach

I have a base class, say BaseObject, that has the publicsetProps method. setProps must take parameters that are compatible with the instance of a class derived from BaseClass. This is achieved with generics. The method will look like the following piece of code:

I constrain the type parameter T to represent a type that inherits from BaseObject by using the syntax <T extends BaseObject>. …


Image for post
Image for post
Photo by Adeolu Eletu on Unsplash

My Aim

I want to specify validation rules for the objects I am using in my typescript apps in the most ergonomic, hence elegant, manner possible.

github: https://github.com/kanian/ts-decorator-validation,

npm: https://www.npmjs.com/package/ts-decorator-validation

Tools

I will use Joi and JavaScript decorators. Here’s a small UML diagram to summarize the approach:


Image for post
Image for post
Photo by John Doyle on Unsplash

My Aim

When writing back-end NodeJS APIs in Typescript, I don’t want to worry about the validity of the data my services are consuming or producing. Given I have correctly captured the business rules and the data validity criteria, I want to be able to trust that the data I am working with is always valid.

Tools

The basic tool I will use here are Joi and a set of abstractions I will present in coming paragraphs :


Image for post
Image for post
Photo by Thomas Kelley on Unsplash

Introduction

Essentially, initializing arrays in javascript can happen in one of four ways:

  • With Array literal initialization, such as in let myArray = [1,2]
  • With Array initialization with undefined, as in new Array(arrayLength)
  • With Array initialization with given elements, as in new Array(el1,el2)
  • With the Array.prototype.of method, as in // -> [7]

This being known, there eventually comes a time when a learning javascript developer will need to have more control over the initialization of arrays. In the coming sections we will explore common array initialization questions, as well as some solutions for the power javascript developer to-be.

Question: How to initialize an array with the same value?

It’s a recurring question. That is the developer wants to initialize an array, of a given length with the same value in each slots of the array. Obviously, what the developer is asking here is what is the most elegant way to do so. A straightforward way to do so…


Image for post
Image for post
Photo by Ricky Kharawala on Unsplash

Why?

A few weeks ago, I decided to revisit Dependency Injection (DI) in the PHP world and quickly build a simple one. I looked at various article including the one by Mustafa Magdi and that by Andrew Carter, among others. With all the powerful and well know PHP DI containers out there, why go through the trouble? I figured that there must be some lessons to learn.

Before going any further, DI can be defined as:

[A] technique whereby one object (or static method) supplies the dependencies of another object. A dependency is an object that can be used (a service). An injection is the passing of a dependency to a dependent object (a client) that would use it. The service is made part of the client’s state.[1] Passing the service to the client, rather than allowing a client to build or find the service, is the fundamental requirement of the pattern. …


Image for post
Image for post
Photo by Gilly Stewart on Unsplash

In a previous article, I describe an approach to a generic PHP Singleton pattern that can be summarized by the following diagram:

About

Patrick Assoa Adou

Solutions Architect and Coder

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store