Skip to main content

Posts

Showing posts from August, 2019

C# Extension Methods

Extension Methods in C# Extension methods allow you to add methods to an existing type without changing then existing type or creating a new derived type. The most common extension method in the LINQ operators added to the existing System.Collections.IEnumerable types. To use standard query operators for most of the <IEnumerable> types add the using directive in your file, using System.Linq;  (You may also need to add a reference to System.Core.dll) class ExtensionMethods2 {       static void Main()      {            int[] ints = { 10, 45, 15, 39, 21, 26 };            var result = ints. OrderBy(g => g);             foreach (var i in result) { System.Console.Write(i + " "); }      } } //Output: 10 15 21 26 39 45 Extension methods are created as non-generic static methods but called by instance method syntax. namespace ExtensionMethods {     public static class MyExtensions     {            public static int WordCount(this String str)           {                 return

Polly C# Tutorial

Polly C# Tutorial Polly is an open-source .Net library used to handle retry logic in your application.  There may be cases when you might want to retry a service call if it fails due to transient errors. Transient errors include errors like Server currently busy, database not available, Not enough resource to process requests etc. Polly provides a callback mechanism where you can retry and configure the number of retries that you want to perform until you record the failure. How to install Polly Nuget package-  1. dotnet CLI Command -  dotnet add package Polly 2. Install the nuget package from Visual Studio - Search for Polly in 'Manage nuget packages' Retry Call without using Polly  - The below implementation does not look pretty to read as it obscures the actual logic to call the service. private void CallExternalService() {       const int RETRY_ATTEMPTS = 3;       for (int i =0, i<RETRY_ATTEMPTS,i++)       {            Thread.Sleep(i * 100);            CallExternalServic

LINQ - Deferred and Immediate Execution

LINQ - Deferred and Immediate Execution LINQ Queries can be categorised based on execution behaviour. Deferred Execution The execution of an expression is delayed until its realized actual value is required.  Deferred execution can be applied on an in-memory collection as well as remote LINQ providers like LINQ to SQL, LINQ to XML etc.  It greatly improves performance by avoiding unnecessary execution. Deferred execution reevaluates on each execution which is called LAZY Evaluation. It always gives the latest data of the collection when executing. Deferred or Lazy Operators - These query operators use deferred execution. Examples - Select, where, Take, Skip 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 List<Student> Students = new List<Student> { new student () { Id = 1 , Name = "Mike" }, new student () { Id = 2 , Name = "Gracy" }, new student () { Id = 3 , Name = "Mitch" } }; var numbers =

How to Improve performance of REST APIs

Ways to improve REST API performance and best practices Use Compression Techniques Use compression techniques like gzip or Brotli to compress the data transferred from server to client. .NET Core has an inbuilt Response Compression Middleware for compression (default: gzip). GZIP is used for file compression and decompression for faster network transfer. Also, reducing the size of the response usually increases the responsiveness Use Asynchronous methods Asynchronous methods help to maximize the number of concurrent requests that can be handled by the API at a given point in time. By implementing asynchronous methods, you can leverage the multiple cores of the server and maximise application's throughput. Increases Scalability. Call data access, I/O and long-running operations asynchronously Use faster Data Access Strategies In order for an app to be responsive, it is essential to retrieve data from database faster and efficiently. One suggestion would be to use ADO.NET over O

Mongo DB Overview

Latest Version - 4.4 MongoDB ATLAS - Atlas takes care of time-consuming costly administration tasks and get the database resources you need quickly MongoDB Cloud Providers - AWS, Google Cloud, Azure Why MongoDB No SQL database? Performance - A single object document retrieved from a single place in a disk  compared to SQL database where data is spread across multiple tables which in turn needs to access multiple places in the disk to retrieve  data.  Scalability - No SQL databases are compared to be more scalable than SQL databases. For eg. Mongo DB has built-in support for replication and sharding (horizontal partitioning of data) for scalability. Hugh amount of hardware and human resources are needed for SQL databases to support scalability. Replication - Has Inbuilt functionality to replicate data to multiple servers for better availability (24*7), No downtime needed for maintenance, Disaster Recovery No need to ORM layer which reduces overhead compares to SQL where ORM sh

MOQ .Net Mocking Framework Overview

MOQ Unit test Framework Mocking is the ability to isolate code which will run in production with a test version so that external resource calls like database calls, web service calls can be mocked for easier unit testing. Benefits of Mocking Execution of tests becomes faster with mocking objects Supports parallel development when the functionality is not yet developed or dependent on another team Isolates specific components from the actual testing components MockBehaviour MockBehaviour.Strict - Using this type will throw an exception if the mocked method is not setup. All the methods of the mocked object need to be setup. There is a chance existing tests might fail if an object is strictly mocked. MockBehaviour.Loose - Using this type will not throw an exception if all methods are not set up and hence fewer lines of code in our unit tests. MockBehaviour.Default - By default, all mocked objects have a Default mock behaviour Argument matching in mocked methods It