Wednesday 16 February 2022

Dot net Services Web API

 Dot net Services  Web API vs. WCF vs. Webservice


1.Encasulation

2.oops concepts

3.abstraction vs encapsulation

4.static key worrd

5.Threads concepts

6.async vs await

7.solid priciples

8.Generics

9.Lamda

10. iqueryable vs ienumerable

11.Finalise and dispose

12. Repository pattern

13.CI CD

14. Event vs delegates

15. WEB API

16. wcf

17. func vs

18. C#, .Net 5, WebAPI, Azure, AWS 

and AMQ, Message Bus 


What is the difference between WCF and WEB API and WCF REST and Web Service?

The .NET framework has a number of technologies that allow us to create HTTP services such as Web Service, WCF, WCF Rest, and now WEB API. Following are the differences between these four:


Web Service:

1. The Web Service is based on SOAP and it returns the data in XML format.

2. It supports only the HTTP protocol.

3. Web Service is not open-source but it can be consumed by any client who understands XML.

4. It can only be hosted on IIS.

WCF:

1. WCF is also based on SOAP and it also returns the data in the form of XML.

2. Unlike Web service, WCF supports different types of protocols (transport protocol) such as TCP, Named Pipes, HTTP, HTTPS, and MSMQ.

3. The main problem with the WCF service it required lots of configuration which is a headache for a developer.

4. Like Web Service, WCF is also not open source but it can be consumed by any client who understands XML.

5. WCF can be host within the application or on IIS or using Windows service.

WCF Rest:

1. To use a WCF service as WCF Rest service we have to enable webHttpBindings.

2. WCF Rest supports the HTTP verbs such as GET and POST by using the [WebGet] and [WebInvoke] attributes respectively.

3. To use other HTTP verbs you have to do some configuration in the IIS so that it will accept the request of that particular verb on the .svc file

4. It supports different data formats such as XML, JSON, and Atom format.

WEB API:

1. The Web API Framework is a new framework that is basically used for developing HTTP-based services in an easy and simple way.

2. Unlike WCF Rest Service, it uses full HTTP features such as URIs, request/response headers, caching, versioning, and various data formats.

3. The ASP.NET Web API also supports most of the MVC features such as routing, controllers, actions, filter, model binders, IOC container, dependency injection, unit testing which makes it more simple and robust.

4. WEB API Services can be hosted on IIS or within the application

5. The Responses in Web API Services are formatted by MediaTypeFormatter into JSON, XML, or any custom format you want.

HTTP POST can support partial updates to a resource. But there is a separate PATCH method. This new HTTP method, PATCH, modifies an existing HTTP resource. The call to the Patch method is sufficient to partially update the corresponding properties of the Employee object in the list.

What are the differences between REST and SOAP?

It is one of the most frequently asked Web API Interview Questions in Web API Interviews.

The difference between REST and SOAP is given below:

1. SOAP stands for Simple Object Access Protocol whereas REST stands for Representational State Transfer.

2. The SOAP is an XML-based protocol whereas REST is not a protocol rather it is an architectural pattern i.e. resource-based architecture.                         

3. SOAP has specifications for both stateless and state-full implementation whereas REST is completely stateless.

4. SOAP enforces message format as XML whereas REST does not enforce message format as XML or JSON.

5. The SOAP message consists of an envelope that includes SOAP headers and body to store the actual information we want to send whereas REST uses the HTTP build-in headers (with a variety of media-types) to store the information and uses the HTTP Methods such as GET, POST, PUT, PATCH, and DELETE  to perform CRUD operations.

6. SOAP uses interfaces and named operations (i.e. Service Contract and Operation Contract) to expose the service whereas to expose resources (service) REST uses URI and methods like (GET, PUT, POST, PATCH, and DELETE).

7. SOAP Performance is slow as compared to REST.


1) What is Web API?

WebAPI is a framework which helps you to build/develop HTTP services.

2) Why is Web API required? Is it possible to use RESTful services using WCF?

Yes, we can still develop RESTful services with WCF. However, there are two main reasons that prompt users to use Web API instead of RESTful services.

Web API increases TDD (Test Data Driven) approach in the development of RESTful services.

If we want to develop RESTful services in WCF, you surely need a lot of config settings, URI templates, contracts & endpoints for developing RESTful services using web API.

3) Why select Web API?

It is used to create simple, non-SOAP-based HTTP Services

It is also an easy method for creation with Web API. With WCF REST Services

It is based on HTTP and easy to define, expose and consume in a REST-ful way.

It is lightweight architecture and ideal for devices that have limited bandwidth like smartphones.

4) Is it right that ASP.NET Web API has replaced WCF?

It’s a not at all true that ASP.NET Web API has replaced WCF. In fact, it is another way of building non-SOAP based services, i.e., plain XML or JSON string.

5) What are the advantages of Web API?

Advantages of Web API are:

OData

Filters

Content Negotiation

Self-Hosting

Routing

Model Bindings

6) What are main return types supported in Web API?

A Web API controller action can return following values:

Void – It will return empty content

HttpResponseMessage – It will convert the response to an HTTP message.

IHttpActionResult – internally calls ExecuteAsync to create an HttpResponseMessage

Other types – You can write the serialized return value into the response body

7) Web API supports which protocol?

Web App supports HTTP protocol.

8) Which .NET framework supports Web API?

NET 4.0 and above version supports web API.

9) Web API uses which of the following open-source library for JSON serialization?

Web API uses Json.NET library for JSON serialization.

10) By default, Web API sends HTTP response with which of the following status code for all uncaught exception?

500 – Internal Server Error

11) What is the biggest disadvantage of “Other Return Types” in Web API?

The biggest disadvantage of this approach is that you cannot directly return an error code like 404 error.


If in iis you need to attach to the corresponding w3p.exe process.

If in a stand alone app or windows service, attach to the name of your exe.

In Visual Studio, on the debugger menu, there is an "attach to process". Open the relevant code, set a breakpoint, and call the service causing that code path to execute.

Outside of debugging, using .net tracing with switchable levels is a good way to get insight to what's going on. I typically setup sys internals debugview to color highlight errors and warnings and constantly have it running while running code or tests. Colored lines out of my peripheral vision while working finds issues.



Abstraction

Abstraction is one of the major principles of Object-oriented programming. Abstraction allows the programmer to display only the necessary details to the world while hiding the others. Abstraction is achieved in C# by using the Abstract class and interface.

A class can be declared as an abstract class by using the “Abstract” keyword. The Abstract class in C# is always the base class in the hierarchy. What makes them different from the other class is that they cannot be instantiated. A C# abstract class needs to be inherited.

Example:

class Program

    {

           static void Main(string[] args)

           {

                  Hyundai hyn = new Hyundai();

                  String descp = hyn.Describe();

                  Console.WriteLine(descp);

                  Console.ReadKey();

            }

      }

abstract class Car

 {

          public void Describe()

          {

                return "Description of the car";            

           }

   }

 

class Hyundai : Car

{

}

The output of the following is:

Description of the car

If you compare it with our previous examples during inheritance or dynamic polymorphism, then you will find the similarities. The most recognizable difference is the use of abstract keyword before the class Car. In case you want to override this or provide your own implementation similar to what we did in dynamic polymorphism. Then you can achieve that by the following.

class Program

{

           static void Main(string[] args)

           {

                Hyundai hyn = new Hyundai();

                String descp = hyn.Describe();

                Console.WriteLine(descp);

                Console.ReadKey();

            }

 }

abstract class Car

{

              public virtual string Describe()

              {

                      return "Description of the car";

               }

   }

class Hyundai : Car

{

         public override string Describe()

         {

                 return "Description of the car is now Hyundai";

          }

 }


Abstraction

Abstraction is "To represent the essential feature without representing the background details."


Abstraction lets you focus on what the object does instead of how it does it.


Abstraction provides you a generalized view of your classes or objects by providing relevant information.


Abstraction is the process of hiding the working style of an object, and showing the information of an object in an understandable manner.


Real-world Example of Abstraction

Suppose you have an object Mobile Phone.


Suppose you have 3 mobile phones as in the following:


Nokia 1400 (Features: Calling, SMS)

Nokia 2700 (Features: Calling, SMS, FM Radio, MP3, Camera)

Black Berry (Features:Calling, SMS, FM Radio, MP3, Camera, Video Recording, Reading E-mails)


Abstract information (necessary and common information) for the object "Mobile Phone" is that it makes a call to any number and can send SMS.


So that, for a mobile phone object you will have the abstract class as in the following,  

abstract class MobilePhone {

    public void Calling();

    public void SendSMS();

}

public class Nokia1400: MobilePhone {}

public class Nokia2700: MobilePhone {

    public void FMRadio();

    public void MP3();

    public void Camera();

}

public class BlackBerry: MobilePhone {

    public void FMRadio();

    public void MP3();

    public void Camera();

    public void Recording();

    public void ReadAndSendEmails();

}

C#

Copy

Abstraction means putting all the variables and methods in a class that are necessary.

For example: Abstract class and abstract method.

Abstraction is a common thing.

Example

If somebody in your college tells you to fill in an application form, you will provide your details, like name, address, date of birth, which semester, percentage you have etcetera.

If some doctor gives you an application to fill in the details, you will provide the details, like name, address, date of birth, blood group, height and weight.

See in the preceding example what is in common?

Age, name and address, so you can create a class that consists of the common data. That is called an abstract class.

That class is not complete and it can be inherited by other classes.

Encapsulation

Wrapping up a data member and a method together into a single unit (in other words class) is called Encapsulation.

Encapsulation is like enclosing in a capsule. That is enclosing the related operations and data related to an object into that object.


Encapsulation is like your bag in which you can keep your pen, book etcetera. It means this is the property of encapsulating members and functions.

class Bag {

    book;

    pen;

    ReadBook();

}

C#

Copy

Encapsulation means hiding the internal details of an object, in other words how an object does something.


Encapsulation prevents clients from seeing its inside view, where the behaviour of the abstraction is implemented.


Encapsulation is a technique used to protect the information in an object from another object.


Hide the data for security such as making the variables private, and expose the property to access the private data that will be public. 

So, when you access the property you can validate the data and set it.


Example 1

class Demo {

    private int _mark;

    public int Mark {

        get {

            return _mark;

        }

        set {

            if (_mark > 0) _mark = value;

            else _mark = 0;

        }

    }

}

C#

Copy

Real-world Example of Encapsulation

Let's use as an example Mobile Phones and Mobile Phone Manufacturers.

Suppose you are a Mobile Phone Manufacturer and you have designed and developed a Mobile Phone design (a class). Now by using machinery you are manufacturing Mobile Phones (objects) for selling, when you sell your Mobile Phone the user only learns how to use the Mobile Phone but not how the Mobile Phone works.


This means that you are creating the class with functions and by with objects (capsules) of which you are making available the functionality of your class by that object and without the interference in the original class.

Encapsulation

Encapsulation is an object-oriented programming concept that allows programmers to wrap data and code snippets inside an enclosure. By using the encapsulation program, you can hide the members of one class from another class. It’s like encircling a logical item within a package. It allows only relevant information available and visible outside and that too only to specific members.

Polymorphism means “Many Forms”. In Polymorphism, poly means “Many” and morph means “Forms.” Polymorphism is one of the main pillars in Object Oriented Programming. It allows you to create multiple methods with the same name but different signatures in the same class. The same name methods can also be in derived classes.

 

There are two types of Polymorphism,

1. Method Overloading (compile time)

2. Method Overriding (run time)

Method Overloading

Method Overloading is a type of polymorphism. It has several names like “Compile Time Polymorphism” or “Static Polymorphism” and sometimes it is called “Early Binding”.

 

Method Overloading means creating multiple methods in a class with same names but different signatures (Parameters). It permits a class, struct, or interface to declare multiple methods with the same name with unique signatures.

 

Compiler automatically calls required method to check number of parameters and their type which are passed into that method.

1. using System;  

2. namespace DemoCsharp  

3. {  

4.     class Program  

5.     {  

6.         public int Add(int num1, int num2)  

7.         {  

8.             return (num1 + num2);  

9.         }  

10.         public int Add(int num1, int num2, int num3)  

11.         {  

12.             return (num1 + num2 + num3);  

13.         }  

14.         public float Add(float num1, float num2)  

15.         {  

16.             return (num1 + num2);  

17.         }  

18.         public string Add(string value1, string value2)  

19.         {  

20.             return (value1 + " " + value2);  

21.         }  

22.         static void Main(string[] args)  

23.         {  

24.             Program objProgram = new Program();  

25.             Console.WriteLine("Add with two int parameter :" + objProgram.Add(3, 2));  

26.             Console.WriteLine("Add with three int parameter :" + objProgram.Add(3, 2, 8));  

27.             Console.WriteLine("Add with two float parameter :" + objProgram.Add(3 f, 22 f));  

28.             Console.WriteLine("Add with two string parameter :" + objProgram.Add("hello", "world"));  

29.             Console.ReadLine();  

30.         }  

31.     }  

32. }  



Method Overriding

 

Method Overriding is a type of polymorphism. It has several names like “Run Time Polymorphism” or “Dynamic Polymorphism” and sometime it is called “Late Binding”. 

 

Method Overriding means having two methods with same name and same signatures [parameters], one should be in the base class and other method should be in a derived class [child class]. You can override the functionality of a base class method to create a same name method with same signature in a derived class. You can achieve method overriding using inheritance. Virtual and Override keywords are used to achieve method overriding.



1. using System;  

2. namespace DemoCsharp  

3. {  

4.     class BaseClass  

5.     {  

6.         public virtual int Add(int num1, int num2)  

7.         {  

8.             return (num1 + num2);  

9.         }  

10.     }  

11.     class ChildClass: BaseClass  

12.     {  

13.         public override int Add(int num1, int num2)  

14.         {  

15.             if (num1 <= 0 || num2 <= 0)  

16.             {  

17.                 Console.WriteLine("Values could not be less than zero or equals to zero");  

18.                 Console.WriteLine("Enter First value : ");  

19.                 num1 = Convert.ToInt32(Console.ReadLine());  

20.                 Console.WriteLine("Enter First value : ");  

21.                 num2 = Convert.ToInt32(Console.ReadLine());  

22.             }  

23.             return (num1 + num2);  

24.         }  

25.     }  

26.     class Program  

27.     {  

28.         static void Main(string[] args)  

29.         {  

30.             BaseClass baseClassObj;  

31.             baseClassObj = new BaseClass();  

32.             Console.WriteLine("Base class method Add :" + baseClassObj.Add(-3, 8));  

33.             baseClassObj = new ChildClass();  

34.             Console.WriteLine("Child class method Add :" + baseClassObj.Add(-2, 2));  

35.             Console.ReadLine();  

36.         }  

37.     }  

38. }  

What Inheritance is

 

Acquiring (taking) the properties of one class into another class is called inheritance. Inheritance provides reusability by allowing us to extend an existing class.

 

The reason behind OOP programming is to promote the reusability of code and to reduce complexity in code and it is possible by using inheritance.

 

The following are the types of inheritance in C#.

 

  

 

The inheritance concept is based on a base class and derived class. Let us see the definition of a base and derived class.

Base class - is the class from which features are to be inherited into another class.

Derived class - it is the class in which the base class features are inherited.

1.Single inheritance 

 

It is the type of inheritance in which there is one base class and one derived class.

 

  

 

For example,

1. public class Accountcreditinfo //base class    

2. {  

3.     public string Credit()  

4.     {  

5.         return "balance is credited";  

6.     }  

7. }  

8. public class debitinfo : Accountcreditinfo //derived class    

9. {  

10.     public string debit()  

11.     {  

12.         Credit();                       ////derived class method    

13.         return "balance is debited";  

14.     }  

15. }  

In the preceding sample program Accountcreditinfo is the base class and debitinfo is the derived class.

 

2.Hierarchical inheritance

 

This is the type of inheritance in which there are multiple classes derived from one base class. This type of inheritance is used when there is a requirement of one class feature that is needed in multiple classes.

 

  

 

For example,

1. class A  //base class    

2. {  

3.     public string msg()  

4.     {  

5.         return "this is A class Method";  

6.     }  

7. }  

8. class B : A  

9. {  

10.     public string info()  

11.     {  

12.         msg();  

13.         return "this is B class Method";  

14.     }  

15.     class C : A  

16.     {  

17.         public string getinfo()  

18.         {  

19.             msg();  

20.             return "this is B class Method";  

21.         }  

22.     }  

23. }    

In the preceding program one base class is derived in many classes hence it is a called a Hierarchical Inheritance.

 

3.Multilevel inheritance

 

When one class is derived from another derived class then this type of inheritance is called multilevel inheritance.

 

  

 

For example,

1. public class Person  

2. {  

3.     public string persondet()  

4.     {  

5.         return "this is the person class";  

6.     }  

7. }  

8. public class Bird : Person  

9. {  

10.     public string birddet()  

11.     {  

12.         persondet();  

13.         return "this is the birddet Class";  

14.     }  

15. }  

16. public class Animal : Bird  

17. {  

18.     public string animaldet()  

19.     {  

20.         persondet();  

21.         birddet();  

22.         return "this is the Animal Class";  

23.     }  

24. }   

In the preceding program, each class is derived from one class that is derived from another class hence this type of inheritance is called Multilevel Inheritance.

 

4.Multiple inheritance using Interfaces

 

C# does not support multiple inheritances of classes. To overcome this problem we can use interfaces, we will see more about interfaces in my next article in detail.

  

For example,

1. public interface IA //ineterface  1    

2. {  

3.     string setImgs(string a);  

4. }  

5. public interface IB  //Interface 2    

6. {  

7.     int getAmount(int Amt);  

8. }  

9. public class ICar : IA, IB //implementatin    

10. {  

11.     public int getAmount(int Amt)  

12.     {  

13.         return 100;  

14.     }  

15.     public string setImgs(string a)  

16.     {  

17.         return "this is the car";  

18.     }  

19. }    

In the preceding program the ICar class inherits the features of the two interfaces hence this type of inheritance is called Multiple Inheritance.

 

The following are some key points about inheritance:

1. C# does not support multiple inheritances of classes, the same thing can be done using interfaces.

2. Private members are not accessed in a derived class when one class is derived from another.

Static Variable

A static variable is declared with the help of static keyword. When a variable is declared as static, then a single copy of the variable is created and shared among all objects at the class level. Static variables are accessed with the name of the class, they do not require any object for access.


// C# program to illustrate the

// concept of static variable

using System;

 

class Vehicle {

 

    // Creating static variable

    // Using static keyword

    public static string Model_color = "Black";

}

// Driver Class

public class GFG {

 

    // Main Method

    static public void Main()

    {

 

        // Accessing the static variable

        // using its class name

        Console.WriteLine("Color of XY model is  : {0} ",

                                    Vehicle.Model_color);

    }



C# async await 


We can run all the methods parallelly by using simple thread programming, but it will block UI and wait to complete all the tasks.

class Program

{

    static void Main(string[] args)

    {

        Method1();

        Method2();

        Console.ReadKey();

    }


    public static async Task Method1()

    {

        await Task.Run(() =>

        {

            for (int i = 0; i < 100; i++)

            {

                Console.WriteLine(" Method 1");

                // Do something

                Task.Delay(100).Wait();

            }

        });

    }



    public static void Method2()

    {

        for (int i = 0; i < 25; i++)

        {

            Console.WriteLine(" Method 2");

            // Do something

           Task.Delay(100).Wait();

        }

    }

}


In the code given above, Method1 and Method2 are not dependent on each other and we are calling from the Main method.

Here, we can clearly see Method1, and Method2 are not waiting for each other.

Output

 



Encapsulation:



Encapsulation is a process of binding the data members and member functions into a single unit.



Example for encapsulation is class. A class can contain data structures and methods.

Consider the following class



public class Aperture

{

public Aperture ()

{

}

protected double height;

protected double width;

protected double thickness;

public double get volume()

{

Double volume=height * width * thickness;

if (volume<0)

return 0;

return volume;

}

}


In this example we encapsulate some data such as height, width, thickness and method Get Volume. Other methods or objects can interact with this object through methods that have public access modifier.


To know more about Encapsulation check this Encapsulation in C# with Examples.



Abstraction:



Abstraction is a process of hiding the implementation details and displaying the essential features.



Example1: A Laptop consists of many things such as processor, motherboard, RAM, keyboard, LCD screen, wireless antenna, web camera, usb ports, battery, speakers etc. To use it, you don't need to know how internally LCD screens, keyboard, web camera, battery, wireless antenna, speaker’s works.  You just need to know how to operate the laptop by switching it on. Think about if you would have to call to the engineer who knows all internal details of the laptop before operating it. This would have highly expensive as well as not easy to use everywhere by everyone.



Multithreading Implementation

 

Obtaining Current Thread Information

 

To illustrate the basic use of the Thread type, suppose you have a console application in which the CurrentThread property retrieves a Thread object that represents the currently executing thread.

1. using System;  

2. using System.Threading;  

3. namespace threading  

4. {  

5.     class Program  

6.     {  

7.         static void Main(string[] args)  

8.         {  

9.             Console.WriteLine("**********Current Thread Informations***************\n");  

10.             Thread t = Thread.CurrentThread;  

11.             t.Name = "Primary_Thread";  

12.             Console.WriteLine("Thread Name: {0}", t.Name);  

13.             Console.WriteLine("Thread Status: {0}", t.IsAlive);  

14.             Console.WriteLine("Priority: {0}", t.Priority);  

15.             Console.WriteLine("Context ID: {0}", Thread.CurrentContext.ContextID);  

16.             Console.WriteLine("Current application domain: {0}",Thread.GetDomain().FriendlyName);  

17.             Console.ReadKey();   

18.         }  

19.     }  

20. }  


After compiling this application, the output will be as in the following;

 

 


Simple Thread Creation

 

The following example explains the Thread class implementation in which the constructor of Thread class accepts a delegate parameter. After the Thread class object is created, you can start the thread with the Start() method as in the following;

1. using System;  

2. using System.Threading;  

3. namespace threading  

4. {  

5.     class Program  

6.     {  

7.         static void Main(string[] args)  

8.         {  

9.             Thread t = new Thread(myFun);  

10.              t.Start();  

11.             Console.WriteLine("Main thread Running");  

12.             Console.ReadKey();   

13.         }  

14.         static void myFun()  

15.         {  

16.             Console.WriteLine("Running other Thread");   

17.         }  

18.     }  

19. }  

After running the application, you will get the following output of the two threads:

 

 

 


Background Thread

 

The process of the application keeps running as long as at least one foreground thread is running. If more than one foreground thread is running and the Main() method ends, the process of the application keeps active until all foreground threads finish their work, and by foreground thread termination all background threads will terminate immediately.

 

When you create a thread with the Thread class, you can define it as being either a foreground or background thread by setting the property IsBackground. The Main() method sets this property of the thread "t" to false. After setting the new thread, the main thread just writes to the console an end message. The new thread writes a start and an end message, and in between it sleeps for 2 seconds.

1. using System;  

2. using System.Threading;  

3. namespace threading  

4. {  

5.     class Program  

6.     {  

7.         static void Main(string[] args)  

8.         {  

9.             Thread t = new Thread(myFun);  

10.             t.Name = "Thread1";  

11.             t.IsBackground = false;   

12.             t.Start();  

13.             Console.WriteLine("Main thread Running");  

14.             Console.ReadKey();   

15.         }  

16.         static void myFun()  

17.         {  

18.             Console.WriteLine("Thread {0} started", Thread.CurrentThread.Name);  

19.             Thread.Sleep(2000);   

20.             Console.WriteLine("Thread {0} completed", Thread.CurrentThread.Name);   

21.         }  

22.     }  

23. }  

When you compile this application, you will still see the completion message written to the console because the new thread is a foreground thread. Here, the output is as in the following;

 

If you change the IsBackground property to true then the result shown at the console will be as in the following;

 


Race Condition

 

A race condition occurs if two or more threads access the same object and access to the shared state is not synchronized. To illustrate the problem of a Race condition, let's build a console application. This application uses the Test class to print 10 numbers by pausing the current thread for a random number of times. 

1. using System;  

2. using System.Threading;  

3. namespace threading  

4. {  

5.     public class Test  

6.     {  

7.         public void Calculate()  

8.         {    

9.             for (int i = 0; i < 10; i++)  

10.             {  

11.                 Thread.Sleep(new Random().Next(5));  

12.                 Console.Write(" {0},", i);  

13.             }  

14.             Console.WriteLine();  

15.         }  

16.     }  

17.     class Program  

18.     {  

19.         static void Main(string[] args)  

20.         {  

21.             Test t = new Test();  

22.             Thread[] tr = new Thread[5];  

23.             for (int i = 0; i < 5; i++)  

24.             {  

25.                 tr[i] = new Thread(new ThreadStart(t.Calculate));  

26.                 tr[i].Name = String.Format("Working Thread: {0}", i);  

27.             }  

28.             //Start each thread  

29.             foreach (Thread x in tr)  

30.             {  

31.                 x.Start();  

32.             }  

33.             Console.ReadKey();  

34.         }  

35.     }  

36. }  


After compiling this program, the primary thread within this application domain begins by producing five secondary threads. And each working thread is told to call the Calculate() method on the same Test class instance. Hence, all five threads start to access the Calculate() method simultaneously and since we have not taken any precaution to lock down this object's shared resources; this will lead towards the Race Condition and the application produces unpredictable output as in the following;

 


Deadlocks

 

Having too much locking in an application can get your application into trouble. In a deadlock, at least two threads wait for each other to release a lock. As both threads wait for each other, a deadlock situation occurs and threads wait endlessly and the program stops responding.

 

Here, both the methods changed the state of objects obj1 and obj2 by locking them. The method DeadLock1() first locks obj1 and next for obj2 similarly method DeadLock2() first locks obj2 and then obj1. So lock for obj1 is released, next a thread switch occurs and the second method starts and gets the lock for obj2. The second thread now waits for the lock of obj1. Both of threads now wait and don't release each other. This is typically a deadlock situation.


1. using System;  

2. using System.Threading;  

3. namespace threading  

4. {  

5.     class Program  

6.     {  

7.         static object obj1 = new object();  

8.         static object obj2 = new object();  

9.         public static void DeadLock1()  

10.         {  

11.             lock (obj1)  

12.             {  

13.                 Console.WriteLine("Thread 1 got locked");  

14.                 Thread.Sleep(500);  

15.                 lock (obj2)  

16.                 {  

17.                     Console.WriteLine("Thread 2 got locked");  

18.                 }  

19.             }  

20.         }  

21.         public static void DeadLock2()  

22.         {  

23.             lock (obj2)  

24.             {  

25.                 Console.WriteLine("Thread 2 got locked");  

26.                 Thread.Sleep(500);  

27.                 lock (obj1)  

28.                 {  

29.                     Console.WriteLine("Thread 1 got locked");  

30.                 }  

31.             }  

32.         }  

33.         static void Main(string[] args)  

34.         {  

35.             Thread t1 = new Thread(new ThreadStart(DeadLock1));  

36.             Thread t2 = new Thread(new ThreadStart(DeadLock2));  

37.             t1.Start();  

38.             t2.Start();  

39.             Console.ReadKey();  

40.         }  

41.     }  

42. }  


Synchronization

 

Problems that can occur with multiple threads such as Race condition and deadlocks can be avoided by Synchronization. It is always suggested to avoid concurrency issues by not sharing data between threads. Of course, this is not always possible. If data sharing is unavoidable then you must use synchronization so that only one thread at a time accesses and changes shared states.

 

This section discusses various synchronization techniques.

Locks

 

We can synchronize access to shared resources using the lock keyword. By doing so, incoming threads cannot interrupt the current thread, preventing it from finishing its work. The lock keyword requires an object reference.

 

By taking the previous Race Condition problem, we can refine this program by implementing a lock on crucial statements to make it foolproof from a race conditions as in the following;

1. public class Test  

2. {  

3.     public object tLock = new object();  

4.     public void Calculate()  

5.     {  

6.         lock (tLock)  

7.         {  

8.             Console.Write(" {0} is Executing",Thread.CurrentThread.Name);  

9.             for (int i = 0; i < 10; i++)  

10.             {  

11.                 Thread.Sleep(new Random().Next(5));  

12.                 Console.Write(" {0},", i);  

13.             }  

14.             Console.WriteLine();  

15.         }  

16.     }  

17. }  


After compiling this program, this time it produced the desired result as in the following. Here, each thread has sufficed opportunity to finish its tasks.

 

Monitor

 

The lock statement is resolved by the compiler to the use of the Monitor class. The Monitor class is almost similar to a lock but its advantage is better control than the lock statement. You are able to instruct the lock's enter and exit explicitly, as shown in the code below. 

1. object tLock = new object();  

2. public void Calculate()  

3. {  

4.     Monitor.Enter(tLock);  

5.     try  

6.     {  

7.       for (int i = 0; i < 10; i++)  

8.       {  

9.         Thread.Sleep(new Random().Next(5));  

10.         Console.Write(" {0},", i);  

11.       }  

12.     }  

13.     catch{}  

14.     finally  

15.     {  

16.       Monitor.Exit(tLock);  

17.     }  

18.     Console.WriteLine();  

19. }  

In fact, if you observe the IL code of the any application that uses the lock statement, you will find the Monitor class reference there as in the following;

 

  

 

Using [Synchronization] Attribute

 

The [Synchronization] attribute is a member of System.Runtime.Remoting.Context namespace. This class level attribute effectively locks down all instance of the object for thread safety.

1. using System.Threading;  

2.   

3. using System.Runtime.Remoting.Contexts;   

4. [Synchronization]  

5. public class Test:ContextBoundObject  

6. {  

7.     public void Calculate()  

8.     {  

9.         for (int i = 0; i < 10; i++)  

10.         {  

11.             Thread.Sleep(new Random().Next(5));  

12.             Console.Write(" {0},", i);  

13.         }  

14.         Console.WriteLine();  

15.     }  

16. }  

Mutex

 

Mutex stands for Mutual Exclusion that offers synchronization across multiple threads. The Mutex calss is derived from WaitHandle, you can do a WaitOne() to acquire the mutex lock and be the owner of the mutex that time. The mutex is released by invoking the ReleaseMutex() method as in the following;

1. using System;  

2. using System.Threading;  

3. namespace threading  

4. {  

5.     class Program  

6.     {  

7.         private static Mutex mutex = new Mutex();  

8.         static void Main(string[] args)  

9.         {  

10.             for (int i = 0; i < 4; i++)  

11.             {  

12.                 Thread t = new Thread(new ThreadStart(MutexDemo));  

13.                 t.Name = string.Format("Thread {0} :", i+1);  

14.                 t.Start();  

15.             }  

16.             Console.ReadKey();  

17.         }  

18.         static void MutexDemo()  

19.         {  

20.             try  

21.             {  

22.                 mutex.WaitOne();   // Wait until it is safe to enter.  

23.                 Console.WriteLine("{0} has entered in the Domain", Thread.CurrentThread.Name);  

24.                 Thread.Sleep(1000);    // Wait until it is safe to enter.  

25.                 Console.WriteLine("{0} is leaving the Domain\r\n", Thread.CurrentThread.Name);  

26.             }  

27.             finally  

28.             {  

29.                 mutex.ReleaseMutex();  

30.             }  

31.         }  

32.     }  

33. }  

Once you have successfully compiled this program, it shows when each new thread first enters into its application domain. Once it has finished its tasks then it is released and the second thread starts and so on.

 

  

 

Semaphore

 

A semaphore is very similar to a Mutex but a semaphore can be used by multiple threads at once while a Mutex can't. With a Semaphore, you can define a count of how many threads are allowed to access the resources shielded by a semaphore simultaneously.

 

Here in the following example, 5 threads are created and 2 semaphores. In the constructor of the semaphore class, you can define the number of locks that can be acquired with a semaphore. 

1. using System;  

2. using System.Threading;  

3. namespace threading  

4. {  

5.     class Program  

6.     {  

7.         static Semaphore obj = new Semaphore(2, 4);  

8.         static void Main(string[] args)  

9.         {  

10.             for (int i = 1; i <= 5; i++)  

11.             {  

12.                 new Thread(SempStart).Start(i);  

13.             }  

14.             Console.ReadKey();  

15.         }  

16.         static void SempStart(object id)  

17.         {  

18.             Console.WriteLine(id + "-->>Wants to Get Enter");  

19.             try  

20.             {  

21.                 obj.WaitOne();  

22.                 Console.WriteLine(" Success: " + id + " is in!");     

23.                 Thread.Sleep(2000);  

24.                 Console.WriteLine(id + "<<-- is Evacuating");  

25.             }  

26.             finally  

27.             {  

28.                 obj.Release();  

29.             }  

30.         }  

31.     }  

32. }  

While we run this application, 2 semaphores are immediately created and the others wait because we have created 5 threads. So 3 are in the waiting state. The movement, any one of thread released the rest of created one by one as following.

 

 




SOLID principles

 

SOLID principles are the design principles that enable us to manage most of the software design problems. Robert C. Martin compiled these principles in the 1990s. These principles provide us with ways to move from tightly coupled code and little encapsulation to the desired results of loosely coupled and encapsulated real needs of a business properly. SOLID is an acronym of the following.

S: Single Responsibility Principle (SRP)

O: Open closed Principle (OCP)

L: Liskov substitution Principle (LSP)

I: Interface Segregation Principle (ISP)

D: Dependency Inversion Principle (DIP)

The Single Responsibility Principle gives us a good way of identifying classes at the design phase of an application and it makes you think of all the ways a class can change. A good separation of responsibilities is done only when we have the full picture of how the application should work. Let us check this with an example.

1. public class UserService  

2. {  

3.    public void Register(string email, string password)  

4.    {  

5.       if (!ValidateEmail(email))  

6.          throw new ValidationException("Email is not an email");  

7.          var user = new User(email, password);  

8.   

9.          SendEmail(new MailMessage("mysite@nowhere.com", email) { Subject="HEllo foo" });  

10.    }

11.    public virtual bool ValidateEmail(string email)  

12.    {  

13.      return email.Contains("@");  

14.    }  

15.    public bool SendEmail(MailMessage message)  

16.    {  

17.      _smtpClient.Send(message);  

18.    }  

19. }   



Open/Closed Principle

 

The Open/closed Principle says "A software module/class is open for extension and closed for modification".


 Functionality can be added by creating new classes that implement the interfaces. So let's refract our code using an interface.

1. public abstract class Shape  

2. {  

3.    public abstract double Area();  

4. }  

Inheriting from Shape, the Rectangle and Circle classes now look like this:

1. public class Rectangle: Shape  

2. {  

3.    public double Height {get;set;}  

4.    public double Width {get;set;}  

5.    public override double Area()  

6.    {  

7.       return Height * Width;  

8.    }  

9. }  

10. public class Circle: Shape  

11. {  

12.    public double Radius {get;set;}  

13.    public override double Area()  

14.    {  

15.       return Radius * Radus * Math.PI;  

16.    }  

17. }  



3.Liskov Substitution Principle


The Liskov Substitution Principle (LSP) states that "you should be able to use any derived class instead of a parent class and have it behave in the same manner without modification". It ensures that a derived class does not affect the behavior of the parent class, in other words,, that a derived class must be substitutable for its base class.


1. public class SqlFile  

2. {  

3.    public string LoadText()  

4.    {  

5.    /* Code to read text from sql file */  

6.    }  

7.    public void SaveText()  

8.    {  

9.       /* Code to save text into sql file */  

10.    }  

11. }  

12. public class ReadOnlySqlFile: SqlFile  

13. {  

14.    public string FilePath {get;set;}  

15.    public string FileText {get;set;}  

16.    public string LoadText()  

17.    {  

18.       /* Code to read text from sql file */  

19.    }  

20.    public void SaveText()  

21.    {  

22.       /* Throw an exception when app flow tries to do save. */  

23.       throw new IOException("Can't Save");  

24.    }  

25. }  

To avoid an exception we need to modify "SqlFileManager" by adding one condition to the loop.


4.Interface Segregation Principle (ISP)

 

The Interface Segregation Principle states "that clients should not be forced to implement interfaces they don't use. Instead of one fat interface, many small interfaces are preferred based on groups of methods, each one serving one submodule.".

 we need to create an interface and a TeamLead class to implement it. 

1. public Interface ILead  

2. {  

3.    void CreateSubTask();  

4.    void AssginTask();  

5.    void WorkOnTask();  

6. }  

7. public class TeamLead : ILead  

8. {  

9.    public void AssignTask()  

10.    {  

11.       //Code to assign a task.  

12.    }  

13.    public void CreateSubTask()  

14.    {  

15.       //Code to create a sub task  

16.    }  

17.    public void WorkOnTask()  

18.    {  

19.       //Code to implement perform assigned task.  

20.    }  

21. }  


Dependency Inversion Principle

 

A high-level module/class that has a dependency on low-level modules/classes or some other class and knows a lot about the other classes it interacts with is said to be tightly coupled. When a class knows explicitly about the design and implementation of another class, it raises the risk that changes to one class will break the other class. So we must keep these high-level and low-level modules/classes loosely coupled as much as we can. To do that, we need to make both of them dependent on abstractions instead of knowing each other. Let's start with an example.

 

Suppose we need to work on an error logging module that logs exception stack traces into a file. Simple, isn't it? The following are the classes that provide the functionality to log a stack trace into a file. 

Now, we move to the low-level class's initiation from the ExcetpionLogger class to the DataExporter class to make ExceptionLogger loosely coupled with the low-level classes FileLogger and EventLogger. And by doing that we are giving provision to DataExporter class to decide what kind of Logger should be called based on the exception that occurs.

1. public class ExceptionLogger  

2. {  

3.    private ILogger _logger;  

4.    public ExceptionLogger(ILogger aLogger)  

5.    {  

6.       this._logger = aLogger;  

7.    }  

8.    public void LogException(Exception aException)  

9.    {  

10.       string strMessage = GetUserReadableMessage(aException);  

11.       this._logger.LogMessage(strMessage);  

12.    }  

13.    private string GetUserReadableMessage(Exception aException)  

14.    {  

15.       string strMessage = string.Empty;  

16.       //code to convert Exception's stack trace and message to user readable format.  

17.       ....  

18.       ....  

19.       return strMessage;  

20.    }  

21. }  

22. public class DataExporter  

23. {  

24.    public void ExportDataFromFile()  

25.    {  

26.       ExceptionLogger _exceptionLogger;  

27.       try {  

28.          //code to export data from files to database.  

29.       }  

30.       catch(IOException ex)  

31.       {  

32.          _exceptionLogger = new ExceptionLogger(new DbLogger());  

33.          _exceptionLogger.LogException(ex);  

34.       }  

35.       catch(Exception ex)  

36.       {  

37.          _exceptionLogger = new ExceptionLogger(new FileLogger());  

38.          _exceptionLogger.LogException(ex);  

39.       }  

40.    }  

41. }  



Delegates are two types


      -   Single Cast Delegates

      -  Multi Cast Delegates


Single Cast Delegates


Single cast delegate means which hold address of single method like as explained in above example.

Syntax of Delegate & Methods Declaration


Check below sample code for delegate declaration and methods declaration



public delegate int Delegatmethod(int a,int b);


public class Sampleclass

{

public int Add(int x, int y)

{

return x + y;

}

public int Sub(int x, int y)

{

return x + y;

}

}

If you observe above code I declared Delegatmethod method with two parameters which matching with methods declared in Sampleclass class.


Complete Example



public delegate int DelegatSample(int a,int b);

public class Sampleclass

{

public int Add(int x, int y)

{

return x + y;

}

public int Sub(int x, int y)

{

return x - y;

}

}

class Program

{

static void Main(string[] args)

{

Sampleclass sc=new Sampleclass();


DelegatSample delgate1 = sc.Add;

int i = delgate1(10, 20);

Console.WriteLine(i);

DelegatSample delgate2 = sc.Sub;

int j = delgate2(20, 10);

Console.WriteLine(j);

}

}

Output



Whenever we run above code we will get output like as shown below



Add Result : 30

Sub Result : 10



Multicast Delegates



Multi cast delegate is used to hold address of multiple methods in single delegate. To hold multiple addresses with delegate we will use overloaded += operator and if you want remove addresses from delegate we need to use overloaded operator -=


Multicast delegates will work only for the methods which have return type only void. If we want to create a multicast delegate with return type we will get the return type of last method in the invocation list


Check below sample code for delegate declaration and methods declaration



Syntax of Multicast Delegate & Method Declaration


Check below sample code for multicast delegate declaration and methods declaration



public delegate void MultiDelegate(int a,int b);

public class Sampleclass

{

public static void Add(int x, int y)

{

Console.WriteLine("Addition Value: "+(x + y));

}

public static void Sub(int x, int y)

{

Console.WriteLine("Subtraction Value: " + (x - y));

}

public static void Mul(int x, int y)

{

Console.WriteLine("Multiply Value: " + (x * y));

}

}

If you observe above code I declared MultiDelegate method with void return type.


Complete Example



public delegate void MultiDelegate(int a,int b);

public class Sampleclass

{

public static void Add(int x, int y)

{

Console.WriteLine("Addition Value: "+(x + y));

}

public static void Sub(int x, int y)

{

Console.WriteLine("Subtraction Value: " + (x - y));

}

public static void Mul(int x, int y)

{

Console.WriteLine("Multiply Value: " + (x * y));

}

}

class Program

{

static void Main(string[] args)

{

Sampleclass sc=new Sampleclass();

MultiDelegate del = Sampleclass.Add;

del += Sampleclass.Sub;

del += Sampleclass.Mul;

del(10, 5);

Console.ReadLine();

}

}

Output



Whenever we run above code we will get output like as shown below



Addition Value : 15

Subtraction Value : 5

Multiply Value : 50

A generic type is declared by specifying a type parameter in an angle brackets after a type name, e.g. TypeName<T> where T is a type parameter.

Generic Class

Generic classes are defined using a type parameter in an angle brackets after the class name. The following defines a generic class.

Example: Define Generic Class

class DataStore<T>

{

    public T Data { get; set; }

}


You can also define multiple type parameters separated by a comma.

Example: Generic Class with Multiple Type Parameters

class KeyValuePair<TKey, TValue>

{

    public TKey Key { get; set; }

    public TValue Value { get; set; }

}


Example:

using System;

public class Program

{

public static void Main()

{

DataStore<string> cities = new DataStore<string>();

cities.AddOrUpdate(0, "Mumbai");

cities.AddOrUpdate(1, "Chicago");

cities.AddOrUpdate(2, "London");


Console.WriteLine(cities.GetData(100));


DataStore<int> empIds = new DataStore<int>();

empIds.AddOrUpdate(0, 50);

empIds.AddOrUpdate(1, 65);

empIds.AddOrUpdate(2, 89);

Console.WriteLine(empIds.GetData(0));

}

}


class DataStore<T>

{

    private T[] _data = new T[10];

    

    public void AddOrUpdate(int index, T item)

    {

        if(index >= 0 && index < 10)

            _data[index] = item;

    }


public T GetData(int index){

if(index >= 0 && index < 10)

return _data[index];

else

return default(T);

}

}


Cached Result


50



lambda expression 

use a lambda expression to create an anonymous function. Use the lambda declaration operator => to separate the lambda's parameter list from its body. A lambda expression can be of any of the following two forms:

Func<int, int> square = x => x * x;

Console.WriteLine(square(5));

// Output:

// 25


(input-parameters) => { <sequence-of-statements> }


Two or more input parameters are separated by commas:

Func<int, int, bool> testForEquality = (x, y) => x == y;


Expression Lambda: Consists of the input and the expression.

Syntax:

input => expression;


// List to store numbers

        List<int> numbers = new List<int>() {36, 71, 12,

                             15, 29, 18, 27, 17, 9, 34};

 

        // foreach loop to display the list

        Console.Write("The list : ");

        foreach(var value in numbers)

        {

            Console.Write("{0} ", value);

        }

        Console.WriteLine();

 

        // Using lambda expression

        // to calculate square of

        // each value in the list

        var square = numbers.Select(x => x * x);

 

        // foreach loop to display squares

        Console.Write("Squares : ");

        foreach(var value in square)

        {

            Console.Write("{0} ", value);

        }

        Console.WriteLine();



1. List<int> list = new List<int>() { 1, 2, 3, 4, 5, 6 };  

2.         List<int> evenNumbers = list.FindAll(x => (x % 2) == 0);  

3.   

4.         foreach (var num in evenNumbers)  

5.         {  

6.             Console.Write("{0} ", num);  

7.         }  

8.         Console.WriteLine();  

9.         Console.Read();  



You can also create lambda expressions with more than one parameter, as shown here:

Func<int, int, int> multiplyTwoNumbers = (a, b) => a * b;

// Returns 35

int result = multiplyTwoNumbers(7, 5);

LINQ:


IList<Student> studentList = new List<Student>() { 

    new Student() { StudentID = 1, StudentName = "John", Age = 18, StandardID = 1 } ,

    new Student() { StudentID = 2, StudentName = "Steve",  Age = 21, StandardID = 1 } ,

    new Student() { StudentID = 3, StudentName = "Bill",  Age = 18, StandardID = 2 } ,

    new Student() { StudentID = 4, StudentName = "Ram" , Age = 20, StandardID = 2 } ,

    new Student() { StudentID = 5, StudentName = "Ron" , Age = 21 } 

};


IList<Standard> standardList = new List<Standard>() { 

    new Standard(){ StandardID = 1, StandardName="Standard 1"},

    new Standard(){ StandardID = 2, StandardName="Standard 2"},

    new Standard(){ StandardID = 3, StandardName="Standard 3"}

};


Multiple Select and where Operator

var studentNames = studentList.Where(s => s.Age > 18)

                              .Select(s => s)

                              .Where(st => st.StandardID > 0)

                              .Select(s => s.StudentName);


LINQ Query returns Collection of Anonymous Objects

var teenStudentsName = from s in studentList

                       where s.age > 12 && s.age < 20

                       select new { StudentName = s.StudentName };




LINQ GroupBy Query - C#

var studentsGroupByStandard = from s in studentList

                              group s by s.StandardID into sg

                              orderby sg.Key 

                                    select new { sg.Key, sg };



foreach (var group in studentsGroupByStandard)

{

    Console.WriteLine("StandardID {0}:", group.Key);

    

    group.sg.ToList().ForEach(st => Console.WriteLine(st.StudentName ));

}


Left outer join

Use left outer join to display students under each standard. Display the standard name even if there is no student assigned to that standard.

Example: LINQ Left Outer Join - C#

using System;

using System.Linq;

using System.Collections.Generic;


public class Program

{

public static void Main()

{

// Student collection

IList<Student> studentList = new List<Student>() { 

new Student() { StudentID = 1, StudentName = "John", Age = 18, StandardID = 1 } ,

new Student() { StudentID = 2, StudentName = "Steve",  Age = 21, StandardID = 1 } ,

new Student() { StudentID = 3, StudentName = "Bill",  Age = 18, StandardID = 2 } ,

new Student() { StudentID = 4, StudentName = "Ram" , Age = 20, StandardID = 2 } ,

new Student() { StudentID = 5, StudentName = "Ron" , Age = 21 } 

};

IList<Standard> standardList = new List<Standard>() { 

new Standard(){ StandardID = 1, StandardName="Standard 1"},

new Standard(){ StandardID = 2, StandardName="Standard 2"},

new Standard(){ StandardID = 3, StandardName="Standard 3"}

};

var studentsGroup = from stad in standardList

                    join s in studentList

                    on stad.StandardID equals s.StandardID

                        into sg

                        select new { 

                                        StandardName = stad.StandardName, 

                                        Students = sg 

                                    };


foreach (var group in studentsGroup)

{

Console.WriteLine(group.StandardName);

group.Students.ToList().ForEach(st => Console.WriteLine(st.StudentName));

}



}

}


public class Student{


public int StudentID { get; set; }

public string StudentName { get; set; }

public int Age { get; set; }

public int StandardID { get; set; }

}


public class Standard{


public int StandardID { get; set; }

public string StandardName { get; set; }

}


Cached Result

Standard 1

John

Steve

Standard 2

Bill

Ram

Standard 3



IEnumerable :


// Query #1.

List<int> numbers = new List<int>() { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };


// The query variable can also be implicitly typed by using var

IEnumerable<int> filteringQuery =

    from num in numbers

    where num < 3 || num > 7

    select num;


// Query #2.

IEnumerable<int> orderingQuery =

    from num in numbers

    where num < 3 || num > 7

    orderby num ascending

    select num;


// Query #3.

string[] groupingQuery = { "carrots", "cabbage", "broccoli", "beans", "barley" };

IEnumerable<IGrouping<char, string>> queryFoodGroups =

    from item in groupingQuery

    group item by item[0];



// Query #6.

IEnumerable<int> largeNumbersQuery = numbers2.Where(c => c > 15);


var queryLondonCustomers = from cust in customers

                           where cust.City == "London"

                           select cust;



var queryLondonCustomers3 =

    from cust in customers

    where cust.City == "London"

    orderby cust.Name ascending

    select cust;




/ queryCustomersByCity is an IEnumerable<IGrouping<string, Customer>>

  var queryCustomersByCity =

      from cust in customers

      group cust by cust.City;

===================================

// custQuery is an IEnumerable<IGrouping<string, Customer>>

var custQuery =

    from cust in customers

    group cust by cust.City into custGroup

    where custGroup.Count() > 2

    orderby custGroup.Key

    select custGroup;

we select users who live in Bratislava.

var res = from user in users

          where user.City == "Bratislava"

          select new { user.Name, user.City }


1. int[] nums = new int[] {0,1,2};  

2. var res = from a in nums where a < 3 orderby a select a;  

3. foreach(int i in res)   

4. Console.WriteLine(i);


1. var mypatient = from pa in patients  

2.                   where pa.Age > 20  

3.                   orderby pa.PatientName, pa.Gender, pa.Age  

4.                   select pa;  

5.         foreach(var pp in mypatient)  

6.         {  

7.         Debug.WriteLine(pp.PatientName + " "+ pp.Age + " " + pp.Gender);  

8.         } 




IEnumerable

1. IEnumerable exists in the System.Collections namespace.


 

 

2. IEnumerable is suitable for querying data from in-memory collections like List, Array and so on.

 

3. While querying data from the database, IEnumerable executes "select query" on the server-side, loads data in-memory on the client-side and then filters the data.


 

 

4. IEnumerable is beneficial for LINQ to Object and LINQ to XML queries.

IQueryable

1. IQueryable exists in the System.Linq Namespace.


 

 

2. IQueryable is suitable for querying data from out-memory (like remote database, service) collections.

 

3. While querying data from a database, IQueryable executes a "select query" on server-side with all filters.


  

4. IQueryable is beneficial for LINQ to SQL queries.




 IEnumerable

1. IEnumerable exists in System.Collections Namespace.

2. IEnumerable can move forward only over a collection, it can’t move backward and between the items.

3. IEnumerable is best to query data from in-memory collections like List, Array, etc.

4. While query data from a database, IEnumerable execute a select query on the server side, load data in-memory on a client-side and then filter data.

5. IEnumerable is suitable for LINQ to Object and LINQ to XML queries.

6. IEnumerable supports deferred execution.

7. IEnumerable doesn’t support custom query.

8. IEnumerable doesn’t support lazy loading. Hence not suitable for paging like scenarios.

9. Extension methods support by IEnumerable takes functional objects.


IQueryable

1. IQueryable exists in System. Linq Namespace.

2. IQueryable can move forward only over a collection, it can’t move backward and between the items.

3. IQueryable is best to query data from out-memory (like remote database, service) collections.

4. While query data from a database, IQueryable execute the select query on the server side with all filters.

5. IQueryable is suitable for LINQ to SQL queries.

6. IQueryable supports deferred execution.

7. IQueryable supports custom query using CreateQuery and Execute methods.

8. IQueryable support lazy loading. Hence it is suitable for paging like scenarios.

9. Extension methods support by IQueryable takes expression objects means expression tree.



IQueryable

dbContext dc = new dbContext ();

IQueryable<SocialMedia> list = dc.SocialMedias.Where(p => p.Name.StartsWith("T"));

list = list.Take<SocialMedia>(1);

IEnumerable

dbContext dc = new dbContext ();

IEnumerable

<SocialMedia>

list = dc.SocialMedias.Where(p => p.Name.StartsWith("T"));

list = list.Take<SocialMedia>(1);

</SocialMedia>



IEnumerable:

1. IEnumerable is an interface that is available in the System.Collections namespace.

2. While querying the data from the database, the IEnumerable executes the “select statement” on the server-side (i.e. on the database), loads data into memory on the client-side, and then only applied the filters on the retrieved data.

3. So you need to use the IEnumerable when you need to query the data from in-memory collections like List, Array, and so on.

4. The IEnumerable is mostly used for LINQ to Object and LINQ to XML queries.

5. The IEnumerable collection is of type forward only. That means it can only move in forward, it can’t move backward and between the items.

6. IEnumerable supports deferred execution.

7. It doesn’t support custom queries.

8. The IEnumerable doesn’t support lazy loading. Hence, it is not suitable for paging like scenarios.

IQueryable:

1. The IQueryable is an interface that exists in the System.Linq Namespace.

2. While querying the data from a database, the IQueryable executes the “select query” with the applied filter on the server-side i.e. on the database, and then retrieves data.

3. So you need to use the IQueryable when you want to query the data from out-memory such as remote database, service, etc.

4. IQueryable is mostly used for LINQ to SQL and LINQ to Entities queries.

5. The collection of type IQueryable can move only forward, it can’t move backward and between the items.

6. IQueryable supports deferred execution.

7. It also supports custom queries using CreateQuery and Executes methods.

8. IQueryable supports lazy loading and hence it is suitable for paging like scenarios.



The method dispose( ) is invoked by the user. The method finalize( ) is invoked by the garbage collector. Method dispose( ) is used to free unmanaged resources whenever it is invoked. Method finalize( ) is used to free unmanaged resources before the object is destroyed.


Dispose

 

Some objects expose Close and Dispose two methods. For Stream classes both serve the same purpose. Dispose method calls Close method inside.

1. void Dispose()   

2. {  

3.     this.Close();  

4. }  


Finalize

 

Finalize method also called destructor to the class. Finalize method can not be called explicitly in the code. Only Garbage collector can call the the Finalize when object become inaccessible. Finalize method cannot be implemented directly it can only be implement via declaring destructor. Following class illustrate, how to declare destructor. It is recommend that implement Finalize and Dispose method together if you need to implement Finalize method. After compilation destructor becomes Finalize method.

1. public class MyClass: IDisposable {  

2.   

3.     //Construcotr   

4.     public MyClass() {  

5.         //Initialization:   

6.     }  

7.   

8.     //Destrucor also called Finalize   

9.     ~MyClass() {  

10.         this.Dispose();  

11.     }  

12.   

13.     public void Dispose() {  

14.         //write code to release unmanaged resource.   

15.     }  

16. }  



Dispose vs. Finalize


Finalize

Finalizers should always be protected, not public or private so that the method cannot be called from the application's code directly and at the same time, it can make a call to the base.Finalize method

Finalizers should release unmanaged resources only.

The framework does not guarantee that a finalizer will execute at all on any given instance.

Never allocate memory in finalizers or call virtual methods from finalizers.

Avoid synchronization and raising unhandled exceptions in the finalizers.

The execution order of finalizers is non-deterministic—in other words, you can't rely on another object still being available within your finalizer.

Do not define finalizers on value types.

Don't create empty destructors. In other words, you should never explicitly define a destructor unless your class needs to clean up unmanaged resources and if you do define one, it should do some work. If, later, you no longer need to clean up unmanaged resources in the destructor, remove it altogether.

Dispose

Implement IDisposable on every type that has a finalizer

Ensure that an object is made unusable after making a call to the Dispose method. In other words, avoid using an object after the Dispose method has been called on it.

Call Dispose on all IDisposable types once you are done with them

Allow Dispose to be called multiple times without raising errors.

Suppress later calls to the finalizer from within the Dispose method using the GC.SuppressFinalize method

Avoid creating disposable value types

Avoid throwing exceptions from within Dispose methods

Dispose/Finalized Pattern

Microsoft recommends that you implement both Dispose and Finalize when working with unmanaged resources. The Finalize implementation would run and the resources would still be released when the object is garbage collected even if a developer neglected to call the Dispose method explicitly.

Cleanup the unmanaged resources in the Finalize method as well as Dispose method. Additionally call the Dispose method for any .NET objects that you have as components inside that class(having unmanaged resources as their member) from the Dispose method.


Finalize

Finalize() is called by the Garbage Collector before an object that is eligible for collection is reclaimed. Garbage collector will take the responsibility to deallocate the memory for the unreferenced object. The Garbage Collector calls this method at some point after there are no longer valid references to that object in memory.

The framework does not guarantee that when this will happen, we can force for Garbage Collection but it will hurt performance of a program. Finalize() belongs to the Object class and it will be called by the runtime.

Example

using System;

namespace DemoApplication{

   public class Demo{

      ~Demo(){

         Console.WriteLine("Finalize called");

      }

   }

}

Dispose

There are some resources like windows handles, database connections, network connections, files, etc. which cannot be collected by the Garbage Collector. If we want to explicitly release some specific objects then this is the best to implement IDisposable and override the Dispose() method of IDisposable interface.

The Dispose() method is not called automatically and we must explicitly call it from a client application when an object is no longer needed. Dispose() can be called even if other references to the object are alive.

Example

using System;

namespace DemoApplication{

   public class Demo : IDisposable{

      private bool disposed = false;

      public void Dispose(){

         Dispose(true);

         GC.SuppressFinalize(this);

      }

      protected virtual void Dispose(bool disposing){

         if (!disposed){

            if (disposing){

               //clean up managed objects

            }

            //clean up unmanaged objects

            disposed = true;

         }

      }

   }

}

Microsoft recommends that we implement both Dispose and Finalize when working with unmanaged resources. The Finalize implementation would run and the resources would still be released when the object is garbage collected even if a developer neglected to call the Dispose method explicitly.


What is a Repository Design Pattern?

 

By definition, the Repository Design Pattern in C# mediates between the domain and the data mapping layers using a collection-like interface for accessing the domain objects. Repository Design Pattern separates the data access logic and maps it to the entities in the business logic. It works with the domain entities and performs data access logic. In the Repository pattern, the domain entities, the data access logic, and the business logic talk to each other using interfaces. It hides the details of data access from the business logic.

 

Advantages of Repository Design Pattern

Testing controllers becomes easy because the testing framework need not run against the actual database access code.

Repository Design Pattern separates the actual database, queries and other data access logic from the rest of the application.

Business logic can access the data object without having knowledge of the underlying data access architecture.

Business logic is not aware of whether the application is using LINQ to SQL or ADO.NET. In the future, underlying data sources or architecture can be changed without affecting the business logic.

Caching strategy for the data source can be centralized.

Centralizing the data access logic, so code maintainability is easier

Paste this Code to IProductRepository.cs,

1. using System;  

2. using System.Collections.Generic;  

3. using System.Linq;  

4. using System.Text;  

5. using System.Threading.Tasks;  

6.   

7. namespace Repositiory_Pattern.Models.DAL  

8. {  

9.     interface IProductRepository  

10.     {  

11.         IEnumerable<Product_Table> GetProducts();  

12.         Product_Table GetProductById(int ProductId);  

13.         void InsertProduct(Product_Table product_);  

14.         void UpdateProduct(Product_Table product_);  

15.         void DeleteProduct(int ProductId);  

16.         void SaveChanges();  

17.     }  

18.


19.

IProductRepository.cs


Create a class in the DAL folder (ProductRepository.cs) This will be child class and will implement all the methods of (IProductRepository.cs) class. Paste this Code.


1. using System;  

2. using System.Collections.Generic;  

3. using System.Linq;  

4. using System.Web;  

5.   

6. namespace Repositiory_Pattern.Models.DAL  

7. {  

8.     public class ProductRepository:IProductRepository  

9.     {  

10.         private DataContext _dataContext;  

11.         public ProductRepository(DataContext dataContext)  

12.         {  

13.             this._dataContext = dataContext;  

14.         }  

15.         public void DeleteProduct(int ProductId)  

16.         {  

17.             Product_Table product_ = _dataContext.Product_Table.Find(ProductId);  

18.             _dataContext.Product_Table.Remove(product_);  

19.         }  

20.         public Product_Table GetProductById(int ProductId)  

21.         {  

22.             return _dataContext.Product_Table.Find(ProductId);  

23.         }  

24.         public IEnumerable<Product_Table> GetProducts()  

25.         {  

26.             return _dataContext.Product_Table.ToList();  

27.         }  

28.         public void InsertProduct(Product_Table product_)  

29.         {  

30.             _dataContext.Product_Table.Add(product_);  

31.         }  

32.         public void SaveChanges()  

33.         {  

34.             _dataContext.SaveChanges();  

35.         }  

36.         public void UpdateProduct(Product_Table product_)  

37.         {  

38.          _dataContext.Entry(product_).State = System.Data.Entity.EntityState.Modified;  

39.         }  

40.     }  

41.



ProductRepository.cs

 

 

 

Step 7

 

Now just go to our built-in Home Controller and paste this code.

1. using Repositiory_Pattern.Models;  

2. using Repositiory_Pattern.Models.DAL;  

3. using System;  

4. using System.Collections.Generic;  

5. using System.Linq;  

6. using System.Web;  

7. using System.Web.Mvc;  

8.   

9. namespace Repositiory_Pattern.Controllers  

10. {  

11.     public class HomeController : Controller  

12.     {  

13.         private IProductRepository _productRepository;  

14.         public HomeController()  

15.         {  

16.             this._productRepository = new ProductRepository(new DataContext());  

17.         }  

18.         public ActionResult Index()  

19.         {  

20.             return View(_productRepository.GetProducts());  

21.         }       

22.         public ActionResult Create()  

23.         {  

24.             return View(new Product_Table());  

25.         }  

26.         [HttpPost]  

27.         public ActionResult Create(Product_Table product)  

28.         {  

29.             _productRepository.InsertProduct(product);  

30.             _productRepository.SaveChanges();  

31.             return RedirectToAction("Index");  

32.         }              

33.         public ActionResult Update(int Id)  

34.         {  

35.             return View(_productRepository.GetProductById(Id));  

36.         }  

37.         [HttpPost]  

38.         public ActionResult Update(Product_Table product)  

39.         {  

40.             _productRepository.UpdateProduct(product);  

41.             _productRepository.SaveChanges();  

42.             return RedirectToAction("Index");  

43.         }              

44.         public ActionResult Delete(int Id)  

45.         {  

46.             _productRepository.DeleteProduct(Id);  

47.             _productRepository.SaveChanges();  

48.             return RedirectToAction("Index");  

49.         }  

50.     }  

51.


Delegate :

1. A delegate is declared using the delegate keyword.

2. Delegate is a function pointer. It holds the reference of one or more methods at runtime.

3. Delegate is independent and not dependent on events.

4. Delegate includes Combine() and Remove() methods to add methods to the invocation list.

5. A delegate can be passed as a method parameter.

6. = operator is used to assigning a single method, and += operator is used to assign multiple methods to a delegate.1.

Event


1. An event is declared using the event keyword.

2. The event is a notification mechanism that depends on delegates.

3. An event is dependent on a delegate and cannot be created without delegates. Event is a wrapper around delegate instance to prevent users of the delegate from resetting the delegate and its invocation list and only allows adding or removing targets from the invocation list.

4. EventInfo class inspect events and to hook up event handlers that include methods AddEventHandler() and RemoveEventHandler() methods to add and remove methods to invocation list, respectively.

5. An event is raised but cannot be passed as a method parameter.

6. = operator cannot be used with events, and only += and -= operator can be used with an event that adds or remove event handler. These methods internally call AddEventHandler and RemoveEventHandler methods.


1. using System;  

2.   

3. namespace Delegates  

4. {  

5.     public class Operation  

6.     {  

7.         public static void Add(int a)  

8.         {  

9.             Console.WriteLine("Addition={0}", a + 10);  

10.         }  

11.         public static void Square(int a)  

12.         {  

13.             Console.WriteLine("Multiple={0}",a * a);  

14.         }  

15.     }  

16.     class Program  

17.     {  

18.         delegate void DelOp(int x);  

19.    

20.         static void Main(string[] args)  

21.         {  

22.             // Delegate instantiation  

23.             DelOp obj = Operation.Add;  

24.             obj += Operation.Square;  

25.    

26.             obj(2);  

27.             obj(8);  

28.    

29.             Console.ReadLine();  

30.         }  

31.     }  

32. }  

Defining an event is a two-step process. First, you need to define a delegate type that will hold the list of methods to be called when the event is fired. Next, you declare an event using the event keyword. To illustrate the event, we are creating a console application. In this iteration, we will define an event to add that is associated to a single delegate DelEventHandler. 

1. using System;  

2.   

3. namespace Delegates  

4. {  

5.     public delegate void DelEventHandler();  

6.   

7.     class Program  

8.     {  

9.         public static event DelEventHandler add;  

10.    

11.         static void Main(string[] args)  

12.         {  

13.             add += new DelEventHandler(USA);  

14.             add += new DelEventHandler(India);  

15.             add += new DelEventHandler(England);  

16.             add.Invoke();  

17.    

18.             Console.ReadLine();  

19.         }  

20.         static void USA()  

21.         {  

22.             Console.WriteLine("USA");    

23.         }  

24.    

25.         static void India()  

26.         {  

27.             Console.WriteLine("India");  

28.         }  

29.    

30.         static void England()  

31.         {  

32.             Console.WriteLine("England");  

33.         }  

34.     } 

35.  

36. }


In the main method, we associate the event with its corresponding event handler with a function reference. Here, we are also filling the delegate invocation lists with a couple of defined methods using the +=operator as well. Finally, we invoke the event via the Invoke method.


Note: Event Handlers can't return a value. They are always void.


Let's use another example to get a better understanding of events. Here we are defining an event name as xyz and a delegate EventHandler with a string argument signature in the operation class. We are putting the implementation in the action method to confirm whether an event is fired or not. 


1. using System;  

2. using System.Drawing;  

3. using System.Windows.Forms;  

4.   

5. namespace Delegates  

6. {  

7.     //custom delegate  

8.     public delegate void DelEventHandler();  

9.   

10.     class Program :Form   

11.     {  

12.         //custom event  

13.         public event DelEventHandler add;  

14.   

15.         public Program()  

16.         {  

17.             // desing a button over form  

18.             Button btn = new Button();  

19.             btn.Parent = this;  

20.             btn.Text = "Hit Me";  

21.             btn.Location = new Point(100,100);  

22.   

23.             //Event handler is assigned to  

24.             // the button click event  

25.             btn.Click += new EventHandler(onClcik);  

26.             add += new DelEventHandler(Initiate);  

27.   

28.             //invoke the event  

29.             add();  

30.         }  

31.         //call when event is fired  

32.         public void Initiate()  

33.         {  

34.             Console.WriteLine("Event Initiated");  

35.         }  

36.    

37.         //call when button clicked  

38.         public void onClcik(object sender, EventArgs e)  

39.         {  

40.             MessageBox.Show("You clicked me");    

41.         }  

42.         static void Main(string[] args)  

43.         {  

44.             Application.Run(new Program());  

45.    

46.             Console.ReadLine();  

47.         }  

48.     }  

49.   

50. }  



Delegates and events both play an important role in the event-driven program. The delegates can refer to a method, and it is quite similar to the function pointer in C++. Events notify that some action has been performed. 


The basic difference between delegates and events is that delegates hold the reference of the methods and event provides a way to access that method using delegates.


Definition of Events

Events are the action performed which changes the state of an object. Events are declared using delegates, without the presence of delegates you can not declare events. You can say that an event provides encapsulation to the delegates.

There are two important compounds when dealing with events that are event and event handler. If the objects are interested in the event, then it registers an event handler for that particular event. And whenever the events are fired the registered event handlers are called. The declaration of the event is as follow:

event event_delegate event_name;

Here, “event” is a keyword which shows the declaration of an event. Next, event_delegate show the name of the delegate that is supporting the event. Then, event_name is the name of the event

There are some important points to be remembered while dealing with the events. Like delegates, events can also be multicast i.e. multiple objects which are chained up (registered) to an event handler, respond when an event is raised.

The event handlers can be added to the chain using the operator “+=” and can be unchained using the operator “-=” Both static and instance method can be used as an event handler. The event accessor can give you the control of the implementation of event handler list.

The interface can contain events. Events can also be abstract whereas accessor based event can not be abstract. The event can be virtual and be overridden by derived class.

What is WCF?

WCF stands for Windows Communication Foundation. It is used to create a distributed and interoperable Applications. WCF is an effective platform for developing service-oriented applications. It allows developers to create secure, reliable, and high profile development solutions which can be integrated across platforms and interoperate with existing investments.

What is Web API?

ASP.NET Web API is a framework which allows you to build HTTP services for browsers and mobile devices. It is an ideal platform for developing RESTful applications on the ASP .NET Framework.

The ASP.NET Web API is an extensible framework for building HTTP based services that can be accessed in different applications on different platforms such a web, windows, mobile devices, etc.

KEY DIFFERENCE

WCF offers request-reply, one-way, or duplex while Web API is by default request-reply only.

WCF is used for developing SOAP-based services whereas Web API is used for both SOAP-based and RESTful services.

WCF does not offer any support for MVC features whereas Web API supports MVC features.

WCF supports HTTP, UDP, and custom transport protocol whereas Web API supports only HTTP protocol.

WCF offers Text, MTOM, and Binary Encoding support whereas Web API supports the UTF-8 encoding format.

WCF supports message queues, message security, duplex communication, transaction whereas Web API doesn’t support.

WCF stands for Windows Communication Foundation whereas API stands for Application Program Interface.

pros/benefits of using web API:

Web API provides supports for convention-based CRUD Actions since it works with HTTP verbs ( GET, PUT, POST, and DELETE)

It is easy to define, expose, and consume in a REST-ful way.

ASP.Net Web API allows you to showcase your data and services to various devices.

Web API is open source, so it is an ideal option for creating RESTful services over the .NET platform.

Allows you scalable, decoupled development of a Web Service which can be shared and used by other developers to access web services.

It is also helpful to build simple, non-SOAP-based HTTP Services.

Characteristics of WCF

Highly configurable transports

Better options for serialization

Well-integrated with IIS

Easy for self-hosting

Very rich metadata

Complex model for publication

Rich data contracts

Characteristics of Web API

Tightly coupled to HTTP

Easy to test

Offers support for content negotiation

Allows open-ended formatting

No reliance on a platform

Solid resource-orientation

Level 2 experience with nascent hypermedia support

Web Service

1. It is based on SOAP and return data in XML form.

2. It support only HTTP protocol.

3. It is not open source but can be consumed by any client that understands xml.

4. It can be hosted only on IIS.

WCF

1. It is also based on SOAP and return data in XML form.

2. It is the evolution of the web service(ASMX) and support various protocols like TCP, HTTP, HTTPS, Named Pipes, MSMQ.

1.                The main issue with WCF is, its tedious and extensive configuration.

2. It is not open source but can be consumed by any client that understands xml.

3. It can be hosted with in the applicaion or on IIS or using window service.

WCF Rest

1. To use WCF as WCF Rest service you have to enable webHttpBindings.

2. It support HTTP GET and POST verbs by [WebGet] and [WebInvoke] attributes respectively.

3. To enable other HTTP verbs you have to do some configuration in IIS to accept request of that particular verb on .svc files

4. Passing data through parameters using a WebGet needs configuration. The UriTemplate must be specified

5. It support XML, JSON and ATOM data format.

Web API

1. This is the new framework for building HTTP services with easy and simple way.

2. Web API is open source an ideal platform for building REST-ful services over the .NET Framework.

3. Unlike WCF Rest service, it use the full featues of HTTP (like URIs, request/response headers, caching, versioning, various content formats)

4. It also supports the MVC features such as routing, controllers, action results, filter, model binders, IOC container or dependency injection, unit testing that makes it more simple and robust.

5. It can be hosted with in the application or on IIS.

6. It is light weight architecture and good for devices which have limited bandwidth like smart phones.

7. Responses are formatted by Web API’s MediaTypeFormatter into JSON, XML or whatever format you want to add as a MediaTypeFormatter.

To Whom Choose Between WCF or WEB API

1. Choose WCF when you want to create a service that should support special scenarios such as one way messaging, message queues, duplex communication, etc.

2. Choose WCF when you want to create a service that can use fast transport channels when available, such as TCP, Named Pipes, or maybe even UDP (in WCF 4.5), and you also want to support HTTP when all other transport channels are unavailable.

3. Choose Web API when you want to create a resource-oriented service over HTTP that can use the full features of HTTP (like URIs, request/response headers, caching, versioning, various content formats).

4. Choose Web API when you want to expose your service to a broad range of clients including browsers, mobiles, iphone, and tablets.


AMQ Components

AMQ Streams

Based on the Apache Kafka project, AMQ Streams offers a distributed backbone that allows microservices and other applications to share data with extremely high throughput and extremely low latency.

Publish and subscribe - Many to many dissemination in a fault tolerant, durable manner

Long-term data retention - Efficiently stores data for immediate access in a manner limited only by disk space

Advanced queueing - Last value queues, message groups, topic hierarchies, and large message support

Replayable events - Serves as a repository for microservices to build in memory copies of source data, up to any point in time

Partition messages for scalability - Allows for organizing messages to maximum concurrent access

AMQ Streams is based on Strimzi and Apache Kafka projects.

AMQ Broker

AMQ Broker is a pure-Java multiprotocol message broker. It’s built on an efficient, asynchronous core with a fast native journal for message persistence and the option of shared-nothing state replication for high availability.

Persistence - A fast, native-IO journal or a JDBC-based store

High availability - Shared store or shared-nothing state replication

Advanced queueing - Last value queues, message groups, topic hierarchies, and large message support

Multiprotocol - AMQP 1.0, MQTT, STOMP, OpenWire, and HornetQ Core

Integration - Full integration with Red Hat JBoss EAP

AMQ Broker is based on the Apache ActiveMQ Artemis project.

AMQ Interconnect

AMQ Interconnect is a high-speed, low-latency AMQP 1.0 message router. You can deploy multiple AMQ Interconnect routers to build a fault-tolerant messaging network that connects your clients and brokers into one seamless fabric.

Disaster recovery - Deploy redundant network routers across geographies

Advanced routing - Control the distribution and processing of messages on your network

Integration - Connect clients, brokers, and standalone services

Management - Streamlined management makes large deployments practical

AMQ Interconnect is based on the Apache Qpid Dispatch project.

AMQ Online

Developers can use Red Hat AMQ Online to provision messaging when and where they need it with zero installation, configuration, and maintenance. The developers serve themselves from an easy to-use browser console. Administrators can also configure a cloud-native, multi-tenant messaging service in the cloud or on-premise. Their teams can then serve themselves messaging. Multiple development teams can provision the brokers and queues they need from a simple console, without requiring each team to install, configure, deploy, maintain, or patch any software..

Low or no administrative costs - Consolidate operations in one small team so developers can serve themselves

Easy provisioning - Work from a browser with no installation or configuration required

Multi-tenancy - Installation is managed as a unit, but instances are protected from each other

Flexible multi-language connectivity - Integrate systems and adapt to market demands and emerging technologies

Open standards - Provide interoperability between other vendor’s messaging clients and products providing protection from vendor lock-in

AMQ Online is based on the enmasse project.

AMQ Clients

AMQ Clients is a suite of AMQP 1.0 messaging APIs that allow you to make any application a messaging application. It includes both industry-standard APIs such as JMS and new event-driven APIs that make it easy to integrate messaging anywhere.

New event-driven APIs - Fast, efficient messaging that integrates everywhere

Industry-standard APIs - JMS 1.1 and 2.0

Broad language support - C++, Java, JavaScript, Python, and .NET

Wide availability - Linux, Windows, and JVM-based environments

Creational Design Pattern

1. Factory Method

2. Abstract Factory

3. Builder

4. Prototype

5. Singleton

Structural Design Patterns

1. Adapter

2. Bridge

3. Composite

4. Decorator

5. Façade

6. Flyweight

7. Proxy

Behavioral Design Patterns

1. Chain of Responsibility

2. Command

3. Interpreter

4. Iterator

5. Mediator

6. Memento

7. Observer

8. State

9. Strategy

10. Visitor

11. Template Method

Abstract Factory

This creates a set of related objects or dependent objects. The "family" of objects created by the factory is determined at run-time depending on the selection of concrete factory classes.

An abstract factory pattern acts as a super-factory that creates other factories. An abstract factory interface is responsible for creating a set of related objects or dependent objects without specifying their concrete classes. 


The classes and objects participating in the above class diagram can be identified as follows:

1. Product - CreditCard

2. ConcreteProduct- MoneyBackCreditCard, TitaniumCreditCard, PlatinumCreditCard

3. Creator- CardFactory

4. ConcreteCreator- MoneyBackCardFactory, TitaniumCardFactory, PlatinumCardFactory

Product

Here are the code blocks for each participant

1. namespace FactoryMethodDesignPatternInCSharp  

2. {  

3.     /// <summary>  

4.     /// The 'Product' Abstract Class  

5.     /// </summary>  

6.     abstract class CreditCard  

7.     {  

8.         public abstract string CardType { get; }  

9.         public abstract int CreditLimit { get; set; }  

10.         public abstract int AnnualCharge { get; set; }  

11.     }     

12.




ConcreteProduct

 

MoneyBackCreditCard:

1. using System;  

2.   

3. namespace FactoryMethodDesignPatternInCSharp  

4. {  

5.     /// <summary>  

6.     /// A 'ConcreteProduct' class  

7.     /// </summary>  

8.     class MoneyBackCreditCard : CreditCard  

9.     {  

10.         private readonly string _cardType;  

11.         private int _creditLimit;  

12.         private int _annualCharge;  

13.   

14.         public MoneyBackCreditCard(int creditLimit, int annualCharge)  

15.         {  

16.             _cardType = "MoneyBack";  

17.             _creditLimit = creditLimit;  

18.             _annualCharge = annualCharge;  

19.         }  

20.   

21.         public override string CardType  

22.         {  

23.             get { return _cardType; }  

24.         }  

25.   

26.         public override int CreditLimit  

27.         {  

28.             get { return _creditLimit; }  

29.             set { _creditLimit = value; }  

30.         }  

31.   

32.         public override int AnnualCharge  

33.         {  

34.             get { return _annualCharge; }  

35.             set { _annualCharge = value; }  

36.         }  

37.     }  

38. }  

TitaniumCreditCard:

1. using System;  

2.   

3. namespace FactoryMethodDesignPatternInCSharp  

4. {  

5.     /// <summary>  

6.     /// A 'ConcreteProduct' class  

7.     /// </summary>  

8.     class TitaniumCreditCard : CreditCard  

9.     {  

10.         private readonly string _cardType;  

11.         private int _creditLimit;  

12.         private int _annualCharge;  

13.   

14.         public TitaniumCreditCard(int creditLimit, int annualCharge)  

15.         {  

16.             _cardType = "Titanium";  

17.             _creditLimit = creditLimit;  

18.             _annualCharge = annualCharge;  

19.         }  

20.   

21.         public override string CardType  

22.         {  

23.             get { return _cardType; }  

24.         }  

25.   

26.         public override int CreditLimit  

27.         {  

28.             get { return _creditLimit; }  

29.             set { _creditLimit = value; }  

30.         }  

31.   

32.         public override int AnnualCharge  

33.         {  

34.             get { return _annualCharge; }  

35.             set { _annualCharge = value; }  

36.         }      

37.     }  

38. }  

2. PlatinumCreditCard:

1. using System;  

2.   

3. namespace FactoryMethodDesignPatternInCSharp  

4. {  

5.     /// <summary>  

6.     /// A 'ConcreteProduct' class  

7.     /// </summary>  

8.     class PlatinumCreditCard : CreditCard  

9.     {  

10.         private readonly string _cardType;  

11.         private int _creditLimit;  

12.         private int _annualCharge;  

13.   

14.         public PlatinumCreditCard(int creditLimit, int annualCharge)  

15.         {  

16.             _cardType = "Platinum";  

17.             _creditLimit = creditLimit;  

18.             _annualCharge = annualCharge;  

19.         }  

20.   

21.         public override string CardType  

22.         {  

23.             get { return _cardType; }  

24.         }  

25.   

26.         public override int CreditLimit  

27.         {  

28.             get { return _creditLimit; }  

29.             set { _creditLimit = value; }  

30.         }  

31.   

32.         public override int AnnualCharge  

33.         {  

34.             get { return _annualCharge; }  

35.             set { _annualCharge = value; }  

36.         }  

37.     }  

38. }  

3. Creator

1. namespace FactoryMethodDesignPatternInCSharp  

2. {  

3.     /// <summary>  

4.     /// The 'Creator' Abstract Class  

5.     /// </summary>  

6.     abstract class CardFactory  

7.     {  

8.         public abstract CreditCard GetCreditCard();  

9.     }  

10. }  

4. ConcreteCreator

 

MoneyBackFactory: 

1. namespace FactoryMethodDesignPatternInCSharp  

2. {  

3.     /// <summary>  

4.     /// A 'ConcreteCreator' class  

5.     /// </summary>  

6.     class MoneyBackFactory : CardFactory  

7.     {  

8.         private int _creditLimit;  

9.         private int _annualCharge;  

10.   

11.         public MoneyBackFactory(int creditLimit, int annualCharge)  

12.         {  

13.             _creditLimit = creditLimit;  

14.             _annualCharge = annualCharge;  

15.         }  

16.   

17.         public override CreditCard GetCreditCard()  

18.         {  

19.             return new MoneyBackCreditCard(_creditLimit, _annualCharge);  

20.         }  

21.     }  

22. }  

TitaniumFactory:

1. namespace FactoryMethodDesignPatternInCSharp      

2. {      

3.     class TitaniumFactory: CardFactory      

4.     {      

5.         private int _creditLimit;      

6.         private int _annualCharge;      

7.       

8.         public TitaniumFactory(int creditLimit, int annualCharge)      

9.         {      

10.             _creditLimit = creditLimit;      

11.             _annualCharge = annualCharge;      

12.         }      

13.       

14.         public override CreditCard GetCreditCard()      

15.         {      

16.             return new TitaniumCreditCard(_creditLimit, _annualCharge);      

17.         }      

18.     }      

19. }    

PlatinumFactory:

1. namespace FactoryMethodDesignPatternInCSharp      

2. {      

3.     class PlatinumFactory: CardFactory      

4.     {      

5.         private int _creditLimit;      

6.         private int _annualCharge;      

7.       

8.         public PlatinumFactory(int creditLimit, int annualCharge)      

9.         {      

10.             _creditLimit = creditLimit;      

11.             _annualCharge = annualCharge;      

12.         }      

13.       

14.         public override CreditCard GetCreditCard()      

15.         {      

16.             return new PlatinumCreditCard(_creditLimit, _annualCharge);      

17.         }      

18.     }      

19. }   

Factory Pattern Client Demo

1. using System;  

2.   

3. namespace FactoryMethodDesignPatternInCSharp  

4. {  

5.     /// <summary>  

6.     /// Factory Pattern Demo  

7.     /// </summary>    

8.     public class ClientApplication  

9.     {  

10.         static void Main()  

11.         {  

12.             CardFactory factory = null;  

13.             Console.Write("Enter the card type you would like to visit: ");  

14.             string car = Console.ReadLine();  

15.  

16.  

17.             switch (car.ToLower())  

18.             {  

19.                 case "moneyback":  

20.                     factory = new MoneyBackFactory(50000, 0);  

21.                     break;  

22.                 case "titanium":  

23.                     factory = new TitaniumFactory(100000, 500);  

24.                     break;  

25.                 case "platinum":  

26.                     factory = new PlatinumFactory(500000, 1000);  

27.                     break;  

28.                 default:  

29.                     break;  

30.             }  

31.   

32.             CreditCard creditCard = factory.GetCreditCard();  

33.             Console.WriteLine("\nYour card details are below : \n");  

34.             Console.WriteLine("Card Type: {0}\nCredit Limit: {1}\nAnnual Charge: {2}",  

35.                 creditCard.CardType, creditCard.CreditLimit, creditCard.AnnualCharge);  

36.             Console.ReadKey();  

37.         }  

38.     }  

39. }   

Output

 

Here, we are accepting input from the clients/users as to which class they want to instantiate.

 

 




C# - Implementation Code

interface Product

{


}


class ConcreteProductA : Product

{

}


class ConcreteProductB : Product

{

}


abstract class Creator

{

 public abstract Product FactoryMethod(string type);

}


class ConcreteCreator : Creator

{

 public override Product FactoryMethod(string type)

 {

 switch (type)

 {

 case "A": return new ConcreteProductA();

 case "B": return new ConcreteProductB();

 default: throw new ArgumentException("Invalid type", "type");

 }

 }

}




routes.MapHttpRoute(

    name: "ActionApi",

    routeTemplate: "api/{controller}/{action}/{id}",

    defaults: new { id = RouteParameter.Optional }

);

===========================

var app = builder.Build();


if (!app.Environment.IsDevelopment())

{

    app.UseExceptionHandler("/Home/Error");

    app.UseHsts();

}


app.UseHttpsRedirection();

app.UseStaticFiles();


app.UseRouting();


app.UseAuthorization();


app.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}");


app.Run();


public class MyDemoController : Controller

{

    [Route("")]

    [Route("Home")]

    [Route("Home/Index")]

    [Route("Home/Index/{id?}")]

    public IActionResult MyIndex(int? id)

    {

        return ControllerContext.MyDisplayRouteInfo(id);

    }

[Route("api/[controller]")]

[ApiController]

public class Test2Controller : ControllerBase

{

    [HttpGet]   // GET /api/test2

    public IActionResult ListProducts()

    {

        return ControllerContext.MyDisplayRouteInfo();

    }


    [HttpGet("{id}")]   // GET /api/test2/xyz

    public IActionResult GetProduct(string id)

    {

       return ControllerContext.MyDisplayRouteInfo(id);

    }


    [HttpGet("int/{id:int}")] // GET /api/test2/int/3

    public IActionResult GetIntProduct(int id)

    {

        return ControllerContext.MyDisplayRouteInfo(id);

    }


    [HttpGet("int2/{id}")]  // GET /api/test2/int2/3

    public IActionResult GetInt2Product(int id)

    {

        return ControllerContext.MyDisplayRouteInfo(id);

    }

}

Interface

 

Add the interface folder and then add the Interface in the data access layer In Interface and Define the Methods that you want to perform in the repository pattern.

1. using System;  

2. using System.Collections.Generic;  

3. using System.Text;  

4. using System.Threading.Tasks;  

5.   

6. namespace CRUD_DAL.Interface  

7. {  

8.     public interface IRepository<T>  

9.     {  

10.         public Task<T> Create(T _object);  

11.   

12.         public void Update(T _object);  

13.   

14.         public IEnumerable<T> GetAll();  

15.   

16.         public T GetById(int Id);  

17.   

18.         public void Delete(T _object);  

19.   

20.     }  

21. }  



    [Route("Home/About")]

    [Route("Home/About/{id?}")]

    public IActionResult MyAbout(int? id)

    {

        return ControllerContext.MyDisplayRouteInfo(id);

    }

}










Interface

 

Add the interface folder and then add the Interface in the data access layer In Interface and Define the Methods that you want to perform in the repository pattern.

1. using System;  

2. using System.Collections.Generic;  

3. using System.Text;  

4. using System.Threading.Tasks;  

5.   

6. namespace CRUD_DAL.Interface  

7. {  

8.     public interface IRepository<T>  

9.     {  

10.         public Task<T> Create(T _object);  

11.   

12.         public void Update(T _object);  

13.   

14.         public IEnumerable<T> GetAll();  

15.   

16.         public T GetById(int Id);  

17.   

18.         public void Delete(T _object);  

19.   

20.     }  

21. }  

Repository

 

Add the Repository Person Class Which Implements the Interface and performs the function defined in the interface like getting All Data, Delete All Data Update Data, Update data by userId like this.

 

 

1. using CRUD_DAL.Data;  

2. using CRUD_DAL.Interface;  

3. using CRUD_DAL.Models;  

4. using System;  

5. using System.Collections.Generic;  

6. using System.Linq;  

7. using System.Text;  

8. using System.Threading.Tasks;  

9.   

10. namespace CRUD_DAL.Repository  

11. {  

12.     public class RepositoryPerson : IRepository<Person>  

13.     {  

14.         ApplicationDbContext _dbContext;  

15.         public RepositoryPerson(ApplicationDbContext applicationDbContext)  

16.         {  

17.             _dbContext = applicationDbContext;  

18.         }  

19.         public async Task<Person> Create(Person _object)  

20.         {  

21.             var obj = await _dbContext.Persons.AddAsync(_object);  

22.             _dbContext.SaveChanges();  

23.             return obj.Entity;  

24.         }  

25.   

26.         public void Delete(Person _object)  

27.         {  

28.             _dbContext.Remove(_object);  

29.             _dbContext.SaveChanges();  

30.         }  

31.   

32.         public IEnumerable<Person> GetAll()  

33.         {  

34.             try  

35.             {  

36.                 return _dbContext.Persons.Where(x => x.IsDeleted == false).ToList();  

37.             }  

38.             catch (Exception ee)  

39.             {  

40.                 throw;  

41.             }  

42.         }  

43.   

44.         public Person GetById(int Id)  

45.         {  

46.             return _dbContext.Persons.Where(x => x.IsDeleted == false && x.Id == Id).FirstOrDefault();  

47.         }  

48.   

49.         public void Update(Person _object)  

50.         {  

51.             _dbContext.Persons.Update(_object);  

52.             _dbContext.SaveChanges();  

53.         }  

54.     }  

55. }  


1. using CRUD_DAL.Interface;  

2. using CRUD_DAL.Models;  

3. using System;  

4. using System.Collections.Generic;  

5. using System.Linq;  

6. using System.Text;  

7. using System.Threading.Tasks;  

8.   

9. namespace CRUD_BAL.Service  

10. {  

11.     public class PersonService  

12.     {  

13.         private readonly IRepository<Person> _person;  

14.   

15.         public PersonService(IRepository<Person> perosn)  

16.         {  

17.             _person = perosn;  

18.         }  

19.         //Get Person Details By Person Id  

20.         public IEnumerable<Person> GetPersonByUserId(string UserId)  

21.         {  

22.             return _person.GetAll().Where(x => x.UserEmail == UserId).ToList();  

23.         }  

24.         //GET All Perso Details   

25.         public IEnumerable<Person> GetAllPersons()  

26.         {  

27.             try  

28.             {  

29.                 return _person.GetAll().ToList();  

30.             }  

31.             catch (Exception)  

32.             {  

33.                 throw;  

34.             }  

35.         }  

36.         //Get Person by Person Name  

37.         public Person GetPersonByUserName(string UserName)  

38.         {  

39.             return _person.GetAll().Where(x => x.UserEmail == UserName).FirstOrDefault();  

40.         }  

41.         //Add Person  

42.         public async Task<Person> AddPerson(Person Person)  

43.         {  

44.             return await _person.Create(Person);  

45.         }  

46.         //Delete Person   

47.         public bool DeletePerson(string UserEmail)  

48.         {  

49.   

50.             try  

51.             {  

52.                 var DataList = _person.GetAll().Where(x => x.UserEmail == UserEmail).ToList();  

53.                 foreach (var item in DataList)  

54.                 {  

55.                     _person.Delete(item);  

56.                 }  

57.                 return true;  

58.             }  

59.             catch (Exception)  

60.             {  

61.                 return true;  

62.             }  

63.   

64.         }  

65.         //Update Person Details  

66.         public bool UpdatePerson(Person person)  

67.         {  

68.             try  

69.             {  

70.                 var DataList = _person.GetAll().Where(x => x.IsDeleted != true).ToList();  

71.                 foreach (var item in DataList)  

72.                 {  

73.                     _person.Update(item);  

74.                 }  

75.                 return true;  

76.             }  

77.             catch (Exception)  

78.             {  

79.                 return true;  

80.             }  

81.         }  

82.     }  

83. }  


Add the Transient in Startup Class

 

Modify the startup class like add the transient in the Startup class for adding the dependency injection transient services created whenever they are injected or requested.

1. using CRUD_BAL.Service;  

2. using CRUD_DAL.Data;  

3. using CRUD_DAL.Interface;  

4. using CRUD_DAL.Models;  

5. using CRUD_DAL.Repository;  

6. using Microsoft.AspNetCore.Builder;  

7. using Microsoft.AspNetCore.Hosting;  

8. using Microsoft.AspNetCore.HttpsPolicy;  

9. using Microsoft.AspNetCore.Mvc;  

10. using Microsoft.EntityFrameworkCore;  

11. using Microsoft.Extensions.Configuration;  

12. using Microsoft.Extensions.DependencyInjection;  

13. using Microsoft.Extensions.Hosting;  

14. using Microsoft.Extensions.Logging;  

15. using Microsoft.OpenApi.Models;  

16. using System;  

17. using System.Collections.Generic;  

18. using System.Linq;  

19. using System.Threading.Tasks;  

20.   

21. namespace CRUDAspNetCore5WebAPI  

22. {  

23.     public class Startup  

24.     {  

25.         private readonly IConfiguration _configuration;  

26.   

27.         public Startup(IConfiguration configuration)  

28.         {  

29.             _configuration = configuration;  

30.         }  

31.   

32.   

33.         // This method gets called by the runtime. Use this method to add services to the container.  

34.         public void ConfigureServices(IServiceCollection services)  

35.         {  

36.             services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(_configuration.GetConnectionString("DefaultConnection")));  

37.             services.AddControllers();  

38.             services.AddHttpClient();  

39.             services.AddTransient<IRepository<Person>, RepositoryPerson>();  

40.             services.AddTransient<PersonService, PersonService>();  

41.             services.AddControllers();  

42.             services.AddSwaggerGen(c =>  

43.             {  

44.                 c.SwaggerDoc("v1", new OpenApiInfo { Title = "CRUDAspNetCore5WebAPI", Version = "v1" });  

45.             });  

46.         }  

47.   

48.         // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.  

49.         public void Configure(IApplicationBuilder app, IWebHostEnvironment env)  

50.         {  

51.             if (env.IsDevelopment())  

52.             {  

53.                 app.UseDeveloperExceptionPage();  

54.                 app.UseSwagger();  

55.                 app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "CRUDAspNetCore5WebAPI v1"));  

56.             }  

57.   

58.             app.UseHttpsRedirection();  

59.   

60.             app.UseRouting();  

61.   

62.             app.UseAuthorization();  

63.   

64.             app.UseEndpoints(endpoints =>  

65.             {  

66.                 endpoints.MapControllers();  

67.             });  

68.         }  

69.     }  

70. }   


1. using CRUD_BAL.Service;  

2. using CRUD_DAL.Interface;  

3. using CRUD_DAL.Models;  

4. using Microsoft.AspNetCore.Http;  

5. using Microsoft.AspNetCore.Mvc;  

6. using Newtonsoft.Json;  

7. using System;  

8. using System.Collections.Generic;  

9. using System.Linq;  

10. using System.Threading.Tasks;  

11.   

12. namespace CRUDAspNetCore5WebAPI.Controllers  

13. {  

14.     [Route("api/[controller]")]  

15.     [ApiController]  

16.     public class PersonDetailsController : ControllerBase  

17.     {  

18.         private readonly PersonService _personService;  

19.   

20.         private readonly IRepository<Person> _Person;  

21.   

22.         public PersonDetailsController(IRepository<Person> Person, PersonService ProductService)  

23.         {  

24.             _personService = ProductService;  

25.             _Person = Person;  

26.   

27.         }  

28.         //Add Person  

29.         [HttpPost("AddPerson")]  

30.         public async Task<Object> AddPerson([FromBody] Person person)  

31.         {  

32.             try  

33.             {  

34.                 await _personService.AddPerson(person);  

35.                 return true;  

36.             }  

37.             catch (Exception)  

38.             {  

39.   

40.                 return false;  

41.             }  

42.         }  

43.         //Delete Person  

44.         [HttpDelete("DeletePerson")]  

45.         public bool DeletePerson(string UserEmail)  

46.         {  

47.             try  

48.             {  

49.                 _personService.DeletePerson(UserEmail);  

50.                 return true;  

51.             }  

52.             catch (Exception)  

53.             {  

54.                 return false;  

55.             }  

56.         }  

57.         //Delete Person  

58.         [HttpPut("UpdatePerson")]  

59.         public bool UpdatePerson(Person Object)  

60.         {  

61.             try  

62.             {  

63.                 _personService.UpdatePerson(Object);  

64.                 return true;  

65.             }  

66.             catch (Exception)  

67.             {  

68.                 return false;  

69.             }  

70.         }  

71.         //GET All Person by Name  

72.         [HttpGet("GetAllPersonByName")]  

73.         public Object GetAllPersonByName(string UserEmail)  

74.         {  

75.             var data = _personService.GetPersonByUserName(UserEmail);  

76.             var json = JsonConvert.SerializeObject(data, Formatting.Indented,  

77.                 new JsonSerializerSettings()  

78.                 {  

79.                     ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore  

80.                 }  

81.             );  

82.             return json;  

83.         }  

84.   

85.         //GET All Person  

86.         [HttpGet("GetAllPersons")]  

87.         public Object GetAllPersons()  

88.         {  

89.             var data = _personService.GetAllPersons();  

90.             var json = JsonConvert.SerializeObject(data, Formatting.Indented,  

91.                 new JsonSerializerSettings()  

92.                 {  

93.                     ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore  

94.                 }  

95.             );  

96.             return json;  

97.         }  

98.     }  

99. }  

100.

TESTING THE API

 

for Testing of API, I will Discuss the complete procedure in a separate article 

 

Project Resource Guideline

First, download the given Project from my GitHub repository / from article resources.

Open the project using visual studio

Go to package manager console

Add the migration

Update the database

Run the project

Enjoy the beauty of web service API with swagger UI

“Happy API Development”



DAL.cs

1. public static class DAL  

2. {  

3.     static ShowroomEntities DbContext;  

4.     static DAL()  

5.     {  

6.         DbContext = new ShowroomEntities();  

7.     }  

8.     public static List<Product> GetAllProducts()  

9.     {  

10.         return DbContext.Products.ToList();  

11.     }  

12.     public static Product GetProduct(int productId)  

13.     {  

14.         return DbContext.Products.Where(p => p.ProductId ==  productId).FirstOrDefault();  

15.     }  

16.     public static bool InsertProduct(Product productItem)  

17.     {  

18.         bool status;  

19.         try  

20.         {  

21.             DbContext.Products.Add(productItem);  

22.             DbContext.SaveChanges();  

23.             status = true;  

24.         }  

25.         catch (Exception)  

26.         {  

27.             status = false;  

28.         }  

29.         return status;  

30.     }  

31.     public static bool UpdateProduct(Product productItem)  

32.     {  

33.         bool status;  

34.         try  

35.         {  

36.             Product prodItem = DbContext.Products.Where(p => p.ProductId == productItem.ProductId).FirstOrDefault();  

37.             if (prodItem != null)  

38.             {  

39.                 prodItem.ProductName = productItem.ProductName;  

40.                 prodItem.Quantity = productItem.Quantity;  

41.                 prodItem.Price = productItem.Price;  

42.                 DbContext.SaveChanges();  

43.             }  

44.             status = true;  

45.         }  

46.         catch (Exception)  

47.         {  

48.             status = false;  

49.         }  

50.         return status;  

51.     }  

52.     public static bool DeleteProduct(int id)  

53.     {  

54.         bool status;  

55.         try  

56.         {  

57.             Product prodItem = DbContext.Products.Where(p => p.ProductId == id).FirstOrDefault();  

58.             if (prodItem != null)  

59.             {  

60.                 DbContext.Products.Remove(prodItem);  

61.                 DbContext.SaveChanges();  

62.             }  

63.             status = true;  

64.         }  

65.         catch (Exception)  

66.         {  

67.             status = false;  

68.         }  

69.         return status;  

70.     }  

71. }  

 WebApiConfig.cs

1. public static void Register(HttpConfiguration config)  

2.        {  

3.            // Web API configuration and services  

4.   

5.            // Web API routes  

6.            config.MapHttpAttributeRoutes();  

7.   

8.            config.Routes.MapHttpRoute(  

9.                name: "DefaultApi",  

10.                routeTemplate: "api/{controller}/{action}/{id}",  

11.                defaults: new { id = RouteParameter.Optional }  

12.            );  

13.


Product.cs

1. namespace WebApiService.Models  

2. {  

3.     public class Product  

4.     {  

5.         public int ProductId { get; set; }  

6.         public string ProductName { get; set; }  

7.         public Nullable<int> Quantity { get; set; }  

8.         public Nullable<int> Price { get; set; }  

9.     }  

10. }  

Copy the connection string from DataAccessLayer -> web.config and paste it in WebApiService -> web.config.

1. <connectionStrings>  

2.     <add name="ShowroomEntities" connectionString="metadata=res://*/ShowRoomEF.csdl|res://*/ShowRoomEF.ssdl|res://*/ShowRoomEF.msl;provider=System.Data.SqlClient;provider connection string="data source=MYSYSTEM\SQLEXPRESS;initial catalog=Showroom;user id=sa;password=xxxxx;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />  

3.   </connectionStrings>  



ShowroomController.cs

 

Showroom Controller takes care of Inserting, Retrieving, Updating and Deleting the data in the database. Request comes to this controller from the consuming application.

1. public class ShowroomController: ApiController {  

2.     // GET: Showroom  

3.     [HttpGet]  

4.     public JsonResult < List < Models.Product >> GetAllProducts() {  

5.             EntityMapper < DataAccessLayer.Product, Models.Product > mapObj = new EntityMapper < DataAccessLayer.Product, Models.Product > ();  

6.             List < DataAccessLayer.Product > prodList = DAL.GetAllProducts();  

7.             List < Models.Product > products = new List < Models.Product > ();  

8.             var config = new MapperConfiguration(cfg => cfg.CreateMap < Product, Models.Product > ());  

9.             var mapper = new Mapper(config);  

10.             foreach(var item in prodList) {  

11.                 products.Add(mapper.Map < Models.Product > (item));  

12.             }  

13.             return Json < List < Models.Product >> (products);  

14.         }  

15.         [HttpGet]  

16.     public JsonResult < Models.Product > GetProduct(int id) {  

17.             EntityMapper < DataAccessLayer.Product, Models.Product > mapObj = new EntityMapper < DataAccessLayer.Product, Models.Product > ();  

18.             DataAccessLayer.Product dalProduct = DAL.GetProduct(id);  

19.             Models.Product products = new Models.Product();  

20.             var config = new MapperConfiguration(cfg => cfg.CreateMap < Product, Models.Product > ());  

21.             var mapper = new Mapper(config);  

22.             products = mapper.Map < Models.Product > (dalProduct);  

23.             return Json < Models.Product > (products);  

24.         }  

25.         [HttpPost]  

26.     public bool InsertProduct(Models.Product product) {  

27.             bool status = false;  

28.             if (ModelState.IsValid) {  

29.                 EntityMapper < Models.Product, DataAccessLayer.Product > mapObj = new EntityMapper < Models.Product, DataAccessLayer.Product > ();  

30.                 DataAccessLayer.Product productObj = new DataAccessLayer.Product();  

31.                 var config = new MapperConfiguration(cfg => cfg.CreateMap < Models.Product, Product > ());  

32.                 var mapper = new Mapper(config);  

33.                 productObj = mapper.Map < Product > (product);  

34.                 status = DAL.InsertProduct(productObj);  

35.             }  

36.             return status;  

37.         }  

38.         [HttpPut]  

39.     public bool UpdateProduct(Models.Product product) {  

40.             EntityMapper < Models.Product, DataAccessLayer.Product > mapObj = new EntityMapper < Models.Product, DataAccessLayer.Product > ();  

41.             DataAccessLayer.Product productObj = new DataAccessLayer.Product();  

42.             var config = new MapperConfiguration(cfg => cfg.CreateMap < Models.Product, Product > ());  

43.             var mapper = new Mapper(config);  

44.             productObj = mapper.Map < Product > (product);  

45.             var status = DAL.UpdateProduct(productObj);  

46.             return status;  

47.         }  

48.         [HttpDelete]  

49.     public bool DeleteProduct(int id) {  

50.         var status = DAL.DeleteProduct(id);  

51.         return status;  

52.     }  

53. }  

Check your service

 

Execute your service created just now by running the below URL in the browser and change the port number accordingly.

 

http://localhost:52956/api/showroom/getallproducts 


WSHttpBinding:


<system.serviceModel>


    <client>

      <!-- this endpoint has an https: address -->

      <endpoint address="https://localhost/servicemodelsamples/service.svc" binding="wsHttpBinding" bindingConfiguration="Binding1" contract="Microsoft.Samples.TransportSecurity.ICalculator"/>

    </client>


    <bindings>

      <wsHttpBinding>

        <!-- configure wsHttpbinding with Transport security mode

                   and clientCredentialType as None -->

        <binding name="Binding1">

          <security mode="Transport">

            <transport clientCredentialType="None"/>

          </security>

        </binding>

      </wsHttpBinding>

    </bindings>


  </system.serviceModel>

 =============================



<%@ ServiceHost Language="C#" Debug="true" Service="WcfServiceAuth.Service.Service1" CodeBehind="Service1.svc.cs" %>


using System;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.ServiceModel.Web;

using System.Text;


namespace WcfServiceAuth.Service

{

    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.

    [ServiceContract]

    public interface IService1

    {


        [OperationContract]

        string GetData(int value);        


        // TODO: Add your service operations here

    }



    // Use a data contract as illustrated in the sample below to add composite types to service operations.

    [DataContract]

    public class CompositeType

    {

        bool boolValue = true;

        string stringValue = "Hello ";


        [DataMember]

        public bool BoolValue

        {

            get { return boolValue; }

            set { boolValue = value; }

        }


        [DataMember]

        public string StringValue

        {

            get { return stringValue; }

            set { stringValue = value; }

        }

    }

}


using System;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.ServiceModel.Web;

using System.Text;


namespace WcfServiceAuth.Service

{

    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.

    // NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.

    public class Service1 : IService1

    {

        public string GetData(int value)

        {

            return string.Format("You entered: {0}", value);

        }        

    }

}


<basicHttpBinding>

   <binding name = "basicHttpBindingDefaults" allowCookies = "false" 

      bypassProxyOnLocal = "false" hostNameComparisonMode = "StrongWildcard" 

      maxBufferPoolSize = "524288" maxBufferSize = "65536" 

      maxReceivedMessageSize = "65536" messageEncoding = "Text" proxyAddress = "" 

      textEncoding = "utf-8" transferMode = "Buffer" useDefaultWebProxy = "true" 

      closeTimeout = "00:01:00" openTimeout = "00:01:00" receiveTimeout = "00:10:00" 

      sendTimeout = "00:01:00">

   

      <readerQuotas maxArrayLength = "16384" maxBytesPerRead = "4096" 

         maxDepth = "32"

         maxNameTableCharCount = "16384" maxStringContentLength = "8192"/>


      <security mode = "None">

         <transport clientCredentialType = "None" proxyCredentialType = "None" realm = ""/>

         <message algorithmSuite = "Basic256" clientCredentialType = "UserName" />

      </security>       

   </binding>


</basicHttpBinding>      


[ServiceContract]

interface IMyContract

{...}

class MyService : IMyContract

{...}

public static void Main()

{

   Uri baseAddress = new

                    Uri("http://localhost:8000/");

   ServiceHost serviceHost;

   serviceHost = new

       ServiceHost(typeof(MyService),

                         baseAddress);

    

   serviceHost.Open();

   

   //Can do blocking calls:

   Application.Run(new MyForm());

    

   serviceHost.Close();

}


Administrative endpoint configuration

<system.serviceModel>

   <services>

      <service name = "MyNamespace.MyService">

         <endpoint

            address  = "http://localhost:8000/MyService"

            binding  = "wsHttpBinding"

            contract = "MyNamespace.IMyContract"

         />

      </service>

   </services>

</system.serviceModel>


Multiple endpoints on the same service

<service name = "MyService">

   <endpoint

      address  = "http://localhost:8000/MyService"

      binding  = "wsHttpBinding"

      contract = "IMyContract"

   />

   <endpoint

      address  = "net.tcp://localhost:8001/MyService"

      binding  = "netTcpBinding"

      contract = "IMyContract"

   />

   <endpoint

      address  = "net.tcp://localhost:8002/MyService"

      binding  = "netTcpBinding"

      contract = "IMyOtherContract"

   />

</service>

1. Basic Binding

This binding is provided by the BasicHttpBinding class. It is designed to expose a WCF service as an ASMX web service, so that old clients (which are still using ASMX web service) can consume new service. By default, it uses Http protocol for transport and encodes the message in UTF - 8 text for-mat. You can also use Https with this binding.

2. Web Binding

This binding is provided by the WebHttpBinding class. It is designed to expose WCF services as Http requests by using HTTP-GET, HTTP-POST. It is used with REST based services which may give output as an XML or JSON format. This is very much used with social networks for implementing a syndication feed.

3. Web Service (WS) Binding

This binding is provided by the WSHttpBinding class. It is like as Basic binding and uses Http or Https protocols for transport. But this is designed to offer various WS - * specifications such as WS – Reliable Messaging, WS - Transactions, WS - Security and so on which are not supported by Basic binding.

   wsHttpBinding= basicHttpBinding + WS-* specification

1.                      WS Dual Binding

This binding is provided by the WsDualHttpBinding class. It is like as wsHttpBinding except it sup-ports bi-directional communication means both clients and services can send and receive messages.

2. TCP Binding

This binding is provided by the NetTcpBinding class. It uses TCP protocol for communication be-tween two machines with in intranet (means same network). It encodes the message in binary format. This is faster and more reliable binding as compared to the Http protocol bindings. It is only used when communication is WCF - to – WCF means both client and service should have WCF.

3. IPC Binding

This binding is provided by the NetNamedPipeBinding class. It uses named pipe for Communication between two services on the same machine. This is the most secure and fastest binding among all the bindings.

4. MSMQ Binding

This binding is provided by the NetMsmqBinding class. It uses MSMQ for transport and offers sup-port to disconnected message queued. It provides solutions for disconnected scenarios in which service processes the message at a different time than the client send the messages.

5. Federated WS Binding

This binding is provided by the WSFederationHttpBinding class. It is a specialized form of WS binding and provides support to federated security.

6. Peer Network Binding

This binding is provided by the NetPeerTcpBinding class. It uses TCP protocol but uses peer net-working as transport. In this networking each machine (node) acts as a client and a server to the other nodes. This is used in the file sharing systems like torrent.

7. MSMQ Integration Binding

This binding is provided by the MsmqIntegrationBinding class. This binding offers support to communicate with existing systems that communicate via MSMQ.

                 

MyService.asmx

<%@ WebService Language="C#" CodeBehind="MyService.asmx.cs" Class="MyWebServiceDemo.MyService"%>

ASP.NET (C#)

MyService.asmx.cs

using System.Web.Script.Serialization;


using System.Web.Services;


namespace MyWebServiceDemo

{

    // Use "Namespace" attribute with an unique name,to make service uniquely discoverable

    [WebService(Namespace = "http://tempuri.org/")]

    // To indicate service confirms to "WsiProfiles.BasicProfile1_1" standard,

    // if not, it will throw compile time error.

    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

    // To restrict this service from getting added as a custom tool to toolbox

    [System.ComponentModel.ToolboxItem(false)]

    // To allow this Web Service to be called from script, using ASP.NET AJAX

    [System.Web.Script.Services.ScriptService]

    public class MyService : WebService

    {

        [WebMethod]

        public int SumOfNums(int First, int Second)

        {

            return First + Second;

        }

    }

}

[WebServiceBinding(ConformsTo= WsiProfiles.BasicProfile1_1)] to [WebServiceBinding(ConformsTo = WsiProfiles.None)].

Example

1. [WebMethod(MessageName = "twoparameter", Description = "addition of two integers")]  

2. public int add(int a, int b) {  

3.         return (a + b);  

4.     }  

5.     [WebMethod(MessageName = "threeparameter", Description = "addition of three integers")]  

6. public int add(int a, int b, int c) {  

7.         return (a + b + c);  

8.     }  

9.     [System.Web.Script.Services.ScriptService]  

To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.

[WebMethod]

public int Add(int a, int b)

{

  return(a + b);

}

[WebMethod]

public System.Single Subtract(System.Single A, System.Single B)

{

  return (A - B);

}

[WebMethod]

public System.Single Multiply(System.Single A, System.Single B)

{

  return A * B;

}

[WebMethod]

public System.Single Divide(System.Single A, System.Single B)

{

  if(B == 0) return -1;

  return Convert.ToSingle(A / B);

}


[WebServiceBinding(ConformsTo= WsiProfiles.BasicProfile1_1)] to [WebServiceBinding (ConformsTo = WsiProfiles.None)].

1. [WebMethod]  

2.         public int SumOfNums(int FirstNumber, int SecondNumber)  

3.         {  

4.             return FirstNumber + SecondNumber;  

5.         }  

6.   

7. //here we will use MessageName property to do the differentiation between both of the methods.  

8.         [WebMethod(MessageName = "SumOfFloats")]  

9.   

10.         public float SumOfNums(float FirstNumber, float SecondNumber)  

11.         {  

12.             return FirstNumber + SecondNumber;  

13.         }  

If we want to call the Web service from the Script, we will use ASP.NET, AJAX and uncomment the below line.

[System.Web.Script.Services.ScriptService]

[WebMethod]

This attribute is always used at the top of the method. We write this attribute in the Web Service. [WebMethod] attribute is used to define that the method used this attribute is exposed to the user/client access.

If we remove this attribute, in that case, the client will not be able to see the details of the method; hence they cannot implement it.

Proxy class or WSDL

WSDL (Web Service Development Language) document is used to generate the proxy class of the Web Service. WSDL document defines the Web Service. WSDL document contains the

1. All the method which is exposed by the Web service.

2. WSDL document contains the parameters and their types.

3. WSDL document contains the return type of the methods.

Visual Studio used the information to create the proxy class. The client application will call the proxy class method. The proxy class will serialize the parameters, prepare the SOAP request message and send it to the Web Service. Web Service executes this method and returns the SOAP return message to the Proxy. The proxy class will then deserialize the SOAP response message and give it to the Client application. There is not any need to serialize or deserialize the dot net CLR objects to and from the SOAP format. The proxy class has all the responsibility of the serialization and deserialization, which makes the life of the developer easy.

Now we will see the WebService1.asmx.cs window:

1. WebService.asmx.cs page includes the System.Web.Services, and it also consists of the four other namespaces which are included by the visual Studio in the Web Application.

2. "Service1" class is inherited from the "System.Web.Services.WebServices". After inheriting the class from the "System.Web.Services.WebService" we can access the built-in ASP.Net objects such as (Application, Session, User, Context, Server ). If there is not any need for the built-in objects of .NET, then there is also do not any need of the service class from "WebService".

3. "Service1" includes the "WebService" attribute. If we want to expose any class as a service, then there is also a need to include the attribute of the "WebService".

WebService attribute contains the different properties like:

FirstService.asmx

<%@ WebService language = "C#" class = "FirstService" %>


using System;

using System.Web.Services;

using System.Xml.Serialization;


[WebService(Namespace = "http://localhost/MyWebServices/")]

public class FirstService : WebService{

   [WebMethod]

   public int Add(int a, int b) {

      return a + b;

   }


   [WebMethod]

   public String SayHello() {

      return "Hello World";

   }

}


1. [HttpPut]  

2. public void Put(int id, [FromBody]string value)  

3. {  

4.   

5. }  

6. [HttpPost]  

7. public void Post([FromBody]string value)  

8. {  

9.   

10. }  

11. [HttpDelete]  

12. public void Delete(int id)  

13. {}  


The HTTP actions and their corresponding CRUD operations are:

GET (Read)

Retrieves the representation of the resource.


PUT(Update)

Update an existing resource.


POST (Create)

Create new resource.


DELETE (Delete)

Delete an existing resource.

Let's get to the first method in CarDetailsController.

1. GET IEnumerable

1. [HttpGet]  

2. public IEnumerable<CarsStock> GetAllcarDetails()  

3. {  

4.       CarsStock ST = new CarsStock();  

5.       CarsStock ST1 = new CarsStock();  

6.       List<CarsStock> li = new List<CarsStock>();  

7.   

8.       ST.CarName = "Maruti Waganor";  

9.       ST.CarPrice = "4 Lakh";  

10.       ST.CarModel = "VXI";  

11.       ST.CarColor = "Brown";  

12.   

13.       ST1.CarName = "Maruti Swift";  

14.       ST1.CarPrice = "5 Lakh";  

15.       ST1.CarModel = "VXI";  

16.       ST1.CarColor = "RED";  

17.   

18.       li.Add(ST);  

19.       li.Add(ST1);  

20.       return li;  

21.


This method is used to get a list of data.


In this method, I have used the Model CarsStock and created a list of CarsStock “List<CarsStock>“.


And returning it.


2. GET by id

1. public IEnumerable<CarsStock> Get(int id)  

2. {  

3.     CarsStock ST = new CarsStock();  

4.     CarsStock ST1 = new CarsStock();  

5.     List<CarsStock> li = new List<CarsStock>();  

6.     if (id == 1)  

7.     {  

8.         ST.CarName = "Maruti Waganor";  

9.         ST.CarPrice = "4 Lakh";  

10.         ST.CarModel = "VXI";  

11.         ST.CarColor = "Brown";  

12.         li.Add(ST);  

13.     }  

14.     else  

15.     {  

16.         ST1.CarName = "Maruti Swift";  

17.         ST1.CarPrice = "5 Lakh";  

18.         ST1.CarModel = "VXI";  

19.         ST1.CarColor = "RED";  

20.         li.Add(ST1);  

21.     }  

22.     return li;  

23.


3. Web API Controller Class template. Name the class TodoController.

4. Replace the generated code with the following:

5. using System.Collections.Generic;

6. using Microsoft.AspNetCore.Mvc;

7. using TodoApi.Models;

8.

9. namespace TodoApi.Controllers

10. {

11.     [Route("api/[controller]")]

12.     public class TodoController : Controller

13.     {

14.         public TodoController(ITodoRepository todoItems)

15.         {

16.             TodoItems = todoItems;

17.         }

18.         public ITodoRepository TodoItems { get; set; }

19.     }

20. }

21.

In this GET method, you can retrieve records for the database by passing an id.


22. POST

1. [HttpPost]  

2. public void PostCar([FromBody] CarsStock cs)  

3. {  

4.   

5.


In this POST method, you can post data (CREATE) to the database. In this, I am using the Carstock model to post the data.


23. PUT

1. [HttpPut]  

2. public void Putcar(int id, [FromBody]CarsStock cs)   

3. {  

4.                

5.


In this PUT method you can UPDATE the data (UPDATE) to the database. I am using the Carstock model to update the data.


24. DELETE

1. [HttpDelete]  

2. public void Deletecar(int id)  

3. {  

4.   

5.


In this DELETE method you can delete data (DELETE) from the database. I am using an id to delete the data.

 

Here is a snapshot of all the methods and models after adding the attributes to it.

 

 

using DemoWebAPI.Models;

using DemoWebAPI.Repositories;

using System;

using System.Collections.Generic;

using System.Linq;

using System.Net;

using System.Net.Http;

using System.Web.Http;

 

namespace DemoWebAPI.Controllers

{

public class ValuesController : ApiController

{

IComments Icom;

public ValuesController()

{

Icom = new Comments();

}

// GET api/values

publicIEnumerable<Comment> GetAlldata()

{

return Icom.ListofComment();    


}

// GET api/values/5

public string Get(int id)

{

return"value";

}

// POST api/values

public void Post([FromBody]Comment Comment)

{

Icom.InsertComment(Comment); 


}

// PUT api/values/5

public void Put(int id, [FromBody]string value)

{

}

// DELETE api/values/5

public void Delete(int id)

{

}

}

}


<script type="text/javascript">

function PostData() {

debugger;

var Commenta = { "UserComment": $("#UserComment").val() };

$.ajax({

type: "POST",

data: Commenta,

url: '@Url.Action("Post", "api/values")',

success: function (data) {

GetData();

},

});

}

</script>


Types of Design Patterns

Gang of Four categorized the Design Pattern into three main categories based on the three problem areas (object creation and initialization, Structural Changes of class and interfaces, and the relationship between classes, Communication between objects) of software architecture. They are as follows.

1. Creational Design Pattern (object creation and initialization)

2. Structural Design Pattern (Structural Changes of class and interfaces, and the relationship between classes)

3. Behavioral Design Pattern (Communication Between Objects)

Creational Design Patterns:

The Creational Design Pattern deals with Object Creation and Initialization. The Creational Design Pattern gives the programmer more flexibility in deciding which objects need to be created for a given case. For example, if we have a huge project, a huge project means we have a lot of classes, a lot of classes mean we are dealing with a lot of objects. So we need to create different objects (like new Customer(), new product(), new invoice(), etc.) based on some conditions. If the object creations logic based on some condition is implemented in the client code, then it leads to lots of complicated logic in the client code. That means if the object creations and initialization logic are not centralized then it leads to a very complicated code. The Creational Design Pattern helps us to centralized the object creation logic and depending upon the condition, it will create and initialize the appropriate object and returns that object.

Examples of Creational design patterns are Singleton, Factory, Builder, Prototype, Fluent Interface, Factory Method, and Abstract Factory.

Structural Design Patterns: 

The Structural Design Pattern is basically used to manage the structure of classes and interface as well as manage the relationship between the classes. For example, if we have a Customer and Product class and the Product class is used inside the Customer class making one to many relationships. Tomorrow, as the project proceeds, now we want to keep away the product class from the Customer class as we want to use the Product and Customer class independently. This is a structural change and we don’t want this structural change to affect our project. This is where the Structural Design Pattern helps us.

Examples of Structural Design Patterns category: Adapter, Facade, Decorator, Composite, Proxy, Flyweight, and Bridge Design Pattern. 

Behavioral Design Patterns: 

The Behavioral Design Patterns deal with the communication between Classes and objects. That means if you want to change the behavior of a class and again you want it to affect other classes of the project as well. For example, you have an Invoice class that currently applying taxes as 18%. Tomorrow if you have to add another extra tax. That means you are changing the behavior of a class. To solve such types of Behavioral issues Behavioral Design Pattern comes into the picture.

Examples of Behavioral design patterns: Chain of Responsibility, Command,  Observer, Iterator, State, Template Method, Visitor, Strategy, Mediator, Memento, and Interpreter Design Pattern.

How easily you are going to understand the design patterns that basically depends on how strong you are in object-oriented programming concepts. So, to take full advantage of Design Patterns in C# tutorials, it is very important for you to have at least the basic knowledge of the following object-oriented programming concepts.

1. Abstraction

2. Inheritance

3. Polymorphism

4. Encapsulation

5. Interfaces

6. Classes

7. Abstract classes

Along with GoF 23 Design Patterns, we are also going to discuss the following dot net design patterns which are used frequently in most real-time applications.