c# - 委托(delegate)在依赖注入(inject)中扮演什么角色?

标签 c# dependency-injection delegates

在大多数依赖注入(inject)示例中,我看到简单的对象被注入(inject),例如下面的示例中SecurityManager被注入(inject)到MainApplication中。

但是,注入(inject)委托(delegate)似乎也很自然,如下面的示例所示,LogHandler被注入(inject)到MainApplication中。

委托(delegate)通常不用于依赖注入(inject)吗?支持和反对使用它们的理由是什么?

using System;
using System.Windows;
using System.Windows.Controls;

namespace TestSimpleDelegate82343
{
    public partial class Window1 : Window
    {
        public delegate void LogHandler(string message);

        public Window1()
        {
            InitializeComponent();
        }

        private void Button_Gui_Lax_Click(object sender, RoutedEventArgs e)
        {
            MainApplication app = new MainApplication(new LogHandler(GuiLogHandler), new LaxSecurityManager());
        }

        private void Button_Console_Lax_Click(object sender, RoutedEventArgs e)
        {
            MainApplication app = new MainApplication(new LogHandler(ConsoleLogHandler), new LaxSecurityManager());
        }

        private void Button_Gui_Tough_Click(object sender, RoutedEventArgs e)
        {
            MainApplication app = new MainApplication(new LogHandler(GuiLogHandler), new ToughSecurityManager());
        }

        private void Button_Console_Tough_Click(object sender, RoutedEventArgs e)
        {
            MainApplication app = new MainApplication(new LogHandler(ConsoleLogHandler), new ToughSecurityManager());
        }

        public void GuiLogHandler(string message)
        {
            TextBlock tb = new TextBlock();
            tb.Text = "logging: " + message;
            TheContent.Children.Add(tb);
        }

        public void ConsoleLogHandler(string message)
        {
            Console.WriteLine("logging: " + message);
        }
    }

    public interface ISecurityManager
    {
        bool UserIsEntitled();
    }

    public class LaxSecurityManager : ISecurityManager
    {
        public bool UserIsEntitled()
        {
            return true;
        }
    }

    public class ToughSecurityManager : ISecurityManager
    {
        public bool UserIsEntitled()
        {
            return false;
        }
    }

    public class MainApplication
    {
        public MainApplication(Window1.LogHandler logHandler, ISecurityManager securityManager)
        {
            logHandler("test1");
            logHandler("test2");
            logHandler("test3");
            if (securityManager.UserIsEntitled())
            {
                logHandler("secret");
            }
        }
    }

}

最佳答案

我偶尔使用委托(delegate) Anonymous Interfaces - 也适用于 DI。

然而,这种方法的一个问题是,对在类中注入(inject)和使用正确的依赖项进行单元测试变得有点困难,因为委托(delegate)实例不是一种类型,有时您会只是想验证一个类是否使用正确类型的策略/依赖关系。

关于c# - 委托(delegate)在依赖注入(inject)中扮演什么角色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1536758/

相关文章:

c# - 使用 Revit API 在模型中放置自定义体量族实例

java - 方法的输出可以用于 Autowiring 另一个 bean 吗?

JavaEE 7 : CDI+OSGI - one real example

c# - 为什么 C# 在带有委托(delegate)的输入参数中使用逆变(而不是协方差)?

c# - 在 WPF 中打开窗口时的新任务栏图标

c# - 事件窗口上的 Windows 系统事件是否已更改?

php - 为什么在 laravel 中创建一个门面而不是直接调用一个方法?

cocoa - 如何在不给对象太多领域知识的情况下为一个对象拥有多个委托(delegate)?

c# - 如何知道 UserControl 何时完成触发事件?

c# - 有没有办法延长 SqlConnection GetSchema() 查询的超时时间?