93 lines
1.9 KiB
C#
93 lines
1.9 KiB
C#
using Autofac;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace AutoFacSamles
|
|
{
|
|
public interface ILog
|
|
{
|
|
void Write(string message);
|
|
}
|
|
|
|
public class ConsoleLog : ILog
|
|
{
|
|
public void Write(string message)
|
|
{
|
|
Console.WriteLine(message);
|
|
}
|
|
}
|
|
|
|
public class EmailLog : ILog
|
|
{
|
|
private const string adminEmail = "tfoman@oeman.se";
|
|
public void Write(string message)
|
|
{
|
|
Console.WriteLine($"Email sent to {adminEmail} : {message}");
|
|
}
|
|
}
|
|
|
|
public class Engine
|
|
{
|
|
private ILog log;
|
|
private int id;
|
|
|
|
public Engine(ILog log, int id)
|
|
{
|
|
this.log = log;
|
|
this.id = id;
|
|
}
|
|
|
|
public Engine(ILog log)
|
|
{
|
|
this.log = log;
|
|
id = new Random().Next();
|
|
}
|
|
|
|
public void Ahead(int power)
|
|
{
|
|
log.Write($"Engine [{id}] ahead {power}");
|
|
}
|
|
}
|
|
|
|
public class Car
|
|
{
|
|
private Engine engine;
|
|
private ILog log;
|
|
|
|
public Car(Engine engine)
|
|
{
|
|
this.engine = engine;
|
|
this.log = new EmailLog();
|
|
}
|
|
|
|
public Car(Engine engine, ILog log)
|
|
{
|
|
this.engine = engine;
|
|
this.log = log;
|
|
}
|
|
|
|
public void Go()
|
|
{
|
|
engine.Ahead(100);
|
|
log.Write("Car going forward...");
|
|
}
|
|
}
|
|
|
|
class Program
|
|
{
|
|
static void Main(string[] args)
|
|
{
|
|
var builder = new ContainerBuilder();
|
|
|
|
// IList<T> --> List<T>
|
|
// IList<int> --> List<int>
|
|
builder.RegisterGeneric(typeof(List<>)).As(typeof(IList<>));
|
|
|
|
IContainer container = builder.Build();
|
|
|
|
var myList = container.Resolve<IList<int>>();
|
|
Console.WriteLine(myList.GetType());
|
|
}
|
|
}
|
|
}
|