I'm a huge fan of using Unity for .NET applications to keep code module and dependencies "manageable" in large code-bases. I've used it for years with .NET by dropping assemblies into the main program's program directory and re-configuring an IoC configuration file (usually the .exe.config file), injecting new behaviour into a program without needing to re-compile the main program.
我非常喜欢使用Unity for .NET应用程序来保持代码模块和依赖关系在大型代码库中“易于管理”。通过将程序集放入主程序的程序目录并重新配置IoC配置文件(通常是.exe.config文件),我已经将它用于.NET多年,将新行为注入程序而无需重新编译主程序。
However, I have started my foray into writing universal apps for Windows Store and more importantly, Windows IoT - however because these apps are "packaged up" during compilation, I'm finding it difficult to understand how best to continue doing this.
但是,我开始尝试为Windows Store编写通用应用程序,更重要的是,Windows IoT - 但是因为这些应用程序在编译期间被“打包”,我发现很难理解如何最好地继续这样做。
One possible option would be to compile the "modules" (PCL/Universal class libraries) and include them (and their dependencies) as "Content" files within the Universal app, then use reflection to load types from these assemblies during the start-up of the program via reflection.
一种可能的选择是编译“模块”(PCL / Universal类库)并将它们(及其依赖项)包含在Universal应用程序中的“Content”文件中,然后在启动期间使用反射从这些程序集中加载类型通过反思的程序。
Another option would be to include all modules and their dependencies into the main program's project references before compilation but using "poor man's dependency injection to hard code the registrations and resolutions", but this feels really wrong to me - I'd ideally like to keep the separation of dependencies if at all possible?
另一个选择是在编译之前将所有模块及其依赖项包含到主程序的项目引用中,但使用“穷人的依赖注入来硬编码注册和解决方案”,但这对我来说真的是错的 - 我理想地想保持如果可能的话,分离依赖关系?
Any ideas...?
PS. I'm not tied to Unity in any way, if there is another IoC library that does it better on this platform then fine - but Unity is what I already know and love!
PS。我没有以任何方式与Unity联系,如果有另一个IoC库在这个平台上做得更好那么好 - 但Unity是我已经知道和喜欢的!
1 个解决方案
#1
I do this using Castle Windsor. You can probably use the same approach with Unity:
我这样做是使用Castle Windsor。您可以使用与Unity相同的方法:
- Limit references only to when an assembly needs a type defined in another assembly
- Define a class (or classes depending on how you want to organize) for IoC configuration in each assembly by implementing IWindsorInstaller and configure DI using the fluent API inside the implemented
Install
method. - Define a container instance inside your app root
- At application start, use reflection to find all types implementing IWindsorInstaller in the bin folder and run their
Install
method, passing in the container instance
仅限于程序集需要在另一个程序集中定义的类型时的引用
通过实现IWindsorInstaller并使用实现的Install方法中的fluent API配置DI,为每个程序集中的IoC配置定义一个类(或类,具体取决于您希望如何组织)。
在应用程序根目录中定义容器实例
在应用程序启动时,使用反射来查找在bin文件夹中实现IWindsorInstaller的所有类型并运行它们的Install方法,传入容器实例
This way you can keep your dependencies separate, and you don't have to worry about adding extra references or adding .dlls as content files.
这样,您可以将依赖项分开,并且不必担心添加额外的引用或将.dll添加为内容文件。
#1
I do this using Castle Windsor. You can probably use the same approach with Unity:
我这样做是使用Castle Windsor。您可以使用与Unity相同的方法:
- Limit references only to when an assembly needs a type defined in another assembly
- Define a class (or classes depending on how you want to organize) for IoC configuration in each assembly by implementing IWindsorInstaller and configure DI using the fluent API inside the implemented
Install
method. - Define a container instance inside your app root
- At application start, use reflection to find all types implementing IWindsorInstaller in the bin folder and run their
Install
method, passing in the container instance
仅限于程序集需要在另一个程序集中定义的类型时的引用
通过实现IWindsorInstaller并使用实现的Install方法中的fluent API配置DI,为每个程序集中的IoC配置定义一个类(或类,具体取决于您希望如何组织)。
在应用程序根目录中定义容器实例
在应用程序启动时,使用反射来查找在bin文件夹中实现IWindsorInstaller的所有类型并运行它们的Install方法,传入容器实例
This way you can keep your dependencies separate, and you don't have to worry about adding extra references or adding .dlls as content files.
这样,您可以将依赖项分开,并且不必担心添加额外的引用或将.dll添加为内容文件。