Tuesday, October 6, 2020

Dependency Injection and types of dependency injection.

Understanding of the Dependency Injection and different types of Dependency Injection in .Net.



Hello friend today I’m trying to explain what is Dependency Injection and type of Dependency Injection. Most of the interviewer asked this question during the interview. Don’t worry today I’m explaining to you in the easiest and simplest way.

Definition of Dependency Injection.

Dependency Injection (DI) is a technique that provides a dependent class object. This technique works outside of the Client class and provides the object of the Service class.

Dependency Injection is a relation of mainly 3 types of classes.

  1. Client Class: This class is a dependent class. Which is depends on the service class.
  2. Service Class: Service class is a dependency class that provides service to the client class.
  3. Injector Class: The injector class injects the service class and provides an object to the client class.

So we can say the client class uses service class, where the injector class which helps to client class to create the object of the service class. This is the simple mechanism of DI.

There are 3 different types of Dependency Injection.
  1. Constructor Injection: Inject dependency at the constructor level.
  2. Property Injection: Inject dependency using setter.
  3. Method Injection: Inject dependency using method.

1) Example of Constructor Injection

public interface IService

    {

        string Mthod();

    }

    public class Service1 : IService

    {

        public string Mthod()

        {

            return "Mthod of service 1.";

        }

    }

    public class Service2 : IService

    {

        public string Mthod()

        {

            return "Mthod of service 2.";

        }

    }

    public class Client

    {

        private readonly IService _iService;

        public Client(IService iService)

        {

            this._iService = iService;

        }

        public string GetMethod()

        {

            return _iService.Mthod();

        }

    }

    class Program

    {

        static void Main(string[] args)

        {

            //Service 1

            Service1 s1 = new Service1();

            Client c1 = new Client(s1);

            //Service 2

            Service2 s2 = new Service2();

            c1 = new Client(s2);

        }

    }

In the above example, you can see Client class is not responsible to create a service class object. We can’t use the new keyword to create an object of class Service1 and Service2 into the Client class. Instead of that, we are passing the object parameter of Service1 and Service2 class when calling client class.

2) Example of Property Injection

public interface IService

    {

        string Mthod();

    }

    public class Service1 : IService

    {

        public string Mthod()

        {

            return "Mthod of service 1.";

        }

    }

    public class Service2 : IService

    {

        public string Mthod()

        {

            return "Mthod of service 2.";

        }

    }

    public class Client

    {

        private IService _iService;

        public IService Service

        {

            set

            {

                this._iService = value;

            }

        }

        public string GetMethod()

        {

            return _iService.Mthod();

        }

    }

    class Program

    {

        static void Main(string[] args)

        {

            //Service 1

            Service1 s1 = new Service1();

            Client c1 = new Client();

            c1.Service = s1;

            //Service 2

            Service2 s2 = new Service2();

            c1.Service = s2;

        }

    }

In the above example, you can see that we are injecting the Service class object using setter property when calling client class. We set the Service1 and Service2 class object during the calling of Client class so client class is not responsible to create the object of Service class. So this way we achieve the property injection.

3) Example of Method Injection

public interface IService

    {

        string Mthod();

    }

    public class Service1 : IService

    {

        public string Mthod()

        {

            return "Mthod of service 1.";

        }

    }

    public class Service2 : IService

    {

        public string Mthod()

        {

            return "Mthod of service 2.";

        }

    }

    public class Client

    {

        private IService _iService;

        public void Serve(IService iService)

        {

            _iService = iService;

        }

        public string GetMethod()

        {

            return _iService.Mthod();

        }

    }

    class Program

    {

        static void Main(string[] args)

        {

            //Service 1

            Service1 s1 = new Service1();

            Client c1 = new Client();

            c1.Serve(s1);

            var result = c1.GetMethod();

            //Service 2

            Service2 s2 = new Service2();

            c1.Serve(s2);

            var result2 = c1.GetMethod();

        }

    }

 

As you have seen in the above example here we are injecting the Service class object through the Serve method of Client class. So this way we achieve the Method Injection.

Advantage of Dependency Injection

  1. Achieve loose coupling.

  2. Code reusability increases.

  3. Easy to unit testing

  4. Improve code management due to separation of class and less responsibility.

List of some readymade containers available in the market for achieving Dependency Injection (DI). Some of them are open source and commercial.

  1. Unity
  2. Autofac
  3. Ninject
  4. Simple Injector
  5. DryIoc
  6. Light Inject
  7. Castle Windsor
  8. StuructureMap

I hope you have liked this article for the understanding of Dependency Injection and types of dependency injection.

Please feel free to comment if you have found anything incorrect, or you want to share more information about the topic discussed above.

Please Share this article so it will help to another one because “Helping millions grow better”.

Follow my blog https://mydotnetguru.blogspot.com to read the more interesting technical articles.

Next Recommended Article:

  1. Optimized top 2 ways to find 2nd or Nth highest salary in SQL Server.
  2. Different ways to remove or delete duplicate rows from a SQL Table.
  3. When to use abstract class and interface in C#?
  4. Async and Await in C#
  5. Top 100+ popular ASP.Net MVC Interview Questions and Answers.
  6. Top 100+ popular SQL Interview Questions and Answers Blog.
  7. Top 100+ popular C# Interview Questions and Answers.
  8. @Input, @Output decorator and EventEmitter class in Angular.


Friday, October 2, 2020

@Input, @Output decorator and EventEmitter class in Angular.

Understanding of the @Input and @Output decorator with EventEmitter class in Angular.



Today we will understand of what the exact use of @Input and @Output decorator in angular. Also, explore how to use with examples so you can get more idea about this decorator.

Why @Input and @Output Decorator in Angular?

In the simple word, we can say @Input and @Output both are used to share or exchange data between components. We send or receive data between child and parent components. 

How to use @Input and @Output Decorator in Angular?

@Input is used to receive data and @output used to send data. 

1) Example of @Input Decorator in Angular.

@Input is used when any parent component wants to send the data to the child component. Input is help to child component to receive data sent by the parent component. How it is possible to lets’ start with one simple example.

Code Example of @Input.

Please look into the below code sample carefully for sending data parent to child component.

1) Create one parent component let’s say MasterPageComponent. In that component declare one variable called myInputMessage and set the value as “Parent” for sending this data to the child component.


2) MasterPageComponent .html page is looking as below.


3)  <app-footer> which is selector of child component and send data thru [MyInput] variable.

4) [MyInput] carry data to the child component.

5) Let’s create a child component with the name is MainFooterComponent.


6) Please look carefully highlighted line, I have declared @Input decorator with the same name which I have used in master as [MyInput].

7) @Input help to read value sent by master component or we can say receive those value which was sent by master component.

8) In the above example myInput variable is assigned with value “Parent” which is sent by the parent component.

9) Please don’t try to access @Input value within the constructor because It is always found within ngOnOnit() of angular.

Now let's understand the @Output and EventEmitter.

2) Example of @Output Decorator in Angular.

@Output is used when any child component wants to send the data to the parent component. The output is helping to parent component to receive data sent by the child component. @Output binds a property of the type of angular EventEmitter class. How it is possible to lets’ start with one simple example.

Why EventEmitter?

First we understand Emit meaning. Emit means to produce something. Now we relate this terminology with angular. EventEmitter is used with @Output directives to emit custom events synchronously or asynchronously and register handlers for those events by subscribing to an instance.

Code example of @Output in Angular.

1) For sending a message from the child to the parent we create one button in the child component which sends a message to the parent component.

2) Open the HTML page of the child component called  MainFooterComponent. Write code for the click event of the button, now the view is looking as below.


3) Now register the sendParent() click event on the component side as below.


4) In the child component first register the @Output variable with the help of EventEmitter.

5) After that we register our click event sendParent() which is emit the output message which we have set in outputMessage variable.

6) Now mention this myOutput variable in HTML of the master page component as below.


7) Here we declare the myOutput variable of child component with getChild($event) method.

8) Now getChild($event) method register as below in the parent component for leasing the data sent by the child component.


9) Here the getChild(data) method read data that is sent by the child component.

So this way you can send and receive data using @Input, @output decorator with the help of EventEmitter.

I hope you have liked this article for the understanding of @Input and @Output decorator with EventEmitter class in Angular.

Please feel free to comment if you have found anything incorrect, or you want to share more information about the topic discussed above.

Please Share this article so it will help to another one because “Helping millions grow better”.

Follow my blog https://mydotnetguru.blogspot.com to read the more interesting technical articles.

Next Recommended Article:

  1. Optimized top 2 ways to find 2nd or Nth highest salary in SQL Server.
  2. Different ways to remove or delete duplicate rows from a SQL Table.
  3. When to use abstract class and interface in C#?
  4. Async and Await in C#
  5. Top 100+ popular ASP.Net MVC Interview Questions and Answers.
  6. Top 100+ popular SQL Interview Questions and Answers Blog.
  7. Top 100+ popular C# Interview Questions and Answers.
  8. Dependency Injection and types of dependency injection.


Monday, September 28, 2020

Remove duplicate rows from the SQL table.

Different ways to remove or delete duplicate rows from a SQL Table.



Today we will see different solutions for remove or delete duplicates row from our tables. This is the most popular interview question. Don’t worry I will saw you all the possible ways to remove duplicate rows from the table.


[Top 100+ popular SQL Interview Questions and Answers Blog.]


We create the Employee table and insert some dummy data.

CREATE TABLE [dbo].[Employee]

(

       EmpId INT IDENTITY(1,1) PRIMARY KEY,

       EmpName VARCHAR(20),

       ManagerId INT NULL,

       Salary MONEY,

       DeptId INT

);

GO

INSERT INTO [dbo].[Employee] (EmpName,ManagerId,Salary,DeptId) VALUES ('Rajesh',NULL, 60000,1);

INSERT INTO [dbo].[Employee] (EmpName,ManagerId,Salary,DeptId) VALUES ('Rohan',1, 50000,1);

INSERT INTO [dbo].[Employee] (EmpName,ManagerId,Salary,DeptId) VALUES ('Janak',NULL, 70000,2);

INSERT INTO [dbo].[Employee] (EmpName,ManagerId,Salary,DeptId) VALUES ('Mohan',3, 50000,2);

INSERT INTO [dbo].[Employee] (EmpName,ManagerId,Salary,DeptId) VALUES ('Rahul',NULL, 40000,3);

INSERT INTO [dbo].[Employee] (EmpName,ManagerId,Salary,DeptId) VALUES ('Ronak',NULL, 40000,4);

INSERT INTO [dbo].[Employee] (EmpName,ManagerId,Salary,DeptId) VALUES ('Ronak',NULL, 40000,4);

INSERT INTO [dbo].[Employee] (EmpName,ManagerId,Salary,DeptId) VALUES ('Rahul',NULL, 40000,3);


Hope you can successfully able to create both the above tables in your database now we go for example.

The actual employee data in the employee table is as below.

SELECT * FROM Employee



1) Remove duplicate rows using Group By and Having Clause query.

SELECT

    EmpName,

    Salary,

    COUNT(*) AS CNT,

    MAX(EmpId) AS EmpId

FROM

       Employee

GROUP BY

       EmpName,

       Salary

HAVING COUNT(*) > 1;


The above query returns the duplicate rows based on the employee name and salary from the employee table and the result is like below.



Now, we delete above duplicate rows from our table using Group By and Having Clause.

DELETE FROM

       Employee

WHERE

       EmpId NOT IN 

(

SELECT

       MAX(EmpId) AS EmpId

FROM

       Employee

GROUP BY

       EmpName,

       Salary

);

The above query successfully deletes duplicate rows from our table and now our result is as below.

SELECT * FROM Employee


2) Remove duplicate rows using CTE (Common Table Expression) query.

;WITH CTE AS(

 SELECT *,ROW_NUMBER() OVER (PARTITION BY EmpName,Salary ORDER BY EmpId) AS DuplicateRowCount FROM Employee

)

SELECT * FROM CTE

The result of the above query is as below.



Now, we delete duplicate rows based on the column DuplicateRowCount using the below query.

;WITH CTE AS(

 SELECT *,ROW_NUMBER() OVER (PARTITION BY EmpName,Salary ORDER BY EmpId) AS DuplicateRowCount FROM Employee

)

DELETE FROM CTE WHERE DuplicateRowCount>1


The above query successfully deletes duplicate rows from the employee table and now our result is as below.

SELECT * FROM Employee 



I hope you have liked this article for the understanding of Remove duplicate rows from the SQL table.

Please feel free to comment if you have found anything incorrect, or you want to share more information about the topic discussed above.

Please Share this article so it will help to another one because “Helping millions grow better”.

Follow my blog https://mydotnetguru.blogspot.com to read the more interesting technical articles.

Next Recommended Article:

  1. Optimized top 2 ways to find 2nd or Nth highest salary in SQL Server.
  2. When to use abstract class and interface in C#?
  3. Async and Await in C#
  4. Top 100+ popular ASP.Net MVC Interview Questions and Answers.
  5. Top 100+ popular SQL Interview Questions and Answers Blog.
  6. Top 100+ popular C# Interview Questions and Answers.
  7. @Input, @Output decorator and EventEmitter class in Angular.
  8. Dependency Injection and types of dependency injection.