From 618ce16f709a6f0bfc6f9860a2429a5f7bb34607 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tommy=20=C3=96man?= Date: Sun, 29 Nov 2020 21:46:11 +0100 Subject: [PATCH] Extracting classes from assembly --- AutoFacSamles.sln | 6 ++ AutofacSamples3/AutofacSamples3.csproj | 12 +++ AutofacSamples3/Program.cs | 129 +++++++++++++++++++++++++ 3 files changed, 147 insertions(+) create mode 100644 AutofacSamples3/AutofacSamples3.csproj create mode 100644 AutofacSamples3/Program.cs diff --git a/AutoFacSamles.sln b/AutoFacSamles.sln index 5e52b27..3c1de9c 100644 --- a/AutoFacSamles.sln +++ b/AutoFacSamles.sln @@ -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 diff --git a/AutofacSamples3/AutofacSamples3.csproj b/AutofacSamples3/AutofacSamples3.csproj new file mode 100644 index 0000000..d1ec505 --- /dev/null +++ b/AutofacSamples3/AutofacSamples3.csproj @@ -0,0 +1,12 @@ + + + + Exe + netcoreapp3.1 + + + + + + + diff --git a/AutofacSamples3/Program.cs b/AutofacSamples3/Program.cs new file mode 100644 index 0000000..14f2953 --- /dev/null +++ b/AutofacSamples3/Program.cs @@ -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() + .Except(c => c.As().SingleInstance()) + .AsSelf(); + + builder.RegisterAssemblyTypes(assembly) + .Except() + .Where(t => t.Name.EndsWith("Log")) + .As(t => t.GetInterfaces()[0]); + + } + } +}