Question
IN C#: Dependency injection describes a process whereby dependent objects are injected into a consuming object rather than having the consuming object instantiate them itself.
IN C#:
Dependency injection describes a process whereby dependent objects are injected into a consuming object rather than having the consuming object instantiate them itself.
In this Assignment, you will explore a simple example of dependency injection using logger classes.
Please download the following starter files:
cs file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
namespace
UCLAExtension.DesignPatterns.DependencyInjection.LoggerService.Lab
{
public interface ILoggerService
{
void LogInfo(string msg, params object[] args);
void LogError(string msg, params object[] args);
}
public class ConsoleLogger : ILoggerService
{
// *** Add code here ***
}
public class FileLogger : ILoggerService
{
// *** Add code here ***
}
public class BankAccount
{
// *** Add code here ***
}
class Program
{
static void Main(string[] args)
{
Console.Title = "Dependency Injection: Logger Example";
Console.WindowHeight = (int)(Console.LargestWindowHeight *
0.75);
Console.BufferHeight = 5000;
// Test the Console Logger
// *** Add code here ***
// Test the File Logger
// *** Add code here ***
Console.ReadLine();
}
}
}
pdf:
Assignment: Dependency Injection-Logger Class Dependency injection describes a process whereby dependent objects are "injected" into a consuming object rather than having the consuming object instantiate them itself In this assignment, you will explore a simple example of dependency injection using logger classes. Please submit only the completed program file (Program [...].cs) to the drop box Overview Dependency injection promotes flexibility and adaptability in software systems through loose coupling Coupling refers to the degree to which objects are interdependent. Dependency injection eliminates strong dependencies between objects by using abstract interfaces to express dependencies A dependency injection involves three elements ea consumer a contract an injector The contract, an interface or abstract class, declares the requirements for the dependent object. The injector instantiates the dependent object and then injects it into the consumer. Typically, a variety of dependent objects satisfy the contract. Constructor injection uses the consuming object's constructor to inject the dependent object. Other alternatives include property setter and interface injection Here is a UML diagram describing constructor injection Implementing Constructor Injection Dependent IDependency 0 In this exercise, you will implement two simple logger classes, a console logger and a file logger, that implement a common logger interface. Other loggers could easily be defined (e.g., an event logger) Make that an exercise for next time
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started