File tree Expand file tree Collapse file tree
DownloadableCodeProjects/LP2_implement_interfaces/Interfaces_M3 Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -3,8 +3,8 @@ public class Application
33 private readonly ILogger _logger ;
44 private readonly IDataAccess _dataAccess ;
55
6- // The constructor now accepts ILogger and IDataAccess interfaces,
7- // enabling dependency injection and decoupling the class from specific implementations .
6+ // TASK 6: Implement ILogger and IDataAccess interfaces and
7+ // refactor this constructor to accept them as parameters .
88 public Application ( ILogger logger , IDataAccess dataAccess )
99 {
1010 _logger = logger ;
Original file line number Diff line number Diff line change 11using System ;
22
3- // This class implements the ILogger interface and is responsible for logging messages to the console.
3+ // TASK 5: Refactor the code to use an ILogger interface.
4+ // This class implements the ILogger interface and is responsible for
5+ // logging messages to the console.
46public class ConsoleLogger : ILogger
57{
68 // Logs a message to the console.
Original file line number Diff line number Diff line change 1+ // TASK 3: Refactor the code to use an IDataAccess interface.
12// This class implements the IDataAccess interface and is responsible for connecting to a database and retrieving data.
23public class DatabaseAccess : IDataAccess
34{
Original file line number Diff line number Diff line change 1+ // TASK 2: Create an interface ILogger with a method Log(string message).
12// Interface for data access operations.
23public interface IDataAccess
34{
Original file line number Diff line number Diff line change 1+ // TASK 4: Create an interface ILogger for logging messages
12// Interface for logging messages.
23public interface ILogger
34{
Original file line number Diff line number Diff line change 1- class Program
2- {
3- static void Main ( string [ ] args )
4- {
5- // Create instances of ConsoleLogger and DatabaseAccess .
6- var logger = new ConsoleLogger ( ) ;
7- var dataAccess = new DatabaseAccess ( ) ;
1+ // TASK 7: Refactor Program to use dependency injection.
2+ // Refactor the Program class to create instances of ILogger and IDataAccess
3+ // and pass them as dependencies to the Application class constructor.
4+ // This change decouples the Program class from specific implementations
5+ // and aligns it with the refactored Application class .
6+ var logger = new ConsoleLogger ( ) ;
7+ var dataAccess = new DatabaseAccess ( ) ;
88
9- // Inject the dependencies into the Application class.
10- var app = new Application ( logger , dataAccess ) ;
11- app . Run ( ) ;
12- }
13- }
9+ // Inject the dependencies into the Application class.
10+ var app = new Application ( logger , dataAccess ) ;
11+ app . Run ( ) ;
Original file line number Diff line number Diff line change 1+ <Project Sdk =" Microsoft.NET.Sdk" >
2+
3+ <PropertyGroup >
4+ <OutputType >Exe</OutputType >
5+ <TargetFramework >net9.0</TargetFramework >
6+ <ImplicitUsings >enable</ImplicitUsings >
7+ <Nullable >enable</Nullable >
8+ </PropertyGroup >
9+
10+ </Project >
Original file line number Diff line number Diff line change 1- // This class represents the main application logic.
2- // It is currently tightly coupled to the ConsoleLogger and DatabaseAccess classes.
3- // Your task is to refactor this class to use interfaces (ILogger and IDataAccess)
4- // instead of directly depending on concrete implementations.
1+
52public class Application
63{
74 private readonly ConsoleLogger _logger ;
85 private readonly DatabaseAccess _dataAccess ;
96
10- // The constructor directly instantiates the ConsoleLogger and DatabaseAccess classes,
11- // creating tight coupling. This will be refactored in the exercise.
7+ // TASK 6: Implement ILogger and IDataAccess interfaces and
8+ // refactor this constructor to accept them as parameters.
9+
10+ // *The Application class represents the main application logic, and
11+ // *is tightly coupled to ConsoleLogger and DatabaseAccess.
12+ // *Currently, the constructor directly instantiates the
13+ // *ConsoleLogger and DatabaseAccess classes, creating tight coupling.
1214 public Application ( )
1315 {
1416 _logger = new ConsoleLogger ( ) ;
Original file line number Diff line number Diff line change 1- using System ;
1+ // TASK 5: Refactor the code to use an ILogger interface.
2+ // This class implements the ILogger interface and is responsible for
3+ // logging messages to the console.
24
3- // This class is responsible for logging messages to the console.
4- // Currently, it is tightly coupled to the Application class.
5- // In this exercise, you will refactor the code to use an ILogger interface.
5+ // * This class is responsible for logging messages to the console.
6+ // * Currently, it is tightly coupled to the Application class.
7+ // * In this exercise, you will refactor the code to use an ILogger interface.
68public class ConsoleLogger
79{
810 // Logs a message to the console.
Original file line number Diff line number Diff line change 1+ // TASK 3: Refactor the code to use an IDataAccess interface.
2+
13// This class is responsible for connecting to a database and retrieving data.
24// It is currently tightly coupled to the Application class.
3- // In this exercise, you will refactor the code to use an IDataAccess interface.
45public class DatabaseAccess
56{
67 // Simulates connecting to a database.
You can’t perform that action at this time.
0 commit comments