简体   繁体   English

DLL 的入口点

[英]Entry Point of a DLL

I've a c# .net WPF application, now i need to register something(basically kernel of NInject IoC pattern) that has been used by the BLL and DAL layer. I've a c# .net WPF application, now i need to register something(basically kernel of NInject IoC pattern) that has been used by the BLL and DAL layer.

I want to know the entry point or something like that for the dll where i could put that code(kernel registration).我想知道dll的入口点或类似的东西,我可以在其中放置该代码(内核注册)。

For WPF section, i use App.xaml.cs , for WCF section i use Global.asax.cs as they are the entry point of these things.对于WPF部分,我使用App.xaml.cs ,对于WCF部分,我使用Global.asax.cs因为它们是这些东西的入口点。 But what about standalone dlls , what is their entry point.但是独立dlls呢,它们的入口点是什么。

One approach is that, i could add a static class in my dll which fulfil this purpose and from app.xaml.cs i call this method of BLL and register my kernels. One approach is that, i could add a static class in my dll which fulfil this purpose and from app.xaml.cs i call this method of BLL and register my kernels. But this seems more like a workaround than approach.但这似乎更像是一种解决方法而不是方法。

Please guide me for something more to the point and logical.请指导我更多的点和合乎逻辑的东西。

Container configuration is done in the composite root of your application (The point where your code is called the first time).容器配置是在应用程序的复合根目录中完成的(代码第一次被调用的地方)。 As you already said, in case of WPF this is the App.xaml.cs.如您所说,在 WPF 的情况下,这是 App.xaml.cs。 Here you register the components of ALL layers.在这里注册所有层的组件。 Preferably you have to UI code in another assembly than the App.xaml.最好您必须在 App.xaml 之外的另一个程序集中编写 UI 代码。 This way the creation of the spplication is completely separated from the execution of the code.这样,spplication 的创建与代码的执行完全分离。

I suggest to read Mark Seemans book where this is described in detail.我建议阅读 Mark Seemans 的书,其中对此进行了详细描述。

C# doesn't allow to run code on assembly loading, and static class constructors are lazily executed on first access to the class. C# 不允许在程序集加载时运行代码,并且 static class 构造函数在第一次访问 ZA2F2F2ED4F8EBC21 时延迟执行。 However the CLR supports a static "assembly constructor", so to speak, which is executed when the assembly is first loaded.然而,CLR 支持 static “程序集构造函数”,可以说,它是在第一次加载程序集时执行的。 Mind you, references are still loaded lazily unless you put in special attributes to mark a referenced assembly to be loaded eagerly.请注意,除非您输入特殊属性来标记要急切加载的引用程序集,否则引用仍然会延迟加载。

If you want you could put a static constructor into the assembly module through ildasm/ilasm.如果您愿意,可以通过 ildasm/ilasm 将 static 构造函数放入组装模块中。 You could probably make some scripts to automate this on build.您可能可以制作一些脚本来在构建时自动执行此操作。

I didn't do this myself yet, so I can't give any examples.我自己还没有这样做,所以我不能举任何例子。 Though if you consider doing it I can maybe dig up some links.虽然如果你考虑这样做,我也许可以挖掘一些链接。

It almost sounds like your wanting a "plug-in" model where the app can dynamically discover components that are available.听起来您几乎想要一个“插件”model,应用程序可以在其中动态发现可用的组件。 If so, then MEF might be a better option.如果是这样,那么MEF可能是一个更好的选择。

MEF seems to work well for cases where the app might not know about all it's dependencies ahead of time. MEF 似乎适用于应用程序可能无法提前了解其所有依赖项的情况。 Dependency injection, on the other hand, assumes that your app is fairly knowledgeable about these dependencies ahead of time.另一方面,依赖注入假定您的应用程序提前对这些依赖关系相当了解。

I don't know if this is what you're after, but it might be worth a look.我不知道这是否是你所追求的,但它可能值得一看。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM