From 14bec4c3d99055de160fd19e0df943dc810cfdb3 Mon Sep 17 00:00:00 2001 From: Poyam Sharma Date: Tue, 21 May 2024 14:58:59 +0530 Subject: [PATCH] Added solution to DIP problem statement --- Database.cs | 9 +++++++++ Employee.cs | 14 ++++++++++++++ EmployeeService.cs | 15 +++++++++++++++ IDatabase.cs | 7 +++++++ README.md | 8 ++++++++ 5 files changed, 53 insertions(+) create mode 100644 Database.cs create mode 100644 Employee.cs create mode 100644 EmployeeService.cs create mode 100644 IDatabase.cs diff --git a/Database.cs b/Database.cs new file mode 100644 index 0000000..97a47e4 --- /dev/null +++ b/Database.cs @@ -0,0 +1,9 @@ +namespace EmployeeSystem { + + // This way Low-level module(database) implements the abstraction + public class Database : IDatabase { + public void save(Employee employee) { + // Code to save employee to the database + } + } +} diff --git a/Employee.cs b/Employee.cs new file mode 100644 index 0000000..3e54d5e --- /dev/null +++ b/Employee.cs @@ -0,0 +1,14 @@ +namespace EmployeeSystem { + + public class Employee { + private String name; + private String address; + + public Employee(String name, String address) { + this.name = name; + this.address = address; + } + + // Getters and setters + } +} diff --git a/EmployeeService.cs b/EmployeeService.cs new file mode 100644 index 0000000..a5f7c25 --- /dev/null +++ b/EmployeeService.cs @@ -0,0 +1,15 @@ +namespace EmployeeSystem { + + // This way High-level module(EmployeeService) depends on abstraction + public class EmployeeService { + private IDatabase iDatabase; + + public EmployeeService(IDatabase employeeRepository) { + this.iDatabase = employeeRepository; + } + + public void saveEmployee(Employee employee) { + iDatabase.save(employee); + } + } +} diff --git a/IDatabase.cs b/IDatabase.cs new file mode 100644 index 0000000..a56c8e1 --- /dev/null +++ b/IDatabase.cs @@ -0,0 +1,7 @@ +namespace EmployeeSystem { + public interface IDatabase { + void save(Employee employee); + } +} + +//By creating an interface we can also implement it into other database sources such as Files etc in future without changing the existing code. This will also help in maintaining OCP. \ No newline at end of file diff --git a/README.md b/README.md index 7a63948..b40d265 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,10 @@ # Learn-and-Code This Repository includes all the assignments for Learn And Code - 2023 + +In the provided problem statement of Assignment 7, the code is violating DIP beacause of the following reasons: + +1. As the EmployeeService class is directly dependent on the implementation of the Database class, but According to DIP, high-level modules (like EmployeeService) should not depend on low-level modules (like Database). They should depend on abstraction. + +2. We also learned that according to DIP the abstractions should not depend upon details, details should depend upon abstractions. Here, EmployeeService directly depends on the Database class, which is a concrete implementation and thus it makes hard to replace Database with a different storage mechanism for eg. Files. + +3. Here, it is also hard to maintain the code as if the implementation of Database changes, EmployeeService also might need to change too.