modify
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
using Autofac;
|
using Autofac;
|
||||||
using IoC.implement;
|
using IoC.implement;
|
||||||
using IoC.inerface;
|
using IoC.inerface;
|
||||||
|
using IoC.services;
|
||||||
|
|
||||||
namespace IoC
|
namespace IoC
|
||||||
{
|
{
|
||||||
@@ -15,7 +16,7 @@ namespace IoC
|
|||||||
ContainerBuilder builder = new ContainerBuilder();
|
ContainerBuilder builder = new ContainerBuilder();
|
||||||
|
|
||||||
//在仲介公司裡寫需求人申請單
|
//在仲介公司裡寫需求人申請單
|
||||||
builder.RegisterType<MineWithMiddle>();
|
builder.RegisterType<MineIOC>();
|
||||||
//小明所需打掃阿姨需求
|
//小明所需打掃阿姨需求
|
||||||
builder.RegisterType<Aunt>().As<ISwapable>();
|
builder.RegisterType<Aunt>().As<ISwapable>();
|
||||||
|
|
||||||
|
|||||||
@@ -56,6 +56,8 @@
|
|||||||
<Compile Include="inerface\ISwapable.cs" />
|
<Compile Include="inerface\ISwapable.cs" />
|
||||||
<Compile Include="Program.cs" />
|
<Compile Include="Program.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
|
<Compile Include="services\Mine.cs" />
|
||||||
|
<Compile Include="services\MineIOC.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Include="app.config" />
|
<None Include="app.config" />
|
||||||
|
|||||||
@@ -1,51 +1,9 @@
|
|||||||
using Autofac;
|
using Autofac;
|
||||||
using IoC.implement;
|
using IoC.services;
|
||||||
using IoC.inerface;
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
|
|
||||||
namespace IoC
|
namespace IoC
|
||||||
{
|
{
|
||||||
/// <summary>
|
|
||||||
/// (沒有注入的)
|
|
||||||
/// 小明直接依賴 Aunt 不是依賴抽象
|
|
||||||
/// 日後要改必須動內部
|
|
||||||
/// </summary>
|
|
||||||
public class Mine
|
|
||||||
{
|
|
||||||
public Aunt aunt = new Aunt();
|
|
||||||
|
|
||||||
public void Room()
|
|
||||||
{
|
|
||||||
aunt.Swapping();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 小明 ( 有注入的 )
|
|
||||||
/// </summary>
|
|
||||||
public class MineWithMiddle
|
|
||||||
{
|
|
||||||
private ISwapable aunt;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 依賴抽象[打掃動作的人]
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="swapAble"></param>
|
|
||||||
public MineWithMiddle(ISwapable swapAble)
|
|
||||||
{
|
|
||||||
aunt = swapAble;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Room()
|
|
||||||
{
|
|
||||||
aunt.Swapping();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
class Program
|
class Program
|
||||||
{
|
{
|
||||||
static void Main(string[] args)
|
static void Main(string[] args)
|
||||||
@@ -57,11 +15,11 @@ namespace IoC
|
|||||||
//========================
|
//========================
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// 有注入的
|
// 有注入的
|
||||||
IContainer middleCompany = Container.MiddleCompany();
|
IContainer middleCompany = Container.MiddleCompany();
|
||||||
|
|
||||||
//仲介公司(IOC AutoFac)自動幫小明注入一個打掃阿姨
|
//仲介公司(IOC AutoFac)自動幫小明注入一個打掃阿姨
|
||||||
MineWithMiddle mineWithMiddle = middleCompany.Resolve<MineWithMiddle>();
|
MineIOC mineWithMiddle = middleCompany.Resolve<MineIOC>();
|
||||||
|
|
||||||
mineWithMiddle.Room();
|
mineWithMiddle.Room();
|
||||||
|
|
||||||
|
|||||||
17
src/IoC/IoC/services/Mine.cs
Normal file
17
src/IoC/IoC/services/Mine.cs
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
using IoC.implement;
|
||||||
|
|
||||||
|
namespace IoC.services
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 沒有注入
|
||||||
|
/// </summary>
|
||||||
|
public class Mine
|
||||||
|
{
|
||||||
|
public Aunt aunt = new Aunt();
|
||||||
|
|
||||||
|
public void Room()
|
||||||
|
{
|
||||||
|
aunt.Swapping();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
26
src/IoC/IoC/services/MineIOC.cs
Normal file
26
src/IoC/IoC/services/MineIOC.cs
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
using IoC.inerface;
|
||||||
|
|
||||||
|
namespace IoC.services
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 有注入
|
||||||
|
/// </summary>
|
||||||
|
public class MineIOC
|
||||||
|
{
|
||||||
|
private readonly ISwapable aunt;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 依賴抽象[打掃動作的人]
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="swapable"></param>
|
||||||
|
public MineIOC(ISwapable swapable)
|
||||||
|
{
|
||||||
|
aunt = swapable;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Room()
|
||||||
|
{
|
||||||
|
aunt.Swapping();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user