Microsoft.Extensions.DependencyInjection

项目引入:Microsoft.Extensions.DependencyInjection

https://www.nuget.org/packages/Microsoft.Extensions.DependencyInjection

原始写法:

            IMicrophone microphone = new Microphone();
            IPower power = new Power(microphone);
            IHeadphone headphone = new Headphone(power);

依赖注入写法:

            ServiceCollection services = new ServiceCollection();
            services.AddTransient<IMicrophone, Microphone>();
            services.AddTransient<IPower, Power>();
            services.AddTransient<IHeadphone, Headphone>();

            ServiceProvider serviceProvider = services.BuildServiceProvider();

           var headphone = serviceProvider.GetService<IHeadphone>();

相关类

    public interface  IMicrophone{}

    public class Microphone : IMicrophone {}

    public interface IPower{}

    public class Power : IPower {     
        public Power(IMicrophone microphone) { }
    }

    public interface IHeadphone {}

    public class Headphone: IHeadphone {    
        public Headphone(IPower power) { }
    }

  • 9
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Dependency Injection Principles, Practices, and Patterns teaches you to use DI to reduce hard-coded dependencies between application components. You'll start by learning what DI is and what types of applications will benefit from it. Then, you'll work through concrete scenarios using C# and the .NET framework to implement DI in your own projects. As you dive into the thoroughly-explained examples, you'll develop a foundation you can apply to any of the many DI libraries for .NET and .NET Core. Purchase of the print book includes a free eBook in PDF, Kindle, and ePub formats from Manning Publications. About the Technology Dependency Injection (DI) is a great way to reduce tight coupling between software components. Instead of hard-coding dependencies, such as specifying a database driver, you make those connections through a third party. Central to application frameworks like ASP.NET Core, DI enables you to better manage changes and other complexity in your software. About the Book Dependency Injection Principles, Practices, and Patterns is a revised and expanded edition of the bestselling classic Dependency Injection in .NET. It teaches you DI from the ground up, featuring relevant examples, patterns, and anti-patterns for creating loosely coupled, well-structured applications. The well-annotated code and diagrams use C# examples to illustrate principles that work flawlessly with modern object-oriented languages and DI libraries. What's Inside Refactoring existing code into loosely coupled code DI techniques that work with statically typed OO languages Integration with common .NET frameworks Updated examples illustrating DI in .NET Core About the Reader For intermediate OO developers.
WPF (Windows Presentation Foundation) 是一个基于 .NET Framework 的 UI 框架,它提供了丰富的界面设计和开发功能。在 WPF 中使用 Microsoft.Extensions.DependencyInjection 可以帮助我们更好地实现依赖注入(Dependency Injection)。 Microsoft.Extensions.DependencyInjection 是 .NET Core 中的一个轻量级依赖注入框架,它提供了简单、灵活的依赖注入方式。在 WPF 中,我们同样可以使用该框架来实现依赖注入。 首先,我们需要在 WPF 应用程序中安装 Microsoft.Extensions.DependencyInjection NuGet 包。 在 App.xaml.cs 文件中,我们可以创建一个 IServiceCollection 对象,并在其中注册依赖: ```csharp public partial class App : Application { public static IServiceProvider ServiceProvider { get; private set; } protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); var services = new ServiceCollection(); // 注册依赖 services.AddSingleton<IMyService, MyService>(); // 创建容器 ServiceProvider = services.BuildServiceProvider(); } } ``` 在上面的代码中,我们注册了一个名为 `MyService` 的服务,并指定其实现接口为 `IMyService`。当我们需要使用该服务的时候,可以通过 ServiceProvider 来解析: ```csharp var service = App.ServiceProvider.GetService<IMyService>(); ``` 这样就完成了依赖注入的过程。在 WPF 中,我们通常会在 ViewModel 中使用依赖注入,来实现视图和逻辑的分离。通过使用 Microsoft.Extensions.DependencyInjection,我们可以更方便地管理和注入依赖项,提高代码的可测试性和可维护性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值