Compare commits
10 Commits
e10dcfa377
...
9cc7079329
| Author | SHA1 | Date | |
|---|---|---|---|
| 9cc7079329 | |||
| 0588a6a224 | |||
| 618ce16f70 | |||
| fda4022f5e | |||
| e2027a5f7e | |||
| 5ca347fc6a | |||
| 4abfd0ed97 | |||
| e7cebf1563 | |||
| 7cf622179c | |||
| 666e719f5d |
@ -3,7 +3,17 @@ Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 16
|
||||
VisualStudioVersion = 16.0.30717.126
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutoFacSamles", "AutoFacSamles\AutoFacSamles.csproj", "{2CFA4809-16F7-4DB9-B4BB-CF60945656E5}"
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AutoFacSamles", "AutoFacSamles\AutoFacSamles.csproj", "{2CFA4809-16F7-4DB9-B4BB-CF60945656E5}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PatternDemoCore", "PatternDemoCore\PatternDemoCore.csproj", "{D8041EF1-F2AC-4A04-A7A5-56E2CB6F249C}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PatternDemoCore2", "PatternDemo2\PatternDemoCore2.csproj", "{2152A909-E97C-453A-8A6C-869F7987D7B5}"
|
||||
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
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutofacSamples4", "AutofacSample3\AutofacSamples4.csproj", "{9F3E52C4-985F-47FD-9949-B5539200069F}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
@ -15,6 +25,26 @@ Global
|
||||
{2CFA4809-16F7-4DB9-B4BB-CF60945656E5}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{2CFA4809-16F7-4DB9-B4BB-CF60945656E5}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{2CFA4809-16F7-4DB9-B4BB-CF60945656E5}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{D8041EF1-F2AC-4A04-A7A5-56E2CB6F249C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{D8041EF1-F2AC-4A04-A7A5-56E2CB6F249C}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{D8041EF1-F2AC-4A04-A7A5-56E2CB6F249C}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{D8041EF1-F2AC-4A04-A7A5-56E2CB6F249C}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{2152A909-E97C-453A-8A6C-869F7987D7B5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{2152A909-E97C-453A-8A6C-869F7987D7B5}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{2152A909-E97C-453A-8A6C-869F7987D7B5}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{2152A909-E97C-453A-8A6C-869F7987D7B5}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{0EB88BA8-3B88-4142-A6D3-07F72120488D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{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
|
||||
{9F3E52C4-985F-47FD-9949-B5539200069F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{9F3E52C4-985F-47FD-9949-B5539200069F}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{9F3E52C4-985F-47FD-9949-B5539200069F}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{9F3E52C4-985F-47FD-9949-B5539200069F}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
<TargetFramework>net5.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
using Autofac;
|
||||
using Autofac.Core;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
@ -49,6 +50,20 @@ namespace AutoFacSamles
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
@ -79,14 +94,36 @@ namespace AutoFacSamles
|
||||
{
|
||||
var builder = new ContainerBuilder();
|
||||
|
||||
// IList<T> --> List<T>
|
||||
// IList<int> --> List<int>
|
||||
builder.RegisterGeneric(typeof(List<>)).As(typeof(IList<>));
|
||||
// Named parameter
|
||||
//builder.RegisterType<SMSLog>()
|
||||
// .As<ILog>()
|
||||
// .WithParameter("phoneNumber", "+173874872643");
|
||||
|
||||
IContainer container = builder.Build();
|
||||
// Typed parameter
|
||||
//builder.RegisterType<SMSLog>()
|
||||
// .As<ILog>()
|
||||
// .WithParameter(new TypedParameter(typeof(string), "+173874872643"));
|
||||
|
||||
// resolved parameter
|
||||
//builder.RegisterType<SMSLog>()
|
||||
// .As<ILog>()
|
||||
// .WithParameter(
|
||||
// new ResolvedParameter(
|
||||
// // predicate
|
||||
// (pi, ctx) => pi.ParameterType == typeof(string) && pi.Name == "phoneNumber",
|
||||
// (pi, ctx) => "+173874872643"
|
||||
// )
|
||||
// );
|
||||
|
||||
Random random = new Random();
|
||||
builder.Register((c, p) => new SMSLog(p.Named<string>("phoneNumber")))
|
||||
.As<ILog>();
|
||||
Console.WriteLine("About to build container ...");
|
||||
var container = builder.Build();
|
||||
|
||||
var log = container.Resolve<ILog>(new NamedParameter("phoneNumber", random.Next().ToString()));
|
||||
log.Write("Testing");
|
||||
|
||||
var myList = container.Resolve<IList<int>>();
|
||||
Console.WriteLine(myList.GetType());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
12
AutoFacSamples2/AutoFacSamples2.csproj
Normal file
12
AutoFacSamples2/AutoFacSamples2.csproj
Normal file
@ -0,0 +1,12 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net5.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Autofac" Version="6.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
58
AutoFacSamples2/Program.cs
Normal file
58
AutoFacSamples2/Program.cs
Normal file
@ -0,0 +1,58 @@
|
||||
using Autofac;
|
||||
using System;
|
||||
|
||||
namespace AutoFacSamples2
|
||||
{
|
||||
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
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
var builder = new ContainerBuilder();
|
||||
builder.RegisterType<Parent>();
|
||||
|
||||
//1/builder.RegisterType<Child>().PropertiesAutowired();
|
||||
|
||||
//2/builder.RegisterType<Child>()
|
||||
//2/ .WithProperty("Parent", new Parent());
|
||||
|
||||
//3/builder.Register(c =>
|
||||
//3/{
|
||||
//3/ var child = new Child();
|
||||
//3/ child.SetParent(c.Resolve<Parent>());
|
||||
//3/ return child;
|
||||
//3/});
|
||||
|
||||
builder.RegisterType<Child>()
|
||||
.OnActivated(e =>
|
||||
{
|
||||
var p = e.Context.Resolve<Parent>();
|
||||
e.Instance.SetParent(p);
|
||||
});
|
||||
|
||||
|
||||
var container = builder.Build();
|
||||
var parent = container.Resolve<Child>().Parent;
|
||||
Console.WriteLine(parent);
|
||||
}
|
||||
}
|
||||
}
|
||||
8
AutofacSample3/AutofacSamples4.csproj
Normal file
8
AutofacSample3/AutofacSamples4.csproj
Normal file
@ -0,0 +1,8 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
53
AutofacSample3/Program.cs
Normal file
53
AutofacSample3/Program.cs
Normal file
@ -0,0 +1,53 @@
|
||||
using System;
|
||||
|
||||
namespace AutofacSamples4
|
||||
{
|
||||
|
||||
public interface ILog
|
||||
{
|
||||
void Write(string message);
|
||||
}
|
||||
|
||||
public class ConsoleLog : ILog
|
||||
{
|
||||
public ConsoleLog()
|
||||
{
|
||||
Console.WriteLine($"Console log created at {DateTime.Now.Ticks}");
|
||||
}
|
||||
public void Write(string message)
|
||||
{
|
||||
Console.WriteLine(message);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Console.WriteLine($"Console logger no longer required");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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 void Dispose()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Console.WriteLine("Hello World!");
|
||||
}
|
||||
}
|
||||
}
|
||||
12
AutofacSamples3/AutofacSamples3.csproj
Normal file
12
AutofacSamples3/AutofacSamples3.csproj
Normal 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>
|
||||
133
AutofacSamples3/Program.cs
Normal file
133
AutofacSamples3/Program.cs
Normal file
@ -0,0 +1,133 @@
|
||||
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);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
12
PatternDemo2/PatternDemoCore2.csproj
Normal file
12
PatternDemo2/PatternDemoCore2.csproj
Normal file
@ -0,0 +1,12 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net5.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Autofac" Version="6.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
58
PatternDemo2/Program.cs
Normal file
58
PatternDemo2/Program.cs
Normal file
@ -0,0 +1,58 @@
|
||||
using Autofac;
|
||||
using System;
|
||||
|
||||
namespace PatternDemoCore
|
||||
{
|
||||
|
||||
public class Entity
|
||||
{
|
||||
public delegate Entity Factory();
|
||||
|
||||
private static Random random = new Random();
|
||||
private int number;
|
||||
|
||||
public Entity()
|
||||
{
|
||||
number = random.Next();
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"test {number}";
|
||||
}
|
||||
}
|
||||
|
||||
public class ViewModel
|
||||
{
|
||||
private readonly Entity.Factory entityFactory;
|
||||
|
||||
public ViewModel(Entity.Factory entityFactory)
|
||||
{
|
||||
this.entityFactory = entityFactory;
|
||||
}
|
||||
|
||||
public void Method()
|
||||
{
|
||||
var entity = entityFactory();
|
||||
Console.WriteLine(entity);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public class Demo
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
var cb = new ContainerBuilder();
|
||||
cb.RegisterType<Entity>().InstancePerDependency();
|
||||
cb.RegisterType<ViewModel>();
|
||||
|
||||
var container = cb.Build();
|
||||
var vm = container.Resolve<ViewModel>();
|
||||
|
||||
vm.Method();
|
||||
vm.Method();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
12
PatternDemoCore/PatternDemoCore.csproj
Normal file
12
PatternDemoCore/PatternDemoCore.csproj
Normal file
@ -0,0 +1,12 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net5.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Autofac" Version="6.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
56
PatternDemoCore/Program.cs
Normal file
56
PatternDemoCore/Program.cs
Normal file
@ -0,0 +1,56 @@
|
||||
using Autofac;
|
||||
using System;
|
||||
|
||||
namespace PatternDemoCore
|
||||
{
|
||||
|
||||
public class Service
|
||||
{
|
||||
public string DoSomething(int value)
|
||||
{
|
||||
return $"I have {value}";
|
||||
}
|
||||
}
|
||||
|
||||
public class DomainObject
|
||||
{
|
||||
private readonly Service service;
|
||||
private readonly int value;
|
||||
|
||||
public delegate DomainObject Factory(int value);
|
||||
|
||||
public DomainObject(Service service, int value)
|
||||
{
|
||||
this.service = service;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return service.DoSomething(value);
|
||||
}
|
||||
}
|
||||
|
||||
public class Demo
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
var cb = new ContainerBuilder();
|
||||
cb.RegisterType<Service>();
|
||||
cb.RegisterType<DomainObject>();
|
||||
|
||||
var container = cb.Build();
|
||||
var dobj = container.Resolve<DomainObject>(
|
||||
new PositionalParameter(1, 42));
|
||||
Console.WriteLine(dobj);
|
||||
|
||||
// Better way
|
||||
|
||||
var factory = container.Resolve<DomainObject.Factory>();
|
||||
var dobj2 = factory(42);
|
||||
Console.WriteLine(dobj2);
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user