How to install Kafka and use it with .Net in details

today we will talk about how to use Apache Kafka messaging in. Net ;

but first, let take a look about what is KAFKA and it’s the main terminology and why to use it    ;

kafka is open source stream processing platform developed by the Apache Software Foundation written in Scala and Java for more details got to https://kafka.apache.org/  its provide a unified, high-throughput, low-latency platform for handling real-time data messages

Kafka depends on the following components:

  • Kafka Cluster: a collection of one or more servers known as brokers
  • Producer – the component that publishes the messages
  • Consumer – the component that retrieves and consume messages
  • ZooKeeper – software developed by Apache that acts as a centralized service to maintain the configuration information across cluster nodes -in a distributed environment –

How to install KAFKA on windows 10 :

1- Go to the Kafka downloads page and download the binary package  then unzip the package in a particular path let say D:\kafka_2.11-2.2.0

kafka1

in this article, we will use the ZooKeeper which will be included in the package itself so no need to install the ZooKeeper package 

now go the Kafka directory to this folder D:\Kafka\kafka_2.11-2.2.0\config and open server.properties file with any text editor TextPad or notePade++ which I prefer 

then change the path of the Kafka log to be like thislog.dirs =: \Kafka\logs or to any other directory you want; this is the path where Kafka will write its log the events and other log information.

you can change the port of the Kafka Zookeeper  by changing the value zookeeper.connect=localhost:2181  and  note that Kafka will run the port 9092 as the default port 

another thing you have to know that Kafka Message  is represented as a key-value pair. and Kafka converts all messages into byte arrays.

2- Now let us start the  Zookeeper by writing the following on the command window 

D:\Kafka\\kafka_2.11-2.2.0>.\bin\windows\zookeeper-server-start.bat .\config\zookeeper.properties

kafka2.png

2- Now let us start the  Kafka by writing the following on another new  command window 

D:\Kafka\kafka_2.11-2.2.0>.\bin\windows\kafka-server-start.bat .\config\server.properties

kafka3.png

3- after we start Kafka we need to create a new topic to use it on sending and consuming the messages 

so open new CMD window and write the following

D:\Kafka\kafka_2.11-2.2.0>.\bin\windows\kafka-topics.bat –create –zookeeper localhost:2181 –replication-factor 1 –partitions 1 –topic Hello_Salem_Topic

kafka4.png

now we are ready to create the consumer and the producer for  , we can create them using the command line but in this articl we will create them using c# language so we need to create to console application  (consumer and producer ) and install this package from the NuGet Package manager kafka-net , for more details about the package you can refer to https://github.com/Jroland/kafka-net 

4 -Go and create new Console application name it KAfaProducer

and then  in the package Manager Console write the following

Install-Package kafka-net

kafka5

now in the main method write the following

static void Main(string[] args)
{
var options = new KafkaOptions(new Uri(http://localhost:9092”), new                  Uri(http://SERVER2:9092”));

         var router = new BrokerRouter(options);
var client = new Producer(router);

client.SendMessageAsync(“HelloWorldTopic”, new[] { new Message(” My Kafka first Message “)  }).Wait();

using (client)
{

}

Console.ReadLine();
}

 

now the same thing for the consumer, create a console application and install the kafka-net then in the main method write the following

static void Main(string[] args)
{
var options = new KafkaOptions(new Uri(http://localhost:9092”), new Uri(http://SERVER2:9092”));
var router = new BrokerRouter(options);
var consumer = new Consumer(new ConsumerOptions(“HelloWorldTopic”, router));

foreach (var message in consumer.Consume())
{
Console.WriteLine(” PartitionId={0},Offset ={1} message {2}”,message.Meta.PartitionId, message.Meta.Offset, message.Value);
}

Console.ReadLine();
}

I hope this will be a good introduction for the one of the most powerful, fast and scalable open source message broker to give you the option to dive deeper on this topic

✌ have Fun

Source Code can be found here

 

https://drive.google.com/file/d/1jiDUncJCLpSiN3-wwKcdzwJFlnRYSKBa/view?usp=sharing

Advertisements

How to secure an ASP.NET Web API with JWT

Securing you API application is the most popular thing nowadays and there is a lot of ways to take it for that; but today   I will try to explain how to use JWT in the simplest and basic way that I can ; so you won’t get lost from a jungle of OWIN, Oauth2, ASP.NET Identity…

but before let take a look at what every single term means; from their official sites

OWIN

defines a standard interface between .NET web servers and web applications. The goal of the OWIN interface is to decouple server and application, encourage the development of simple modules for .NET web development, and, by being an open standard, stimulate the open source ecosystem of .NET web development tools.

Katana

OWIN implementations for Microsoft servers and frameworks. a flexible set of components for building and hosting OWIN-based web applications on .NET Framework.

OAuth 2

OAuth 2 is an authorization framework that enables applications to obtain limited access to user accounts on an HTTP service, such as Facebook, GitHub, and DigitalOcean. It works by delegating user authentication to the service that hosts the user account,

JWT

Abstract

JSON Web Token (JWT) is a compact, URL-safe means of representing
claims to be transferred between two parties. The claims in a JWT
are encoded as a JSON object that is used as the payload of a JSON
Web Signature (JWS) structure or as the plaintext of a JSON Web
Encryption (JWE) structure, enabling the claims to be digitally
signed or integrity protected with a Message Authentication Code
(MAC) and/or encrypted.

JWT contains from three sections which encoded in base64 Header, Claims and Signature

JWT uses signature which is signed from headers and claims with security algorithm specified in the headers (example: HMACSHA256). Therefore, JWT is required to be transferred over HTTPs if you store any sensitive information in claims.

to generate JWT token there is tow way from my experience one with using the OWIN middleware and the second without using OWIN middleware and using action in your controller today we will talk about the second one because its the simplest way then in another article we can grow our example to use OWIN middleware  and inject another authorization types like Role based authorization, claim or external authorization

Now :

to create   JWT token endpoint  using the action from the controller You need to add a NuGet package called System.IdentityModel.Tokens.Jwt from Microsoft,

now go to your visual studio and create a new empty project with API type

Capture

in the console manage window  type this and press enter

Install-Package System.IdentityModel.Tokens.Jwt

co to the controller folder and add new empty API controller and name it

TokenController   

in this controller create a new method like the one below

    private const string Secret = "aGs4andKSWZUaFVlN2dzSVhPWTJicHpoMzljUndXR1I0Zm5tS2NXb29CWUZs"; 
[httpGet]        
public string Token(string username, string userEmail )
        {
            
            var symmetricKey = Convert.FromBase64String(Secret);
            var tokenHandler = new JwtSecurityTokenHandler();
 
            var now = DateTime.UtcNow;
            var tokenDescriptor = new SecurityTokenDescriptor
            {
                Subject = new ClaimsIdentity(new[]
                        {
                        new Claim(ClaimTypes.Name, username) ,
                    new Claim(ClaimTypes.Email, userEmail)
                    }),
                // the Expiration value must be stored in the web config to make it easy when you need to change it 
                Expires =DateTime.Now.AddMinutes(20),
 
                SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(symmetricKey), SecurityAlgorithms.HmacSha256Signature), 
                 Issuer = "T2 - Business Research & Development", 
                 IssuedAt =DateTime.Now,
                 
            };
 
            var secureToken = tokenHandler.CreateToken(tokenDescriptor);
            var token = tokenHandler.WriteToken(secureToken);
 
            return token;
        }
    }

now if you test your method at the postman you can get your first JWT token

token

Copy your token and past it here https://jwt.io/

JWT

now we finishing the JWT  producer and we need to implement the way we authorize the user with

we need to generate new custom attribute that inherits from AuthorizationFilterAttribute

let’s create a new class  CustomAttribute and we need to override the OnAuthorizationAsync method like this

private const string Secret = "aGs4andKSWZUaFVlN2dzSVhPWTJicHpoMzljUndXR1I0Zm5tS2NXb29CWUZs";
 
     public override Task OnAuthorizationAsync(System.Web.Http.Controllers.HttpActionContext actionContext, System.Threading.CancellationToken cancellationToken)
     {
         if(actionContext.Request.Headers.Authorization==null || string.IsNullOrEmpty(actionContext.Request.Headers.Authorization.Parameter))
         {
             actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized, "You Are not Authorized to use this Resource");
             return Task.FromResult<object>(null);
         }
         var principal = GetUserPrincipal(actionContext.Request.Headers.Authorization.Parameter);
 
         if (!principal.Identity.IsAuthenticated)
         { 
             actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized, "You Are not Authorized to use this Resource");
                 return Task.FromResult<object>(null);
         }
 
          var identity = new ClaimsIdentity(principal.Claims, "Jwt");
           IPrincipal user = new ClaimsPrincipal(identity);
 
           //User is Authorized, complete execution
                return Task.FromResult<object>(user );



public static ClaimsPrincipal GetUserPrincipal(string token)
{
try
{
var tokenHandler = new JwtSecurityTokenHandler();
var jwtToken = tokenHandler.ReadToken(token) as JwtSecurityToken;

if (jwtToken == null)
return null;

var symmetricKey = Convert.FromBase64String(Secret);

var validationParameters = new TokenValidationParameters()
{
RequireExpirationTime = true,
ValidateIssuer = false,
ValidateAudience = false,
IssuerSigningKey = new SymmetricSecurityKey(symmetricKey)
};

SecurityToken securityToken;
var principal = tokenHandler.ValidateToken(token, validationParameters, out securityToken);
return principal;
}

catch (Exception)
{
return null;
}
}

now Create new  Controller and give the nave Value  then put the CustomAttribute as an Attribute on the controller level then, this attribute will check if the request has the authorization to execute the method or not if not it will return Unauthorized HTTP status code with custom message otherwise it will allow the request to access the Resource

the controller will be like this

[CustomAttribute]
  public class ValueController : ApiController
  {
 
      [HttpGet]
     
      public string Get()
      {
          
          return "Helloo";
 
      }
  }

Test Screen Shoot below

This slideshow requires JavaScript.

Design Pattern:: 1- Strategy

today we’ll talk about the design pattern, specifically about one of the most used design pattern in the programming world which is the strategy design pattern.

but firstly why we talk about this topic and why we need to know such these things?

as dofactory website define the design pattern

Design patterns are solutions to software design problems you find again and again in real-world application development. Patterns are about reusable designs and interactions of objects.

Strategy Design pattern :

is a behavioral design pattern that enables an algorithm behavior to change at the runtime

Example  of that

Sorting with Custom comparer, Log4Net  ;

and now to the Code  :👏🐱‍👤🐱‍👤

now imagen that we have a game with different levels the, while the player on the game he can get some extra tool and his power will level up, by that we need to change the power of the hero at the runtime , for that we need to use the strategy

we need a base interface to define the signature of the hero functionalities let us name is IHero and be like this  :

public  interface IHero
  {
      string Fight();
      void ChangeMyPower(ISuperPower superPower);
  }

after that, if we this in the hero we think about the superpowers and the capabilities for each one and because we have severl superpower we need to create another Interface for the power say its name is ISuperPower , and it will be like this 

public interface ISuperPower
    {
        string ShowPower();
    }

now we need to implement the superpowers that we need in our application  and to make our application simple will implement just four classes, Weapons, Jumb, FlyPower, Disappearance

public class Disappearance : ISuperPower
  {
      public string ShowPower()
      {
          return "My power now is Disappearance i can walk and no one can see me  ";
      }
  }
public class FlyPower : ISuperPower
    {
 
        public string ShowPower()
        {
            return "My power now is flying    ";
        }
    }
class class Jumb : ISuperPower
   {
       public string ShowPower()
       {
           return "My power now is Spider Strings and Jumbing ";
       }
   }
public class Weapons : ISuperPower
   {
       public string ShowPower()
       {
           return "My power now is Weapons i can fight with Weapons ";
       }
   }

now our superPowers are  ready to use let us create our hero class

public class Hero : IHero
   {
       //instance of the super power 
       ISuperPower _currentPower;
 
       // we construct  it with default power here
       public Hero() : this(new Jumb())
       {
 
       }
 
       public Hero(ISuperPower superPower)
       {
           _currentPower = superPower;
       }
 
 
       // implement the Ihero interface method to change the super power when we need 
       public void ChangeMyPower(ISuperPower superPower) => _currentPower = superPower;
 
       // to let our hero fighting the bad
       public string Fight() => _currentPower.ShowPower();
 
   }

now we have two choices the one and the best is to create specific Hero classes like (Spiderman , Batman, FlashMan …) inherited from the Hero to make our hero functionality extendable and it will be like this

public Class   cSpiderMan : Hero
    {
        public SpiderMan() :base (new Jumb())
        { 
        }
       
    }
public Class  BatMan : Hero
   {
       public BatMan() :base(new Weapons())
       {
 
       }
        
   }
public Class SuperMan:Hero
   {
       public SuperMan() :base (new FlyPower())
       {
 
       }
   }

now our functionality is ready to execute

[TestMethod]
     public void TestMethod()
     {
         IHero spiderman = new SpiderMan();
         Assert.Equals(new Weapons().ShowPower(), spiderman.Fight());
         spiderman.ChangeMyPower(new FlyPower() );
         Assert.Equals(new Weapons().ShowPower(), spiderman.Fight());
         spiderman.ChangeMyPower(new Weapons());
         Assert.Equals(new Weapons().ShowPower(), spiderman.Fight());
     }

 

this is a small demo for Strategy and how we can implement it I hope this will be useful for you, for any question or further feedback kindly don’t hesitate to contact me

 

👏🐱‍👤 Thank you and best wishes

implementing Jil as default serializer

Jil is an open source library, fast JSON (de)serializer, built on Sigil with a number of somewhat crazy optimization tricks.

also, JIL is one of the fastest Serializer and Deserializer libraries. see the figure below

jil

I had some situation on my previous API project where I have to find several ways to optimize my API performance, after searching and working on several tricks and option we find that if we change the serializer we use- besides several things of course –  and we may get more performance.

and from this idea, we start to search and make a POCs on what is the best Serlizser we can use it  ??

after a while, we found Jil and take some time to create implementation on this Library to be the Default Serlizser for our API project ;

because of that, I like to share this experience with you ;

Now to the Code  👏👏

First of all, we need to install Jil Library from Nuget

pm> Install-Package Jil -Version 2.16.0

after that create Class with the name  JilFormatter inhered from MediaTypeFormatter 

like the class below :

public class JilFormatter  : MediaTypeFormatter
   {
       private static readonly MediaTypeHeaderValue _applicationJsonMediaType = new MediaTypeHeaderValue("application/json");
       private static readonly MediaTypeHeaderValue _textJsonMediaType = new MediaTypeHeaderValue("text/json");
        
 
       private readonly Options _options;
 
       public JilFormatter(Options options)
       {
           _options = options;
           SupportedMediaTypes.Add(_applicationJsonMediaType);
           SupportedMediaTypes.Add(_textJsonMediaType);
 
           SupportedEncodings.Add(new UTF8Encoding(false, true));
           SupportedEncodings.Add(new UnicodeEncoding(false, true, true));
       }
 
        
 
       public override bool CanReadType(Type type)
       {
           if (type == null)
               return false;
           return true;
       }
 
       public override bool CanWriteType(Type type)
       {
           if (type == null)
               return false;
           return true;
       }
 
       public override Task<object> ReadFromStreamAsync(Type type, Stream input, HttpContent content, IFormatterLogger formatterLogger)
       {
           var reader = new StreamReader(input);
           var deserialize = TypedDeserializers.GetTyped(type);
           var result = deserialize(reader, _options);
           return Task.FromResult(result);
       }
 
       public override Task WriteToStreamAsync(Type type, object value, Stream output, HttpContent content, TransportContext transportContext)
       {
           var writer = new StreamWriter(output);
           JSON.Serialize(value, writer, _options);
           writer.Flush();
           return Task.FromResult(true);  
       }
   }

 

then use the below class  ” This code uses reflection for deserialization of JSON.”

static class TypedDeserializers
  {
      private static readonly ConcurrentDictionary<Type, Func<TextReader, Options, object>> _methods;
      private static readonly MethodInfo _method = typeof(JSON).GetMethod("Deserialize", new[] { typeof(TextReader), typeof(Options) });
 
      static TypedDeserializers()
      {
          _methods = new ConcurrentDictionary<Type, Func<TextReader, Options, object>>();
      }
 
      public static Func<TextReader, Options, object> GetTyped(Type type)
      {
          return _methods.GetOrAdd(type, CreateDelegate);
      }
 
      private static Func<TextReader, Options, object> CreateDelegate(Type type)
      {
          return (Func<TextReader, Options, object>)_method
              .MakeGenericMethod(type)
              .CreateDelegate(typeof(Func<TextReader, Options, object>));
      }
  }

 

Replace default JSON serializer

In the end, we need to remove default JSON serializer and for this, we go to the  WebApiConfig

and Write this Code

config.Formatters.Clear();  var _jilOptions = new Options(dateFormat: DateTimeFormat.ISO8601, excludeNulls: false, includeInherited: true ); config.Formatters.Add(new JilFormatter(_jilOptions));

 

at the End, Every  Library have its pros and cons  and to take the decision to user Jil or not , you have to know the support type  and this is the list of it

Supported Types

Jil will only (de)serialize types that can be reasonably represented as JSON.

The following types (and any user defined types composed of them) are supported:

  • Strings (including char)
  • Booleans
  • Integer numbers (int, long, byte, etc.)
  • Floating point numbers (float, double, and decimal)
  • DateTimes & DateTimeOffsets
    • Note that DateTimes are converted to UTC time to allow for round-tripping, use DateTimeOffsets if you need to preserve timezone information
    • See Configuration for further details
  • TimeSpans
    • See Configuration for further details
  • Nullable types
  • Enumerations
    • Including [Flags]
  • Guids
  • IList<T>, ICollection<T>, and IReadOnlyList<T> implementations
  • IDictionary<TKey, TValue> implementations where TKey is a string or enumeration
  • ISet<T>

 

 

thank you and best Luck 👏👍

HTTP Verbs (GET, POST, PUT ) in Web API

What Are HTTP Methods?

whenever a user submits a request to the server, this request contains part of it tell the user of what he wants the server to do with the request(ed) resources  HTTP methods represents those requested actions.

Which Verb to use  :

most of these days application stand on what we call it Crud Operations which stands for Create, Read, Update, Delete, and in API there is Four verbs correspond to these actions which are

Create – POST verb 
Read – GET verb 
Update – PUT verb 
Delete – DELETE verb 

in the below example we use  HttpGet to tell the user that want to read Employee and we return the employee as result with the response

[HttpGet]
public async Task<HttpResponseMessage> Employee(int id)         
{   
// do some stuff here to get what ever you want       
  return Request.CreateResponse(HttpStatusCode.OKyourEmplyeeobject);  
 }

and below we using post to add new item

[HttpPost]
public IHttpActionResult Add( Emplyee newEmp)
{
    //Creates a Emplpyee based 
    return Ok();
}

sometimes you have to support multi HTTP verb for one action like supporting put and post to add function because sometimes will face some client to your API  could not use one of these  HTTP verbs so you have to give an additional option and that will be llike below  :

[AcceptVerbs("POST", "PUT")] public IHttpActionResult Add( Emplyee newEmp)
{
    //Creates a Emplpyee based 
    return Ok();
}

 

differences between POST and PUT  :

POST and PUT actions are very similar to each other they both used to send data to the server to store or use it in some operation, and you can use either for the Create or Update scenarios, the main  differences are

  1. PUT is idempotent. What this means is that if you make the same request twice using PUT, with the same parameters both times, the second request will have no effect, This is why PUT is generally used for the Update scenario
  2. POST is not idempotent; making the same call using POST with the same parameters each time will cause two different things to happen, hence why POST is commonly used for the Create scenario

Boxing and Unboxing in C#

Boxing and unboxing are the most important concepts you always get asked about in your development life or even in the interviews you made, but what actually Boxing and Unboxing Means?

Boxing

Implicit conversion of a value type (int, float, string ..etc.) to a reference type (object),  a and as you may know value type is being allocated on the heap rather than the stack.

Unboxing

Explicit conversion of same reference type (which is being created by boxing process); back to a value type I, In unboxing process,  and this means boxed value type is unboxed from the heap and assigned to a value type which is being allocated on the stack.

For Example  :

int myValue  =  12  ;

object  boixingVal  =  myValue  // this is Boxing

 

int newValue  =  (int )boixingVal   ;  //this is the Unboxing

 

in Real Life Example  

int myId = 10;

ArrayList  idsArr = new ArrayList();

//ArrayList contains object type value So , int i is being created on heap

idsArr .Add(myId ); // Boxing occurs automatically

 int j = (int)idsArr[0]; // Unboxing occurs

Now What  ???!! 

sometimes Boxing is necessary But we need to avoid it as much as we can وsince it will slow down the performance and increase memory requirements, and sometimes cause some exception while unboxing some values like the examples below  ;

  • Attempting to unbox a null causes (NullReferenceException)

int? clientPhoneNumber= null;

ArrayList  phoneList= new ArrayList();

phoneList.add(clientPhoneNumber);

int unBoxedValue = (int)clientPhoneList[0];  // NullReferenceException

  • Attempting to unbox a reference to an incompatible value (InvalidCastException)

int myVar = 12;

object boxedVal = myVar ;

float unBoxed = (float)boxedVal ; //Specified cast is not valid.

 

I Hope you will enjoy this tips, Happy Programming Day.

 

 

 

validate Image content before upload in #asp.net using c#

tow years ago I face the situation where a hacker uploaded the malicious asp script to our web server by changing its extension to  .jpg  and our team had put validation on file extension, but unfortunately, the file extension was .jpg  (allowed extension), so the hacker uploaded that file    .

after this situation, we learn that just checking the file extension is not enough and not the sufficient solution so we decided to make it hard and

check the file header  and the file content, but be noticed that checking the whole file content will slow the process  ;

now in this example I will check the header and later in another example, we will check the whole content.

now  to the code :

     protected void btn_Upload(object sender, EventArgs e)
 
    {
        string message = string.Empty;
 
        Dictionary<string, byte[]> ArrImagHeader = new Dictionary<string, byte[]> {

                  {"JPG", new byte[] { 0xFF, 0xD8, 0xFF, 0xE0 }},
 
              {"JPEG", new byte[] { 0xFF, 0xD8, 0xFF, 0xE0 }},
 
              {"PNG", new byte[] { 0x89, 0x50, 0x4E, 0x47 }},
 
              {"TIF", new byte[] { 0x49, 0x49, 0x2A, 0x00 }},
 
              {"TIFF", new byte[] { 0x49, 0x49, 0x2A, 0x00 }},
 
              {"GIF", new byte[] { 0x47, 0x49, 0x46, 0x38 }},
 
              {"BMP", new byte[] { 0x42, 0x4D }},
 
              {"ICO", new byte[] { 0x00, 0x00, 0x01, 0x00 }},

                 };
 
        byte[] header;
 
        if (uploadctrl.HasFile)
 
        {
 
            // GET FILE EXTENSION
 
            string fileExt;
 
            fileExt = uploadctrl.FileName.Substring(uploadctrl.FileName.LastIndexOf('.') + 1).ToUpper();
 
            // CUSTOM VALIDATION GOES HERE BASED ON FILE EXTENSION IF ANY
 
            byte[] tmp = ArrImagHeader[fileExt];
 
            header = new byte[tmp.Length];
 
            // GET HEADER INFORMATION OF UPLOADED FILE
 
            uploadctrl.FileContent.Read(header, 0, header.Length);
 
            if (ComparyHeaders(tmp, header))
 
            {
 
                message = "Valid ." + fileExt + " file.";
 
            }
 
            else
 
            {
 
                message = "Invalid ." + fileExt + " file.";
 
                // INVALID HEADER INFORMATION
 
            }
 
        }
 
        else
 
        {
 
            message = "Please select image file.";
 
        }
 
    }
 
    private bool ComparyHeaders(byte[] target, byte[] source)
 
    {
 
        if (source.Length != target.Length)
 
            return false;
 
        for (int i = 0; i < target.Length; i++)
 
        {
 
            if (target[i] != source[i])
 
                return false;
 
        }
 
        return true;
 
    }
 
 
}

 

whey we need to use Common Table Expressions ( CTE )?

I will introduce some concepts very briefly before discussing on Common Table Expressions and why we need to use, and that because I assume that you have a good understanding of SQL already.

When we look at a basic SQL statement; the general structure  it will look like:

SELECT 
FROM 
WHERE GROUP BY by  < list of column >
HAVING 
ORDER BY  

 This list can contain zero or more tables, and when there are many tables, they can be joined using inner or outer join operators, and possibly also cross join

< list of column > This list can contain zero or more column

see below example :

select count(*) from  [Customers]                    -- 10 rows
select count(*) from  [Orders]                    -- 800 rows 
select count(*) from  [Employees], [Orders]  -- 8000 rows
 
In the third query, we combine the tables, without a join operator. 
The result will be all the combinations of Customers with Orders, 
which will produce  8000 rows.

:: to make this query clean and readable we can rewrite it like : 
select count(*) from Customers cross join [Orders]    -- 8000 

Of Course, This is All Just A Mental Picture, Imagine a join between 3 tables, each containing 1000 rows. The resulting virtual table would contain 1.000.000.000 rows

Inner Queries :

The table list can also contain the result of another SQL statement. as the following example of this:

select count(*) 
from (select * from  [Customers]) E   -- this is useless example just to explain the  idea 
select *
from  [OrderDetails] Ordet
inner join 
    (   select top 3 [order_id]
        from  [Orders]
        order by [unitprice]   desc
     ) order
on order.order_id = Ordet.orderId

Common Table Expressions

Specifies a temporary named result set, known as a common table expression 
(CTE).This is derived from a simple query and defined within the execution 
scope of a single SELECT, INSERT, UPDATE, or DELETE statement

Syntax

[ WITH  [ ,...n ] ]  

::=  
    expression_name [ ( column_name [ ,...n ] ) ]  
    AS  
    ( CTE_query_definition )  

Let’s first start with another clumsy example to make the idea of CTEs more clear:

;with My_cte as
(
    select top 3 [order_id]
    from  [OrderDetails]
    order by [unitprice]  desc
)
select * from My_cte 
What this does is to create a (virtual) table called My_cte, that can be used in the following query as a normal data source. The CTE is NOT a temporary table that you can use. It is part of the  statement that it belongs to, and it is local to that statement.  So later in the script, you can’t refer to the CTE table again. Given that  the CTE is part of this statement, the optimizer will use the whole  statement to make an efficient execution plan. SQL is a declarative  language: you define WHAT you want, and the optimizer decides HOW to do  this. The CTE will not necessarily be executed as first, it will depend on  the query plan. 
    A CTE must be followed by a single SELECT, INSERT, UPDATE, or DELETE statement that references some or all the CTE columns
    Multiple CTE query definitions can be defined in a nonrecursive CTE. The definitions must be combined by one of these set operators: UNION ALL, UNION, INTERSECT, or EXCEPT
    A CTE can reference itself and previously defined CTEs in the same WITH clause

the following clauses cannot be used in the CTE_query_definition:

    • ORDER BY (except when a TOP clause is specified)
    • INTO
    • OPTION clause with query hints
    • FOR BROWSE

Let’s make this example more useful:

;with My_cte as
(
    select top 3 [order_id]
    from  [OrderDetails]
    order by [unitprice]  desc
)
select order_id ,orderDate , itemID,amount ,unitprice 
from  [OrderDetails] orderdet
inner join My_cte  on orderdet.order_id= My_cte .order_id
we have split the query in 2 parts: we first calculate the 3 best orders, 
then we use the results of that to select their order details. Like this, 
we can show the intent of our query.

Conclusion : 
Common Table Expressions are one of the more advanced query mechanisms in T-SQL. They can make your queries more readable, or perform queries that would otherwise be impossible, such as outputting a hierarchical list. In this case, the real power is that a CTE can reference itself, making it possible to handle recursive structures

 

Reference

https://docs.microsoft.com/en-us/sql/t-sql/queries/with-common-table-expression-transact-sql?view=sql-server-2017

 

Implementing the Singleton Pattern in C#

Essentially, a singleton is a class which only allows a single instance of itself to be created and usually gives simple access to that instance. Most commonly, singletons don’t allow any parameters to be specified when creating the instance, for that This article deals only with the situation where no parameters are required.

There are various different ways of implementing the singleton pattern in C# and I will describe main four ways to implement Singleton Pattern.

First Version: Not Thread Save 

public seald class  singletomInstance {

private singletomInstance (){    }

private static  singletomInstance  _singleInstance =null  ;  

Public  staitc singletomInstance  Instance {

get{

               if(_singleInstance ==null)  {  _singleInstance  = new  singletomInstance() ;   }

                return _singleInstance ;

          }

  }

}

Second Version: simple thread Save  :

This implementation is thread-safe. The thread takes out a lock on a shared object and then checks whether or not the instance has been created before creating the instance.

public sealed class singletomInstance
{
private static  singletomInstance  _singleInstance =null  ;  
private static readonly object padlock = new object();

singletomInstance ()  {    }

public static  singletomInstance  Instance
{
get
{
                          lock (padlock)
{
                                    if (_singleInstance == null)
{
                                                  _singleInstance new Singleton();
}
                                           return _singleInstance ;
}
}
}
}

Third Version: Thread Save by double checking :

This implementation attempts to be thread-safe without the necessity of taking out a lock every time. Unfortunately, there are four downsides to the pattern:

  • It doesn’t work in Java. This may seem an odd thing to comment on, but it’s worth knowing if you ever need the singleton pattern in Java
  •  Without any memory barriers, it’s broken in the ECMA CLI specification too. It’s possible that under the .NET 2.0 memory model (which is stronger than the ECMA spec) it’s safe, but I’d rather not rely on that stronger semantics, especially if there’s any doubt as to the safety. Making the variable Instance volatile can make it work, as would explicit memory barrier calls, although in the latter case even experts can’t agree exactly which barriers are required. I tend to try to avoid situations where experts don’t agree what’s right and what’s wrong!
  • It’s easy to get wrong. The pattern needs to be pretty much exactly as Below- any significant changes are likely to impact either performance or correctness.
  • It still doesn’t perform as well as we need

public sealed class singletomInstance 
{
private static singletomInstance   _singleInstance null;
private static readonly object padlock = new object();

Singleton(){    }

public static singletomInstance  Instance
{
get
{
if (_singleInstance == null)
{
lock (padlock)
{
if (_singleInstance == null)
{
_singleInstance new Singleton();
}
}
}
return_singleInstance ;
}
}
}

 

fourth Version: thread save without lock 

public sealed class singletomInstance   
{
private static readonly  singletomInstance   _singleInstance new  singletomInstance   ();

// Explicit static constructor to tell C# compiler
// not to mark type as beforefieldinit
static singletomInstance   ()
{
}

private  singletomInstance   ()
{
}

public static  singletomInstance   Instance
{
get
{
return _singleInstance ;
}
}
}

As you can see, this is really is extremely simple – but why is it thread-safe and how lazy is it? Well, static constructors in C# are specified to execute only when an instance of the class is created or a static member is referenced, and to execute only once per AppDomain. Given that this check for the type being newly constructed needs to be executed whatever else happens, it will be faster than adding extra checking as in the previous examples

How To improve Entity Framework Performance

it is mandatory to know about the performance of it. These are right up to a certain point as LINQ comes with its own penalties. There are some tips and tricks that we should keep in mind while designing and query database using entity framework

Here is a list of some tips you would like to know.

1) Avoid putting all the DB Objects into One Single Entity Model

If we have many database objects that are not connected to one another or these(log tables, objects used by batch processes,etc.) are not used at all. Hence these objects are consuming space in the memory and cause performance degrades. So try to make separate entity models of related database objects

2)Disable change tracking for the entity if not needed

if you need to retrieve the data only for reading purpose then no need for object tracking. disable object tracking by using MergeOption as below

NorthwindDataContext context = new NorthwindDataContext() context.tblCities.MergeOption = MergeOption.NoTracking;

3)Use Compiled Query wherever needed

Make a query to compiled query if it is frequently used to fetch records from the database. This query is slow for the first time but after that, it boosts the performance significantly.

The following example compiles and then invokes a query that accepts a Decimal input parameter and returns a sequence of orders where the total due is greater than or equal to $200.00:

static readonly Func<AdventureWorksEntities, Decimal, IQueryable> s_compiledQuery2 = 
    CompiledQuery.Compile<AdventureWorksEntities, Decimal, IQueryable>(
            (ctx, total) => from order in ctx.SalesOrderHeaders
                            where order.TotalDue >= total
                            select order);

static void CompiledQuery2()
{            
    using (AdventureWorksEntities context = new AdventureWorksEntities())
    {
        Decimal totalDue = 200.00M;

        IQueryable orders = s_compiledQuery2.Invoke(context, totalDue);

        foreach (SalesOrderHeader order in orders)
        {
            Console.WriteLine("ID: {0}  Order date: {1} Total due: {2}",
                order.SalesOrderID,
                order.OrderDate,
                order.TotalDue);
        }
    }            
}

4)Retrieve the only required number of records

retrieve only required no of records to improve performance. This can achieve by using Take, While and Skip methods.

4)Avoid using Views

Views degrade the LINQ query performance costly. These are slow in performance and impact the performance greatly. So avoid using views in LINQ to Entities

Summary  :

this is some tips to help you improve the performance I hope you enjoy reading  this article and I hop it help you to make your query in a better way