sepearte files
This commit is contained in:
26
src/IoC/IoC/Container.cs
Normal file
26
src/IoC/IoC/Container.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using Autofac;
|
||||
using IoC.implement;
|
||||
using IoC.inerface;
|
||||
|
||||
namespace IoC
|
||||
{
|
||||
public class Container
|
||||
{
|
||||
/// <summary>
|
||||
/// 仲介公司
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static IContainer MiddleCompany()
|
||||
{
|
||||
ContainerBuilder builder = new ContainerBuilder();
|
||||
|
||||
//在仲介公司裡寫需求人申請單
|
||||
builder.RegisterType<MineWithMiddle>();
|
||||
//小明所需打掃阿姨需求
|
||||
builder.RegisterType<Aunt>().As<ISwapable>();
|
||||
|
||||
return builder.Build();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -37,7 +37,6 @@
|
||||
<ItemGroup>
|
||||
<Reference Include="Autofac, Version=4.6.2.0, Culture=neutral, PublicKeyToken=17863af14b0044da, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Autofac.4.6.2\lib\net45\Autofac.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
@@ -52,6 +51,9 @@
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Container.cs" />
|
||||
<Compile Include="implement\Aunt.cs" />
|
||||
<Compile Include="inerface\ISwapable.cs" />
|
||||
<Compile Include="Program.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
@@ -59,6 +61,7 @@
|
||||
<None Include="app.config" />
|
||||
<None Include="packages.config" />
|
||||
</ItemGroup>
|
||||
<ItemGroup />
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
Other similar extension points exist, see Microsoft.Common.targets.
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
using Autofac;
|
||||
using IoC.implement;
|
||||
using IoC.inerface;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@@ -7,6 +9,7 @@ using System.Text;
|
||||
namespace IoC
|
||||
{
|
||||
/// <summary>
|
||||
/// (沒有注入的)
|
||||
/// 小明直接依賴 Aunt 不是依賴抽象
|
||||
/// 日後要改必須動內部
|
||||
/// </summary>
|
||||
@@ -21,7 +24,7 @@ namespace IoC
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 小明
|
||||
/// 小明 ( 有注入的 )
|
||||
/// </summary>
|
||||
public class MineWithMiddle
|
||||
{
|
||||
@@ -43,35 +46,20 @@ namespace IoC
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 依賴抽象[打掃動作的人]
|
||||
/// </summary>
|
||||
public class Aunt : ISwapable
|
||||
{
|
||||
public void Swapping()
|
||||
{
|
||||
Console.WriteLine("Aunt Swapping");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 打掃動作
|
||||
/// </summary>
|
||||
public interface ISwapable
|
||||
{
|
||||
void Swapping();
|
||||
}
|
||||
|
||||
|
||||
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
// 沒有注入的
|
||||
Mine mine = new Mine();
|
||||
mine.Room();
|
||||
|
||||
IContainer middleCompany = MiddleCompany();
|
||||
//========================
|
||||
|
||||
|
||||
|
||||
// 有注入的
|
||||
IContainer middleCompany = Container.MiddleCompany();
|
||||
//仲介公司(IOC AutoFac)自動幫小明注入一個打掃阿姨
|
||||
MineWithMiddle mineWithMiddle = middleCompany.Resolve<MineWithMiddle>();
|
||||
|
||||
@@ -80,20 +68,5 @@ namespace IoC
|
||||
Console.ReadKey();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 仲介公司
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private static IContainer MiddleCompany()
|
||||
{
|
||||
ContainerBuilder builder = new ContainerBuilder();
|
||||
|
||||
//在仲介公司裡寫需求人申請單
|
||||
builder.RegisterType<MineWithMiddle>();
|
||||
//小明所需打掃阿姨需求
|
||||
builder.RegisterType<Aunt>().As<ISwapable>();
|
||||
|
||||
return builder.Build();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
16
src/IoC/IoC/implement/Aunt.cs
Normal file
16
src/IoC/IoC/implement/Aunt.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using IoC.inerface;
|
||||
using System;
|
||||
|
||||
namespace IoC.implement
|
||||
{
|
||||
/// <summary>
|
||||
/// 依賴抽象[打掃動作的人]
|
||||
/// </summary>
|
||||
public class Aunt : ISwapable
|
||||
{
|
||||
public void Swapping()
|
||||
{
|
||||
Console.WriteLine("Aunt Swapping");
|
||||
}
|
||||
}
|
||||
}
|
||||
16
src/IoC/IoC/inerface/ISwapable.cs
Normal file
16
src/IoC/IoC/inerface/ISwapable.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace IoC.inerface
|
||||
{
|
||||
/// <summary>
|
||||
/// 打掃動作
|
||||
/// </summary>
|
||||
public interface ISwapable
|
||||
{
|
||||
void Swapping();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user