0

這些項目計劃以多平臺爲目標,所以我最大限度地利用了類庫中的代碼,以便可以輕鬆地重用它。如何按照約定正確綁定類型?

該架構基於Model-View-Presenter原理。

項目結構如下:

Solution 
    -> Domain 
    -> Domain.Tests 
    -> MVP 
    -> MVP.Tests 
    -> Web 
    -> Web.Tests 
    -> Windows 
    -> Windows.Tests 

MVP

這個項目包含了主持人和項目的意見。例如:

public interface IApplicationView : IView, IHasUiHandler<IApplicationUiHandler> { 
} 

public class ApplicationPresenter : Presenter<IApplicationView> 
    , IApplicationUiHAndler { 
    public ApplicationPresenter(IApplicationView view) : base(view) { 
     View.Handler = this; 
    } 
} 

的Windows

該項目包含應用程序的WPF圖形用戶界面,和所謂的組成根。例如:

public class ApplicationWindow : Window, IApplicationView { 
} 

public class App : Application { 
    protected override void OnStartUp(StartupEventArgs e) { 
     IKernel kernel = new StandardKernel(); 
     kernel.Bind(x => x 
      .FromThisAssembly() 
      .SelectAllClasses().EndingWith("Window") 
      .BindAllInterfaces().EndingWith("View") // Here's what I'd like to do. 
     ); 
    } 
} 

網絡

該項目包含ASP.NET網頁GUI的應用程序,即所謂的組成根。

public class ApplicationPage : Page, IApplicationView { 
} 

public class MvcApplication : HttpApplication { 
    protected override void Application_Start() { 
     IKernel kernel = new StandardKernel(); 
     kernel.Bind(x => x 
      .FromThisAssembly() 
      .SelectAllClasses().EndingWith("Page") 
      .BindAllInterfaces().EndingWith("View") // Here's what I'd like to do. 
    } 
} 

好吧,我想你的想法...

我是很新的依賴注入,甚至在常規更新綁定。

我想知道如何使用約定與Ninject配置綁定。

任何想法如何將這些視圖與Windows(WPF)和Pages(Web)綁定?

編輯

試圖@BatteryBackupUnit建議是什麼之後,我想我的問題是關於我的搜索組件。

using (var kernel = new StandardKernel()) 
    kernel.Bind(scanner => scanner 
     .From(AppDomain.CurrentDomain 
      .GetAssemblies() 
      .Where(a => (a.FullName.Contains("MyProject.MVP") 
       || a.FullName.Contains("Windows")) 
       && !a.FullName.Contains("Tests"))) 
     .SelectAllClasses() 
     .EndingWith("Window") 
     .BindSelection((type, baseType) => 
      type.GetInterfaces().Where(iface => iface.Name.EndsWith("View")))); 

如前所述,所述View接口不位於相同的組件作爲Window類。上面的代碼基於@ BatteryBackupUnit的答案,效果很好。

回答

1

如何:

using FluentAssertions; 
using Ninject; 
using Ninject.Extensions.Conventions; 
using System.Linq; 
using Xunit; 

public interface ISomeView { } 
public interface ISomeOtherView { } 
public interface INotEndingWithViewWord { } 

public class SomePage : ISomeView, ISomeOtherView, INotEndingWithViewWord 
{ 
} 

public class Demo 
{ 
    [Fact] 
    public void Test() 
    { 
     using (var kernel = new StandardKernel()) 
     { 
      kernel.Bind(x => x 
       .FromThisAssembly() 
       .SelectAllClasses() 
       .EndingWith("Page") 
       .BindSelection((type, baseType) => 
        type.GetInterfaces() 
        .Where(iface => iface.Name.EndsWith("View")))); 

      kernel.Get<ISomeView>().Should().BeOfType<SomePage>(); 

      kernel.Get<ISomeOtherView>().Should().BeOfType<SomePage>(); 

      kernel.Invoking(x => x.Get<INotEndingWithViewWord>()) 
       .ShouldThrow<ActivationException>(); 
     } 
    } 
} 

注:我一直在使用的NuGet包

  • Ninject
  • Ninject.Extensions.Conventions
  • xunit.net
  • FluentAssertions

那些xunit。net和FluentAssertions只是在那裏運行測試而不是在生產中使用。您可以使用.BindWith<T : IBindingGenerator>.BindUsingRegex(...)

+0

從哪裏可以得到''應該''方法'kernel.Get ()'? =) –

+0

'應該'是Fluent斷言(nuget包)的一部分。我也用xunit.net來運行測試。而且你不需要生產代碼;-) – BatteryBackupUnit

+0

我想不是。另外,我得到一個'ActivationException',說我的視圖沒有被綁定,而且這個類型不是可自我綁定的。我想這是由於我搜索我的程序集的方式。請參閱我的代碼更新。 –