Skip to content

Commit b8a9684

Browse files
authored
Merge pull request #10 from MicrosoftLearning/lp2-m3-eric
Lp2 m3 starter solution code update
2 parents db7c344 + fce9da3 commit b8a9684

14 files changed

Lines changed: 67 additions & 38 deletions

File tree

DownloadableCodeProjects/LP2_implement_interfaces/Interfaces_M3/Solution/Application.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff 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;

DownloadableCodeProjects/LP2_implement_interfaces/Interfaces_M3/Solution/ConsoleLogger.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using 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.
46
public class ConsoleLogger : ILogger
57
{
68
// Logs a message to the console.

DownloadableCodeProjects/LP2_implement_interfaces/Interfaces_M3/Solution/DatabaseAccess.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
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.
23
public class DatabaseAccess : IDataAccess
34
{

DownloadableCodeProjects/LP2_implement_interfaces/Interfaces_M3/Solution/IDataAccess.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// TASK 2: Create an interface ILogger with a method Log(string message).
12
// Interface for data access operations.
23
public interface IDataAccess
34
{

DownloadableCodeProjects/LP2_implement_interfaces/Interfaces_M3/Solution/ILogger.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// TASK 4: Create an interface ILogger for logging messages
12
// Interface for logging messages.
23
public interface ILogger
34
{
Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
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();
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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>

DownloadableCodeProjects/LP2_implement_interfaces/Interfaces_M3/Starter/Application.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
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+
52
public 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();

DownloadableCodeProjects/LP2_implement_interfaces/Interfaces_M3/Starter/ConsoleLogger.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
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.
68
public class ConsoleLogger
79
{
810
// Logs a message to the console.

DownloadableCodeProjects/LP2_implement_interfaces/Interfaces_M3/Starter/DatabaseAccess.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
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.
45
public class DatabaseAccess
56
{
67
// Simulates connecting to a database.

0 commit comments

Comments
 (0)