【问题标题】:Seemann's Dependency Injection, "Three Calls Pattern" vs Service Locator Anit-PatternSeemann 依赖注入,“三调用模式”与服务定位器反模式
【发布时间】:2016-03-09 15:17:39
【问题描述】:

我使用依赖注入 (DI) 和 Ninject 作为 DI 容器创建了一个 WinForms MVC 应用程序。基本架构如下

Program.cs(WinForms 应用程序的主入口点):

static class Program
{
    [STAThread]
    static void Main()
    {
        ...

        CompositionRoot.Initialize(new DependencyModule());
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(CompositionRoot.Resolve<ApplicationShellView>());
    }
}

DependencyModule.cs

public class DependencyModule : NinjectModule
{
    public override void Load()
    {
        Bind<IApplicationShellView>().To<ApplicationShellView>();
        Bind<IDocumentController>().To<SpreadsheetController>();
        Bind<ISpreadsheetView>().To<SpreadsheetView>();
    }
}

CompositionRoot.cs

public class CompositionRoot
{
    private static IKernel ninjectKernel;

    public static void Initialize(INinjectModule module)
    {
        ninjectKernel = new StandardKernel(module);
    }

    public static T Resolve<T>()
    {
        return ninjectKernel.Get<T>();
    }

    public static IEnumerable<T> ResolveAll<T>()
    {
        return ninjectKernel.GetAll<T>();
    }
}

ApplicationShellView.cs(应用程序的主窗体)

public partial class ApplicationShellView : C1RibbonForm, IApplicationShellView
{
    private ApplicationShellController controller; 

    public ApplicationShellView()
    {
        this.controller = new ApplicationShellController(this);
        InitializeComponent();
    }

    public void InitializeView()
    {
        dockPanel.Extender.FloatWindowFactory = new CustomFloatWindowFactory();
        dockPanel.Theme = vS2012LightTheme;
    }

    private void ribbonButtonTest_Click(object sender, EventArgs e)
    {
        controller.OpenNewSpreadsheet();
    }

    public DockPanel DockPanel
    {
        get { return dockPanel; }
    }
}

在哪里

public interface IApplicationShellView
{
    void InitializeView();
    DockPanel DockPanel { get; }
}

ApplicationShellController.cs

public class ApplicationShellController
{
    private IApplicationShellView shellView;

    public ApplicationShellController(IApplicationShellView view)
    {
        this.shellView = view;
    }

    public void OpenNewSpreadsheet(DockState dockState = DockState.Document)
    {
        SpreadsheetController controller = (SpreadsheetController)GetDocumentController("new.xlsx");
        SpreadsheetView view = (SpreadsheetView)controller.New("new.xlsx");
        view.Show(shellView.DockPanel, dockState);
    }

    private IDocumentController GetDocumentController(string path)
    {
        return CompositionRoot.ResolveAll<IDocumentController>()
            .SingleOrDefault(provider => provider.Handles(path));
    }

    public IApplicationShellView ShellView { get { return shellView; } }
}

SpreadsheetController.cs

public class SpreadsheetController : IDocumentController 
{
    private ISpreadsheetView view;

    public SpreadsheetController(ISpreadsheetView view)
    {
        this.view = view;
        this.view.SetController(this);
    }

    public bool Handles(string path)
    {
        string extension = Path.GetExtension(path);
        if (!String.IsNullOrEmpty(extension))
        {
            if (FileTypes.Any(ft => ft.FileExtension.CompareNoCase(extension)))
                return true;
        }
        return false;
    }

    public void SetViewActive(bool isActive)
    {
        ((SpreadsheetView)view).ShowIcon = isActive;
    }

    public IDocumentView New(string fileName)
    {
        // Opens a new file correctly.
    }

    public IDocumentView Open(string path)
    {
        // Opens an Excel file correctly.
    }

    public IEnumerable<DocumentFileType> FileTypes
    {
        get
        {
            return new List<DocumentFileType>()
            {
                new DocumentFileType("CSV",  ".csv" ),
                new DocumentFileType("Excel", ".xls"),
                new DocumentFileType("Excel10", ".xlsx")
            };
        }
    }
}

实现的接口在哪里

public interface IDocumentController
{
    bool Handles(string path);

    void SetViewActive(bool isActive);

    IDocumentView New(string fileName);

    IDocumentView Open(string path);

    IEnumerable<DocumentFileType> FileTypes { get; }
}

现在与这个控制器相关的视图是

public partial class SpreadsheetView : DockContent, ISpreadsheetView
{
    private IDocumentController controller;

    public SpreadsheetView()
    {
        InitializeComponent();
    }

    private void SpreadsheetView_Activated(object sender, EventArgs e)
    {
        controller.SetViewActive(true);
    }

    private void SpreadsheetView_Deactivate(object sender, EventArgs e)
    {
        controller.SetViewActive(false);
    }

    public void SetController(IDocumentController controller)
    {
        this.controller = controller;
        Log.Trace("SpreadsheetView.SetController(): Controller set successfully");
    }

    public string DisplayName
    {
        get { return Text; }
        set { Text = value; }
    }

    public WorkbookView WorkbookView
    {
        get { return workbookView; }
        set { workbookView = value; }
    }
    ...
}

最后是视图界面

public interface ISpreadsheetView : IDocumentView
{
    WorkbookView WorkbookView { get; set; } 
}

public interface IDocumentView
{
    void SetController(IDocumentController controller);

    string DisplayName { get; set; }

    bool StatusBarVisible { get; set; }
}

现在回答我的问题。在 Seemann 的“.NET 中的依赖注入”一书中,他谈到了“三个调用模式”,这就是我在上面尝试实现的。代码有效,shell 视图显示并通过 MVC 模式我的控制器正确打开视图等。但是,我很困惑,因为上面肯定有“服务定位器反模式”的味道。在 Seemann 的书的第 3 章中,他指出

COMPOSITION ROOT 模式描述了应该在哪里使用 DI CONTAINER。然而, 它没有说明如何使用它。 REGISTER RESOLVE RELEASE 模式地址 这个问题 [...] A DI CONTAINER 应该连续三个使用 称为注册​​、解决和释放的阶段。

在其纯粹的形式中,REGISTER RESOLVE RELEASE 模式声明您应该只 在每个阶段进行一次方法调用。 Krzysztof Kozimic 将此称为三调用模式。

在单个方法调用中配置 DI CONTAINER 需要更多解释。这 组件注册应该在单个方法调用中进行的原因是 因为您应该将 DI CONTAINER 的配置视为单个原子操作。 配置完成后,该容器应被视为只读。

这听起来像是疏通的“服务定位器”,为什么这不被视为服务位置?


为了调整我的代码改为使用构造函数注入,我将入口代码更改为

[STAThread]
static void Main()
{
    var kernel = new StandardKernel();
    kernel.Bind(t => t.FromThisAssembly()
                      .SelectAllClasses()
                      .BindAllInterfaces());

    FileLogHandler fileLogHandler = new FileLogHandler(Utils.GetLogFilePath());
    Log.LogHandler = fileLogHandler;
    Log.Trace("Program.Main(): Logging initialized");

    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(kernel.Get<ApplicationShellView>());
}

使用Ninject.Extensions.Conventions,然后我更改了ApplicationShellController,以更正我的代码以通过ctor注入注入IDocumentControllers:

public class ApplicationShellController
{
    private IApplicationShellView shellView;
    private IEnumerable<IDocumentController> controllers; 

    public ApplicationShellController(IApplicationShellView shellView, IEnumerable<IDocumentController> controllers)
    {
        this.shellView = shellView;
        this.controllers = controllers;
        Log.Trace("ApplicationShellController.Ctor(): Shell initialized successfully"); 
    }
    ...
}

在哪里

public class SpreadsheetController : IDocumentController 
{
    private ISpreadsheetView view;

    public SpreadsheetController(ISpreadsheetView view)
    {
        this.view = view;
        this.view.SetController(this);
    }
    ...
}

但这会导致循环依赖,我该如何处理呢?

问题总结:

  1. 为什么我最初使用“Thee Calls Pattern”和CompositionRoot.Resolve&lt;T&gt;() 使用 Ninject 与服务定位器反模式不好或不同?
  2. 如果我想切换到纯 ctor 注入,如何解决上述循环依赖问题?

非常感谢您的宝贵时间。

【问题讨论】:

    标签: c# winforms model-view-controller dependency-injection ninject


    【解决方案1】:

    在此过程中的某个时刻,您必须使用服务位置。但是,DI 和 SL 之间的区别在于,在 SL 中,您在请求服务时解析服务,而在 DI 中,您在某种工厂(例如控制器工厂)中解析它们,然后构造您的对象并传递参考文献。

    您应该创建某种基础设施来分派您的命令并使用某种工厂来定位所创建对象所使用的依赖项。

    这样,您的代码的其余部分没有依赖解析,并且除了在构造点之外,您都遵循 DI 模式。

    【讨论】:

    • 您能否提供示例或参考“控制器工厂”的示例?非常感谢您的宝贵时间...
    • @Killercam - 我没有看到任何成功使用 DI 的 WinForms 代码,但您可以将 asp.net MVC 框架作为示例(尽管许多人不喜欢“符合容器”的方法MVC6 使用)。
    • 但是为什么在 Seemann 的书中他谈到了三个调用模式,就好像它被用作 DI 容器“解析”方法的包装器一样?这似乎在大多数情况下都可以使用,服务位置...这是本章的一个方面,我不喜欢...感谢您的时间。
    猜你喜欢
    • 2015-03-07
    • 2016-08-06
    • 1970-01-01
    • 1970-01-01
    • 2011-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多