Dependency Injection without any framework?
Dependency Injection without any framework
What interviewers test
- SOLID
- Architecture fundamentals
Step 1: Abstraction
public interface IMessageService
{
void Send(string message);
}
Step 2: Implementation
public class EmailService : IMessageService
{
public void Send(string message)
{
Console.WriteLine("Email: " + message);
}
}
Step 3: Injection
public class Notification
{
private readonly IMessageService _service;
public Notification(IMessageService service)
{
_service = service;
}
public void Notify(string msg)
{
_service.Send(msg);
}
}
Why this matters
- Loose coupling
- Testable code
- Swappable implementations