无需先注册即可解析类型 - prism 4 和 Untiy
首先我想说我对棱镜、DI 和容器的概念很陌生。我正在查看 Prism 库提供的代码示例之一: 该代码只是将带有“Hello World”字符串(在 TextBlock 元素中)的视图注入到 shell 中的某个区域。
当应用程序启动时,它会创建一个新的 BootStrapper 实例,该实例创建并初始化 shell:
public class Bootstrapper : UnityBootstrapper
{
protected override DependencyObject CreateShell()
{
return Container.Resolve<Shell>();
}
protected override void InitializeShell()
{
base.InitializeShell();
Application.Current.RootVisual = (UIElement)this.Shell;
}
protected override void ConfigureModuleCatalog()
{
base.ConfigureModuleCatalog();
ModuleCatalog moduleCatalog = (ModuleCatalog)this.ModuleCatalog;
moduleCatalog.AddModule(typeof(HelloWorldModule.HelloWorldModule));
}
}
我的问题涉及 CreateShell() 方法。我在提供的代码中找不到任何地方(包括不在配置文件或任何 xaml 文件中...)他们在哪里注册类型 Shell,即使它已注册 - 供应 Shell 类也没有实现任何接口...什么是解析特定类型的意思吗? Shell 实现:
public partial class Shell : UserControl
{
public Shell()
{
InitializeComponent();
}
}
这对我来说看起来很神奇,所以我尝试创建自己的类型 (MyType) 并以相同的方式解析它:
Container.Resolve<MyType>();
通过在 MyType 构造函数中设置断点,我发现它确实解析了 MyType。有人可以向我解释一下它是如何工作的吗?
First of all I would like to remark I am new with the concept of prism, DI and containers. I am looking on one of the code samples provided with the Prism Library:
The code simply injects a view with the "Hello World" string (in a TextBlock element) to a region in the shell.
When the application starts-up, it creates a new BootStrapper instance, which creates and initializes the shell:
public class Bootstrapper : UnityBootstrapper
{
protected override DependencyObject CreateShell()
{
return Container.Resolve<Shell>();
}
protected override void InitializeShell()
{
base.InitializeShell();
Application.Current.RootVisual = (UIElement)this.Shell;
}
protected override void ConfigureModuleCatalog()
{
base.ConfigureModuleCatalog();
ModuleCatalog moduleCatalog = (ModuleCatalog)this.ModuleCatalog;
moduleCatalog.AddModule(typeof(HelloWorldModule.HelloWorldModule));
}
}
My question refers to the method CreateShell(). I couldnt find nowhere in the supplied code (including not in a configuration file or any xaml file...) where do they register the type Shell, and even if it was registered - the supplies Shell class doesnt implement any interface... what is the meaning of resolving a specific type?
the Shell implementation:
public partial class Shell : UserControl
{
public Shell()
{
InitializeComponent();
}
}
This looks like a magic to me, so I tried to create my own type (MyType) and resolve it the same way:
Container.Resolve<MyType>();
By setting a breakepoint inside MyType constructor, I saw that it DID resolved MyType. Can somebody please explain to me how does it work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这几个线程应该回答您的问题:
此外,如果您渴望了解 Unity 如何做到这一点的更多详细信息,只需下载 Unity 2.0 并打开提供的源代码与安装程序。
我希望这有帮助。
谢谢,
达米安
These couple of threads should answer your question:
Additionally, if you are eager to get more detail into how Unity can do this, simple download Unity 2.0 and open the source code that is provided with the installer.
I hope this helps.
Thanks,
Damian
您不需要注册要解析的类型。您需要注册要解析的类型的依赖项。在这种情况下,Shell不需要任何依赖,因此您可以简单地解决它。但举个例子(不是真的),如果您的 shell 将接口 IService 作为参数,那么您必须在解析 Shell 之前注册 IService。
否则,您将收到依赖项解析失败异常。在 Prism 4.1 中,由于 TryResolve,它将被默默吞掉。
You do not need to register a type you want to resolve. You need to register the dependencies of a type, that you want to resolve. In this case, the Shell doesn't need any dependencies, so you can resolve it simply. But for an example (not really), if your shell getting an interface IService as a parameter, then you must register IService, before you resolve Shell.
Otherwise you will get Dependency Resolution Failed Exception. In Prism 4.1 it will be swallowed silently due to TryResolve.