Q11. What is singleton design pattern?
Q12. What is Dependency Injection in C#. Why we need this?
Q13. Explain Constructor Dependency Injection.
Q14. Explain Property Dependency Injection.
Q15. Explain Method Dependency Injection.
--------------------------------------------------------------------------------------------------------------------------
Q11. What is singleton design pattern?
Answer:
Practical example of singleton class
1. When you use Logger, you use Singleton pattern for the Logger class. In case it is not Singleton, every client will have its own Logger object and there will be concurrent access on the Logger instance in Multithreaded environment, and multiple clients will create/write to the Log file concurrently, this leads to data corruption.
Q12. What is Dependency Injection in C#. Why we need this?
Q13. Explain Constructor Dependency Injection.
Q14. Explain Property Dependency Injection.
Q15. Explain Method Dependency Injection.
--------------------------------------------------------------------------------------------------------------------------
Q11. What is singleton design pattern?
Answer:
Features of singleton design pattern are
- It Ensure that singleton class has only one instance, and provide a global point of access to it by its public property.
- Singleton class has private constructor to prevent it getting initiated by other classes.
- Singleton class has private static variable to hold single crated instance.
1. When you use Logger, you use Singleton pattern for the Logger class. In case it is not Singleton, every client will have its own Logger object and there will be concurrent access on the Logger instance in Multithreaded environment, and multiple clients will create/write to the Log file concurrently, this leads to data corruption.
2. second real life example of singleton pattern could be load balancer.
Not thread safe implementation is below. it means if same code is running in multiple thread then it will create two more instance of singleton class.
Singleton Class
public class Singleton
{
// Use to hold the value of single instance but it will be accessible via public property
private static Singleton _instance;
// protect it from getting instantiated and inheited by other class
private Singleton()
{
Console.WriteLine("Instance created");
}
public void Log(string message)
{
Console.WriteLine(message);
}
// global point of access to single created instance.
public static Singleton Instance
{
get
{
// Ensures only single instance can be created
if (_instance == null)
{
_instance = new Singleton();
}
return _instance;
}
}
}
Main class
//Singleton s = new Singleton(); // error
var s = Singleton.Instance;
s.Log("super day");
var a = Singleton.Instance;
a.Log("next day");
output
Instance created
super day
next day
Q12. What is Dependency Injection in C#. Why we need this?
Answer:
Dependency Injection is a software design pattern. It allows us to create a loosely coupled code. This also make code more maintainable and testable.
- Dependency Injection reduces the hard-coded dependencies among your classes by injecting those dependencies at run time instead of design time technically.
We have 3 types of Dependency injection pattern
Q13. Explain Constructor Dependency Injection.- Constructor Injection
- Property Injection
- Method Injection
Why we need this?
1. It make code more testable.
2. Team-ability - Two persons can work independently instead of waiting for one to complete the code.
3. Code maintenance -
--------------------------------------------------------------------------------------------------------------------------
Answer:
Implementing without DI.
create 3 classes Employee.cs, EmployeeDAL.cs and EmployeeBL.cs
Employee.cs
namespace DependencyInjectionExample
{
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public string Department { get; set; }
}
}
EmployeeDAL.cs- namespace DependencyInjectionExample
- {
- public class EmployeeDAL
- {
- public List<Employee> SelectAllEmployees()
- {
- List<Employee> ListEmployees = new List<Employee>();
- //Get the Employees from the Database
- //for now we are hard coded the employees
- ListEmployees.Add(new Employee() { ID = 1, Name = "Pranaya", Department = "IT" });
- ListEmployees.Add(new Employee() { ID = 2, Name = "Kumar", Department = "HR" });
- ListEmployees.Add(new Employee() { ID = 3, Name = "Rout", Department = "Payroll" });
- return ListEmployees;
- }
- }
- }
- namespace DependencyInjectionExample
- {
- public class EmployeeBL
- {
- public EmployeeDAL employeeDAL;
- public List<Employee> GetAllEmployees()
- {
- employeeDAL = new EmployeeDAL();
- return employeeDAL.SelectAllEmployees();
- }
- }
- }
use the constructor injection to make these classes loosely coupled.
Modify the EmployeeDAL.cs
- namespace DependencyInjectionExample
- {
- public interface IEmployeeDAL
- {
- List<Employee> SelectAllEmployees();
- }
- public class EmployeeDAL : IEmployeeDAL
- {
- public List<Employee> SelectAllEmployees()
- {
- List<Employee> ListEmployees = new List<Employee>();
- //Get the Employees from the Database
- //for now we are hard coded the employees
- ListEmployees.Add(new Employee() { ID = 1, Name = "Pranaya", Department = "IT" });
- ListEmployees.Add(new Employee() { ID = 2, Name = "Kumar", Department = "HR" });
- ListEmployees.Add(new Employee() { ID = 3, Name = "Rout", Department = "Payroll" });
- return ListEmployees;
- }
- }
- }
Modify the EmployeeBL.cs file
- namespace DependencyInjectionExample
- {
- public class EmployeeBL
- {
- public IEmployeeDAL employeeDAL;
- public EmployeeBL(IEmployeeDAL employeeDAL)
- {
- this.employeeDAL = employeeDAL;
- }
- public List<Employee> GetAllEmployees()
- {
- Return employeeDAL.SelectAllEmployees();
- }
- }
- }
Main method of Program class
- namespace DependencyInjectionExample
- {
- class Program
- {
- static void Main(string[] args)
- {
- EmployeeBL employeeBL = new EmployeeBL(new EmployeeDAL());
- List<Employee> ListEmployee = employeeBL.GetAllEmployees();
- foreach(Employee emp in ListEmployee)
- {
- Console.WriteLine("ID = {0}, Name = {1}, Department = {2}", emp.ID, emp.Name, emp.Department);
- }
- Console.ReadKey();
- }
- }
- }
--------------------------------------------------------------------------------------------------------------------------
Q14. Explain Property Dependency Injection.
Answer:
This is also known as 'Setter' property injection.
EmployeeBL.cs for property DI
- namespace DependencyInjectionExample
- {
- public class EmployeeBL
- {
- private IEmployeeDAL employeeDAL;
- public IEmployeeDAL employeeDataObject
- {
- set
- {
- this.employeeDAL = value;
- }
- get
- {
- if (employeeDataObject == null)
- {
- throw new Exception("Employee is not initialized");
- }
- else
- {
- return employeeDAL;
- }
- }
- }
- public List<Employee> GetAllEmployees()
- {
- return employeeDAL.SelectAllEmployees();
- }
- }
- }
- namespace DependencyInjectionExample
- {
- class Program
- {
- static void Main(string[] args)
- {
- EmployeeBL employeeBL = new EmployeeBL();
- employeeBL.employeeDataObject = new EmployeeDAL();
- List<Employee> ListEmployee = employeeBL.GetAllEmployees();
- foreach(Employee emp in ListEmployee)
- {
- Console.WriteLine("ID = {0}, Name = {1}, Department = {2}", emp.ID, emp.Name, emp.Department);
- }
- Console.ReadKey();
- }
- }
- }
Q15. Explain Method Dependency Injection.
Answer:
In Method Dependency Injection, we need to supply the dependency object through a public method of the client class. Let us see an example to understand how we can implement the Method dependency injection in C#.
Modify the EmployeeBL class as shown below
- namespace DependencyInjectionExample
- {
- public class EmployeeBL
- {
- public IEmployeeDAL employeeDAL;
- public List<Employee> GetAllEmployees(IEmployeeDAL _employeeDAL)
- {
- employeeDAL = _employeeDAL;
- return employeeDAL.SelectAllEmployees();
- }
- }
- }
Modify the Main method of Program class as shown below
- namespace DependencyInjectionExample
- {
- class Program
- {
- static void Main(string[] args)
- {
- //Create object of EmployeeBL class
- EmployeeBL employeeBL = new EmployeeBL();
- //Call to GetAllEmployees method with proper object.
- List<Employee> ListEmployee = employeeBL.GetAllEmployees(new EmployeeDAL());
- foreach (Employee emp in ListEmployee)
- {
- Console.WriteLine("ID = {0}, Name = {1}, Department = {2}", emp.ID, emp.Name, emp.Department);
- }
- Console.ReadKey();
- }
- }
- }
--------------------------------------------------------------------------------------------------------------------------
No comments:
Post a Comment