Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions EmployeeManagementSystem/EmployeeManagementSystem/Database.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace EmployeeManagementSystem
{
public class Database : IDatabase
{
public void Save(Employee employee)
{
Console.WriteLine($"Saved employee: {employee.Name}.");
}
}
}
14 changes: 14 additions & 0 deletions EmployeeManagementSystem/EmployeeManagementSystem/Employee.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace EmployeeManagementSystem
{
public class Employee
{
public string Name { get; private set; }
public string Address { get; private set; }

public Employee(string name, string address)
{
Name = name;
Address = address;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace EmployeeManagementSystem
{
public class EmployeeService
{
private readonly IDatabase _database;

public EmployeeService(IDatabase database)
{
_database = database;
}

public void SaveEmployee(Employee employee)
{
_database.Save(employee);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace EmployeeManagementSystem
{
public interface IDatabase
{
void Save(Employee employee);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace EmployeeManagementSystem
{
public interface IEmployeeService
{
void SaveEmployee(Employee employee);
}
}
16 changes: 16 additions & 0 deletions EmployeeManagementSystem/EmployeeManagementSystem/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace EmployeeManagementSystem
{
internal class Program
{
static void Main(string[] args)
{
IDatabase database = new Database();

IEmployeeService employeeService = new EmployeeService(database);

Employee employee = new Employee("Mukul Palol", "Jaipur");

employeeService.SaveEmployee(employee);
}
}
}
40 changes: 40 additions & 0 deletions EmployeeManagementSystem/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Assignemnt 6: Dependency Inversion Principle

### Objective:

```java
//Apply the Dependency Inversion Principle (DIP) by refactoring a set of classes
// Note: Also mention why this code violating DIP

public class EmployeeService {
private Database database = new Database();

public void saveEmployee(Employee employee) {
database.save(employee);
}
}

public class Database {
public void save(Employee employee) {
// Code to save employee to the database
}
}

public class Employee {
private String name;
private String address;

public Employee(String name, String address) {
this.name = name;
this.address = address;
}

// Getters and setters
}
```

### Why this code is violating DIP:
- The **EmployeeService** class directly depends on the **Database** class.
- **EmployeeService** is tightly coupled with the implementation of **Database**.
- It is difficult to change the database implementation without modifying the EmployeeService class.
- There is no abstraction layer between **EmployeeService** and **Database**. DIP states that high-level modules (**EmployeeService**) should not depend on low-level modules (**Database**), instead, both should depend on abstractions.