Skip to content

Instantly share code, notes, and snippets.

@lele85
Created January 31, 2012 20:39
Show Gist options
  • Save lele85/1712766 to your computer and use it in GitHub Desktop.
Save lele85/1712766 to your computer and use it in GitHub Desktop.
Dependency Injection Hello World in C#
using System;
using System.Configuration;
using System.Threading;
using System.Collections;
using System.Collections.Generic;
namespace HelloDI
{
public class MainClass
{
public static void Main (string[] args)
{
var loggedSecureConsoleWriter =
new LoggedMessageWriter(
new SecureMessageWriter(
new ConsoleMessageWriter(
)));
var loggedNullWriter =
new LoggedMessageWriter(
new NullMessageWriter(
));
var loggedConsoleWriter =
new LoggedMessageWriter(
new ConsoleMessageWriter(
));
var multichannelWriter =
new MultiChannelMessageWriter(
new IMessageWriter[]
{
loggedSecureConsoleWriter,
loggedNullWriter,
loggedConsoleWriter,
}
);
var salutation = new Salutation(multichannelWriter);
salutation.Exclaim();
}
}
/// <summary>
/// Message writer.
/// </summary>
public interface IMessageWriter
{
void Write(string message);
}
/// <summary>
/// Salutation.
/// </summary>
/// <exception cref='ArgumentNullException'>
/// Is thrown when an argument passed to a method is invalid because it is <see langword="null" /> .
/// </exception>
public class Salutation
{
private readonly IMessageWriter _writer;
public Salutation(IMessageWriter writer)
{
if (writer == null)
{
throw new ArgumentNullException("writer");
}
_writer = writer;
}
public void Exclaim()
{
_writer.Write("Hello DI!");
}
}
/// <summary>
/// Multi channel message writer.
/// </summary>
public class MultiChannelMessageWriter : IMessageWriter
{
private readonly IEnumerable<IMessageWriter> _writers;
public MultiChannelMessageWriter (IEnumerable<IMessageWriter> writers)
{
if (writers == null){
throw new ArgumentNullException("writers");
}
_writers = writers;
}
public void Write (string message)
{
foreach (var writer in _writers)
{
writer.Write(message);
}
}
}
/// <summary>
/// Console message writer. Writes a message on console stdout.
/// </summary>
public class ConsoleMessageWriter : IMessageWriter
{
public void Write (string message)
{
Console.WriteLine(message);
}
}
/// <summary>
/// Null message writer.
/// Does nothing :)
/// </summary>
public class NullMessageWriter : IMessageWriter
{
public void Write(string message)
{
}
}
/// <summary>
/// Secure message writer.
/// Decorates an IMessageWriter with security checks.
/// </summary>
/// <exception cref='ArgumentNullException'>
/// Is thrown when an argument passed to a method is invalid because it is <see langword="null" /> .
/// </exception>
public class SecureMessageWriter : IMessageWriter
{
private readonly IMessageWriter _writer;
public SecureMessageWriter (IMessageWriter writer)
{
if (writer == null){
throw new ArgumentNullException("writer");
}
_writer = writer;
}
public void Write (string message)
{
if (Thread.CurrentPrincipal.Identity.IsAuthenticated) {
_writer.Write("message");
}
_writer.Write("Unauthenticated attempt");
}
}
/// <summary>
/// Logged message writer. Logs message writer to console.
/// </summary>
/// <exception cref='ArgumentNullException'>
/// Is thrown when an argument passed to a method is invalid because it is <see langword="null" /> .
/// </exception>
public class LoggedMessageWriter : IMessageWriter
{
private readonly IMessageWriter _writer;
public LoggedMessageWriter (IMessageWriter writer)
{
if (writer == null){
throw new ArgumentNullException("writer");
}
_writer = writer;
}
public void Write(string message)
{
LogWriteAttempt ();
_writer.Write(message);
}
void LogWriteAttempt ()
{
var logMessage = string.Format("[INFO] - {0} - Write attempt", DateTime.Now);
Console.WriteLine(logMessage);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment