JavaScript Callbacks

I will call back later!”

A callback is a function passed as an argument to another function

This technique allows a function to call another function

A callback function can run after another function has finished.

// The add() function is 
// called with arguments a, b
// and callback, callback 
// will be executed just
// after ending of add() function
function add(a, b, callback) {
    console.log(`The sum of ${a} 
    and ${b} is ${a + b}`);
    callback();
}
 
// The disp() function is called just
// after the ending of add() function
function disp() {
    console.log(`This must be printed
     after addition`);
}
 
// Calling add() function
add(5, 6, disp)

Reference

https://developer.mozilla.org/en-US/docs/Glossary/Callback_function

https://www.geeksforgeeks.org/promise-vs-callback-in-javascript/

https://www.w3schools.com/js/js_callback.asp

Creating multi-column Dropdown in ASP.NET Core

Generally, in Asp.net core, to display multiple columns in the select elements, we could write our own control or use some JQuery plugins.

Here are some examples;

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/option

https://dzone.com/articles/creating-a-multi-column-dropdown-in-aspnet-mvc

https://learn.microsoft.com/en-us/answers/questions/1251310/multi-column-drop-down-list-javascript-control-opt

Software Design Principles

Design principles form the foundations that design patterns are built upon. They are more fundamental than design patterns. When you follow proven design principles, your code base becomes infinitely more flexible and adaptable to change, as well as more maintainable.

Common Design Principles

There are a number of common design principles ;

Keep It Simple Stupid (KISS)
An all-too-common issue in software programming is the need to overcomplicate a solution. The goal of the KISS principle is concerned with the need to keep code simple but not simplistic, thus avoiding any unnecessary complexities.

Don’t Repeat Yourself (DRY)
The DRY principle aims to avoiding repetition of any part of a system by abstracting out things that are common and placing those things in a single location. This principle is not only concerned with code but any logic that is duplicated in a system; ultimately there should only be one representation for every piece of knowledge in a system.

Tell, Don’t Ask
The Tell, Don’t Ask principle is closely aligned with encapsulation and the assigning of responsibilities to their correct classes. The principle states that you should to tell objects what actions you want them to perform rather than asking questions about the state of the object and then making a decision yourself on what action you want to perform. This helps to align the responsibilities and avoid tight coupling between classes.

You Ain’t Gonna Need It (YAGNI)
The YAGNI principle refers to the need to only include functionality that is necessary for the application and put off any temptation to add other features that you may think you need. A design methodology that adheres to YAGNI is test-driven development (TDD). TDD is all about writing tests that prove the functionality of a system and then writing only the code to get the test to pass.

Separation of Concerns (SoC)
SoC is the process of dissecting a piece of software into distinct features that encapsulate unique behavior and data that can be used by other classes. Generally, a concern represents a feature or behavior of a class. The act of separating a program into discrete responsibilities significantly increases code reuse, maintenance, and testability.

The S.O.L.I.D. Design Principles

The S.O.L.I.D. design principles are a collection of best practices for object-oriented design. Allof the Gang of Four design patterns adhere to these principles in one form or another. The term
S.O.L.I.D. comes from the initial letter of each of the five principles;

Single Responsibility Principle (SR P)
The principle of SRP is closely aligned with SoC. It states that every object should only have one
reason to change and a single focus of responsibility. By adhering to this principle, you avoid the problem of monolithic class design that is the software equivalent of a Swiss army knife. By having concise objects, you again increase the readability and maintenance of a system.

Open-Closed Principle (OCP)
The OCP states that classes should be open for extension and closed for modification, in that you should be able to add new features and extend a class without changing its internal behavior. The principle strives to avoid breaking the existing class and other classes that depend on it, which would create a ripple effect of bugs and errors throughout your application.

Liskov Substitution Principle (LS P)
The LSP dictates that you should be able to use any derived class in place of a parent class and have it behave in the same manner without modification. This principle is in line with OCP in that it ensures that a derived class does not affect the behavior of a parent class, or, put another way, derived classes must be substitutable for their base classes.

Interface Segregation Principle (IS P)
The ISP is all about splitting the methods of a contract into groups of responsibility and assigning interfaces to these groups to prevent a client from needing to implement one large interface and a host of methods that they do not use. The purpose behind this is so that clases wanting to use the same interfaces only need to implement a specific set of methods as opposed to a monolithic interface of methods.

Dependency Inversion Principle (DIP)
The DIP is all about isolating your classes from concrete implementations and having them depend on abstract classes or interfaces. It promotes the mantra of coding to an interface rather than an implementation, which increases flexibility within a system by ensuring you are not tightly coupled to one implementation.

Dependency Injection (DI) and Inversion of Control (IoC)
Closely linked to the DIP are the DI principle and the IOC principle. DI is the act of supplying a low level or dependent class via a constructor, method, or property. Used in conjunction with DI, these dependent classes can be inverted to interfaces or abstract classes that will lead to loosely coupled systems that are highly testable and easy to change.

In IoC, a system’s flow of control is inverted compared to procedural programming. An example of this is an IoC container, whose purpose is to inject services into client code without having the client code specifying the concrete implementation. The control in this instance that is being inverted is the act of the client obtaining the service.

LINQ Operators

LINQ to Objects extends any type that inherits from IEnumerable (which is almost every collection class in .NET, from simple Arrays to List<T>) to support query operations similar to those available in SQL. We can write queries using any of the built-in Standard Query Operators, or add our own operators if we need to. The standard operators cover a wide variety of categories, at present there are over fifty that form the backbone of LINQ. To get an idea of their scope, here is a list of those operators available to us –

Operator TypeOperator Name
AggregationAggregate, Average, Count, LongCount, Max, Min, Sum
ConversionCast, OfType, ToArray, ToDictionary, ToList, ToLookup, ToSequence
ElementDefaultIfEmpty, ElementAt, ElementAtOrDefault, First, FirstOrDefault, Last, LastOrDefault, Single, SingleOrDefault
EqualityEqualAll
GenerationEmpty, Range, Repeat
GroupingGroupBy
JoiningGroupJoin, Join
OrderingOrderBy, ThenBy, OrderByDescending, ThenByDescending, Reverse
PartitioningSkip, SkipWhile, Take, TakeWhile
QuantifiersAll, Any, Contains
RestrictionWhere
SelectionSelect, SelectMany
SetConcat, Distinct, Except, Intersect, Union


Most of the operators should be familiar if you have ever worked with a relational database writing queries in SQL.

One important distinction between writing SQL queries and LINQ queries is that the operator order is reversed. If you are used to Select-From-Where-OrderBy, it might take some time to overcome the muscle memory and move to From-Where-OrderBy-Select.