Extracting classes from assembly

This commit is contained in:
2020-11-29 21:46:11 +01:00
parent fda4022f5e
commit 618ce16f70
3 changed files with 147 additions and 0 deletions

View File

@ -11,6 +11,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PatternDemoCore2", "Pattern
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutoFacSamples2", "AutoFacSamples2\AutoFacSamples2.csproj", "{0EB88BA8-3B88-4142-A6D3-07F72120488D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutofacSamples3", "AutofacSamples3\AutofacSamples3.csproj", "{4EE75D74-3696-41D7-A5D0-A128956FBCBB}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -33,6 +35,10 @@ Global
{0EB88BA8-3B88-4142-A6D3-07F72120488D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0EB88BA8-3B88-4142-A6D3-07F72120488D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0EB88BA8-3B88-4142-A6D3-07F72120488D}.Release|Any CPU.Build.0 = Release|Any CPU
{4EE75D74-3696-41D7-A5D0-A128956FBCBB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4EE75D74-3696-41D7-A5D0-A128956FBCBB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4EE75D74-3696-41D7-A5D0-A128956FBCBB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4EE75D74-3696-41D7-A5D0-A128956FBCBB}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Autofac" Version="6.0.0" />
</ItemGroup>
</Project>

129
AutofacSamples3/Program.cs Normal file
View File

@ -0,0 +1,129 @@
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;
}
}
internal class Program
{
public static void Main(string[] args)
{
var assembly = Assembly.GetExecutingAssembly();
var builder = new ContainerBuilder();
builder.RegisterAssemblyTypes(assembly)
.Where(t => t.Name.EndsWith("Log"))
.Except<SMSLog>()
.Except<ConsoleLog>(c => c.As<ILog>().SingleInstance())
.AsSelf();
builder.RegisterAssemblyTypes(assembly)
.Except<SMSLog>()
.Where(t => t.Name.EndsWith("Log"))
.As(t => t.GetInterfaces()[0]);
}
}
}