134 lines
2.8 KiB
C#
134 lines
2.8 KiB
C#
using Autofac;
|
|
using System;
|
|
using System.Reflection;
|
|
|
|
namespace AutofacSamples3
|
|
{
|
|
|
|
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 SMSLog : ILog
|
|
{
|
|
private readonly string phoneNumber;
|
|
|
|
public SMSLog(string phoneNumber)
|
|
{
|
|
this.phoneNumber = phoneNumber;
|
|
}
|
|
public void Write(string message)
|
|
{
|
|
Console.WriteLine($"SMS to {phoneNumber} : {message}");
|
|
}
|
|
}
|
|
|
|
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...");
|
|
}
|
|
}
|
|
|
|
public class Parent
|
|
{
|
|
public override string ToString()
|
|
{
|
|
return "I am your father";
|
|
}
|
|
}
|
|
|
|
public class Child
|
|
{
|
|
public string Name { get; set; }
|
|
public Parent Parent { get; set; }
|
|
|
|
public void SetParent(Parent parent)
|
|
{
|
|
Parent = parent;
|
|
}
|
|
}
|
|
|
|
public class ParentChildModule : Autofac.Module
|
|
{
|
|
protected override void Load(ContainerBuilder builder)
|
|
{
|
|
builder.RegisterType<Parent>();
|
|
builder.Register(c => new Child() { Parent = c.Resolve<Parent>() });
|
|
}
|
|
}
|
|
|
|
|
|
internal class Program
|
|
{
|
|
public static void Main(string[] args)
|
|
{
|
|
var builder = new ContainerBuilder();
|
|
//builder.RegisterAssemblyModules(typeof(Program).Assembly);
|
|
builder.RegisterAssemblyModules<ParentChildModule>(typeof(Program).Assembly);
|
|
|
|
var container = builder.Build();
|
|
Console.WriteLine(container.Resolve<Child>().Parent);
|
|
|
|
}
|
|
}
|
|
}
|