如何松散地引用Prism中的模块以便它们能够存在还是不存在?

Edw*_*uay 2 wpf containers prism inversion-of-control unity-container

这个stackoverflow问题中,我了解到Prism/Unity并没有像我想象的那样解耦,例如,如果我有这个类将menuManager注入到它的构造函数中,那么我必须确保这个类实际存在于某个地方(我认为你可以只需拉出包含类的.dll,容器就会处理它,例如在其位置注入null):

public class EmployeesPresenter
{
    public EmployeesPresenter(IMenuManager menuManager)
    {

    }
}
Run Code Online (Sandbox Code Playgroud)

但是我可以解决这个问题:应用程序无法在没有MenuModule的情况下运行(或者根据建议我可以使用NullMenuModule,它只会阻止应用程序中断).

但是,我正在构建的应用程序将在MenuModule中有一个MenuManager类,并且每个模块都必须使用MenuManager在菜单中注册它想要的所有内容.但是,我希望能够换出MenuModule,例如有一个InfragisticsMenuModule,并有一个TelerikMenuModule等.

但是,当我在例如CustomersModule中时,为了使用TelerikMenuModule,我需要引用它.当我想使用InfragisticsMenuModule时,我需要引用它.

那么我如何能够与InfragisticsMenuModule"热交换"TelerikMenuModule而无需使用新引用重新编译所有模块,例如我想要替换它:

Application.exe
Customers.dll
TelerikMenuModule.dll
Run Code Online (Sandbox Code Playgroud)

有了这个:

Application.exe
Customers.dll
InfragisticsMenuModule.dll
Run Code Online (Sandbox Code Playgroud)

并且只需能够重新启动应用程序并使用新的InfragisticsMenuModule.dll运行,并且不会抱怨TelerikMenuModule.dll不再存在.

Dan*_*ker 5

这就是接口的用武之地.你需要这样的东西:

public interface IMenuSystem
{
    // whatever operations need to be offered by a menu system
}
Run Code Online (Sandbox Code Playgroud)

Application.exe并且Customers.dll可能只涉及该界面.他们不允许了解具体的实施.

然后,您将使用配置步骤(调用Register...方法或使用配置文件)来指定将提供实现的类型MenuSystem.

  • 这是正确的技术.在Shell中,使用实现IMenuSystem的容器注册对象."客户端"模块只是声明他们需要一个IMenuSystem,而你的shell将注入一个了解Infragistics或了解Telerik的实现.您应该只需要每个模块引用仅包含接口定义的"Contracts"DLL.这是100%正确的答案.我们在自己的项目中这样做,效果很好. (2认同)