Sunday, June 12, 2022

How to cache API response - Angular.

 


C# 9.0 - initialisation of mutable objects

 C# 9.0 - initialisation of mutable objects


Initialisation of mutable objects simplified in c# 9 using




dot net 3.0 -Asyn streams

 


Nullish Coaleasching - Angular 12

 Angular 12 : Nullish Coaleascing


Allows developers to replace complex epressions : {{ age !=  null && age != undefined ? age: calculateage()}}




Angular 11 vs 12 major differnces.


Friday, May 27, 2022

C++ simple example for Factory code to complex example.

 Simple factory method code:

#include <iostream>
#include <string>

using namespace std;

/*
 * Define an interface for creating an object, but let 
 * subclasses decide which class to instantiate. 
 */
 
class Shape {
    public:
       virtual void Draw() = 0;
       static Shape* ShapeFactory(string type);
};

class Circle : public Shape {
    public:
       void Draw() { cout << "I am circle" << endl; }
       friend class Shape;
};

class Square : public Shape {
    public:
       void Draw() { cout << "I am square" << endl; }
       friend class Shape;
};

class Rectangle : public Shape {
    public:
       void Draw() { cout << "I am rectangle " << endl; }
       friend class Shape;
};

Shape* Shape::ShapeFactory(string type) {
    if ( type == "circle" ) return new Circle();
    if ( type == "square" ) return new Square();
    if ( type == "rectangle" ) return new Rectangle();
    return NULL;
}

int main(){
   Shape* obj1 = Shape::ShapeFactory("circle");
   Shape* obj2 = Shape::ShapeFactory("square");
   Shape* obj3 = Shape::ShapeFactory("rectangle");
   obj1->Draw();
   obj2->Draw();
   obj3->Draw();
}
Abstract Factory:










using AbstractFactoryPattern;
 
AbstractFactory factoryI = new ConcreteFactoryI();
Client clientI = new Client(factoryI);
clientI.Run();
 
AbstractFactory factoryII = new ConcreteFactoryII();
Client clientII = new Client(factoryII);
clientII.Run();
public class VehicleBuilderProvider : IVehicleBuilderProvider
	{
		private readonly Dictionary<VehicleType , IVehicleBuilder> _vehicleSet;

		public VehicleBuilderProvider(CarBuilder car , BykeBuilder byke)
		{
			_vehicleSet = new Dictionary<VehicleType, IVehicleBuilderProvider>()
			{
				{ VehicleType.Car, car},
				{ VehicleType.Byke, byke}
			};
		}

		public IIVehicleBuilderProvider Get(VehicleType vehicleType)
		{
			return _vehicleSet[VehicleType];
		}
	}
Dot net.: 
Controller and factory :
public class StreamController : Controller
{
private readonly StreamFactory streamFactory;
public StreamController(StreamFactory streamFactory)
{
this.streamFactory = streamFactory;
}
[HttpGet("movies/{userSelection}")]
public IEnumerable<string> GetMovies(string userSelection)
{
return streamFactory.GetStreamService(userSelection).ShowMovies();
}
}
public class StreamFactory
{
private readonly IServiceProvider serviceProvider;
public StreamFactory(IServiceProvider serviceProvider)
{
this.serviceProvider = serviceProvider;
}
public IStreamService GetStreamService(string userSelection)
{
if(userSelection =="Netflix")
return (IStreamService)serviceProvider.GetService(typeof(NetflixStreamService));
return (IStreamService)serviceProvider.GetService(typeof(AmazonStreamService));
}

Thursday, April 21, 2022

Clean code architecture and CQRS pattern based web api using .NetCore

 Interesting thing about this article is bring ing mediaR pattern into clean architecutre which can support CQRS or command and Query much effectively at the same time manage required layers supported by Cleanr architecutre. 

Easy and useful Pattern to make code clean and  structure. 


Clean Code Architecture

Clean Code Architecture or often called Onion Architecture is a layered architecture that makes your project easy to manage and test.
You can find the common pictures of this concept from visual diagram below.
Alt Text
If you don't know about clean code architecture, there are some useful links you can visit.

CQRS and the Mediator Pattern

CQRS

CQRS stands for “Command Query Responsibility Segregation”. The main purpose of this pattern is to split responsibility of commands (saves) and queries (reads) into different models.

If we think about commonly used CRUD pattern, we think it as working on same data model to perform CRUD operations. For an example if we have a user model class, we usually performs the CRUD operations on that user model class. However CQRS would instead have us split these operations into two models, one for the queries (Read), and another for the commands (Create-Update-Delete).

Alt Text

Mediator pattern

Mediator pattern is used to reduce communication complexity between multiple objects or classes. This pattern provides a mediator class which normally handles all the communications between different classes and supports easy maintenance of the code by loose coupling.

Alt Text


We can see in the image above, Controller Service sends a message to the Mediator, the Mediator then invokes one or multiple services to handle the message. There is no direct dependency between green and blue components.


MediatR library for CQRS and Mediator pattern


MediatR is a .Net library that is used to implement CQRS with mediator pattern. However this library has a limitation. It only works on 'in process' CQRS implementation. The term 'in process' means .NET library that manages interactions within classes on the same process. It is not an appropriate library to use if we wanted to separate the commands and queries across two systems. In that case, a better approach would be to a message broker such as Kafka or RabbitMq.

Setting up project

Project Setup

First off, let’s open Visual Studio and create a new ASP.NET Core Web Application, selecting API as the project type.

Project Dependencies

Let’s install a couple of packages via the Package Manager Console.
PM> install-package MediatR
PM> install-package MediatR.Extensions.Microsoft.DependencyInjection
PM> install-package FluentValidation.AspNetCore
PM> install-package FluentValidation.DependencyInjectionExtensions
PM> install-package Microsoft.AspNetCore.Mvc.Versioning
PM> install-package MongoDB.Driver
PM> install-package Swashbuckle.AspNetCore.Swagger
PM> install-package Swashbuckle.AspNetCore.SwaggerGen
PM> install-package Swashbuckle.AspNetCore.SwaggerGen
PM> install-package Swashbuckle.AspNetCore.SwaggerUI
PM> install-package System.Linq.Dynamic.Core
PM> install-package AutoMapper
PM> install-package AutoMapper.Extensions.Microsoft.DependencyInjection
PM> install-package Autofac
PM> install-package Autofac.Extensions.DependencyInjection

You can find GitHub link here

How Netflix Scales its API with GraphQL Federation (Part 1)

  Netflix is known for its loosely coupled and highly scalable microservice architecture. Independent services allow for evolving at differe...