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:
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.
I have a base class, say
BaseObject, that has the public
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>. …
I want to specify validation rules for the objects I am using in my typescript apps in the most ergonomic, hence elegant, manner possible.
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.
The basic tool I will use here are Joi and a set of abstractions I will present in coming paragraphs :
let myArray = [1,2]
undefined, as in
// -> 
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…
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. Passing the service to the client, rather than allowing a client to build or find the service, is the fundamental requirement of the pattern. …