https://www.cnblogs.com/artech/p/net-core-di-01.html 大内老A的在.NET Core下对这些的介绍,有一系列文章
https://www.cnblogs.com/jesse2013/p/di-in-aspnetcore.html
https://www.cnblogs.com/artech/p/dependency-injection-in-asp-net-core.html
https://www.zybuluo.com/dasajia2lang/note/1481011
下面开始
在上一篇的笔记中,在.NET Freamwork中,有一个第三方容器Unity,可以实现注入,但是在.NET Core里面,有一个IServiceCollection,这个是.NET Core框架自带的一个容器,和Unity很相似,都是个容器。
下面我们新建一个控制台程序,在控制台程序中,对IServiceCollection的使用做介绍。
下面代码,是本次实例中需要注入的类型,需要用的倒是再点开来看吧
namespace Bingle.Core.Interface { public interface ITestServiceA { void Show(); } } namespace Bingle.Core.Service { public class TestServiceA : ITestServiceA { public void Show() { Console.WriteLine("A123456"); } } } namespace Bingle.Core.Interface { public interface ITestServiceB { void Show(); } } namespace Bingle.Core.Service { public class TestServiceB : ITestServiceB { public TestServiceB(ITestServiceA iTestService) { } public void Show() { Console.WriteLine($"This is TestServiceB B123456"); } } } namespace Bingle.Core.Interface { public interface ITestServiceC { void Show(); } } namespace Bingle.Core.Service { public class TestServiceC : ITestServiceC { public TestServiceC(ITestServiceB iTestServiceB) { } public void Show() { Console.WriteLine("C123456"); } } } namespace Bingle.Core.Interface { public interface ITestServiceD { void Show(); } } namespace Bingle.Core.Service { public class TestServiceD : ITestServiceD { public void Show() { Console.WriteLine("D123456"); } } }