Search This Blog

Thursday, 25 August 2022

Quick Tech Bits - N-Tier Architectecture Calls Vs Data

 

Here is a quick diagram of how the call hierarchy vs  data flow hierarchy in an n-tier architecture takes place.

  • The Face in the diagram is the end user who interacts with the Presentation Layer (PL) of your product.
  • The Services Layer (SL) is introduced if the Presentation Layer becomes polyglotic. If your product is a website (Using Java) like Gmail and is also available as Gmail Android App, IPhone App, then all these three applications will need to be synced with the same business logic, Database Servers right?
    • Hence we need a project that uses Rest Services (WebApis), SOAP Services so that all the Apps can access the Business Logic, Databases in a language and framework agnostic way over http.
  • The Business Logic (BL) consists of the product's core logic and is basically a collection of Class Library  projects (producing portable executables like Dlls)
  • The Data Access Layer (DAL) uses a set of technology adapters that can communicate with DBMS residing on different servers.


CALL Hierarchy (marked in red)

When the end-user clicks a button on the UI 

==> A call to ==> Services Layer takes place 

=>which=>calls the ==> Business Logic Layer Methods 

=>which=>calls the==> Data Access Layer 

==>finally calling ==> DBMS Queries / Stored Procedures / Functions / Views / Commands


DATA FLOW Hierarchy

The data is returned after the call in the reverse direction (marked in green)

Looking at the Data Flow Hierarchy, as a developer you can know which projects created in the respective layers need to be referenced and how.

The direction of adding project reference in the N-tier architecture is the same as the Data Flow Hierarchy direction.

Eg: Data flows from DAL to BL, hence DAL project will be added as a reference to BL project

Data Flow Hierarchy is used to seed your product architecture, create boiler-plate code for the product.

Hope this helps.





SQL Cursor Syntax - Quick Tech Bits

 Cursor: 

  • Is used to do a different processing row by row of the select query
  • The column names displayed need to be formatted or validated or processed before showing output. This process can give different results per row. 
  • Hence cursor is used

Eg: Showing culture specific / language specific outputs for the same data. Here cursor can be used

Eg: Creating a comma-separated string for a given table data


- TIP: Imagine you are working with a file when writing the cursor syntax. Makes it easier to visualize and understand the syntax

- File is created, then opened, then processed line-by-line, then closed and removed from memory.

- The same holds for a cursor


- SYNTAX

1.     DECLARE <cursorName> CURSOR FOR
2.               <select query>
3.     OPEN <cursorName>
4.     FETCH NEXT FROM <cursorName>
5.     INTO <variable names one each for selected  columns>
6.    
7.     <your logic for processing>
8.    
9.     CLOSE <cursorName>
10.   DEALLOCATE <cursorName>

*** CURSOR SHOULD ALWAYS BE USED AS THE LAST RESORT 

*** DUE TO PERFORMANCE CONCERNS

Check this repo for an example script of the MoviesDb and run the Cursor example.

The script files below should be run in Microsoft SQL Management Studio / Visual Studio in the same order.

MoviesDB Script: Creates a MoviesDB database with sample data

Cursor Script: Contains an example for cursor & how it can be replaced with a normal SQL query.

Hope this helps.

Wednesday, 3 August 2022

C# Change console Forecolor

 

While working on console applications in .net, its nice to display different colored fonts for different statuses.

For example: Warnings should show up in yellow, errors in red, success messages in fluorescent green.

This is particularly useful when you write Logger modules for your products.

#1 Change Foreground Color

1.  Console.ForegroundColor = ConsoleColor.Green;
2.  Console.WriteLine($"Update successful!");
3.   
4.  Console.ForegroundColor = ConsoleColor.Red;
5.  Console.WriteLine($"There was an error: Invalid input");
6.   
7.  Console.ForegroundColor = ConsoleColor.Yellow;
8.  Console.WriteLine($"Please use Pascal naming conventions"); 
9.    

# Output