使用我的引导加载程序/Unity进行循环引用

发布于 2024-11-27 00:24:20 字数 910 浏览 5 评论 0原文

我的 Unity 和引导加载程序存在严重问题。这是一个循环引用。

让我解释一下我目前所拥有的:

主应用程序(winforms)

ServiceLayer - C# 项目

DependencyInjection - 容器的 C# 项目

Bootloader - 用于引导加载程序的 C# 项目,用于为 automapper 和 Unity 进行映射。

现在,装书器已经引用了所有内容,即服务、容器等 mainApp (winforms),并且它的工作做得很好。

问题是我从我的 MainApp (winforms) 启动引导加载程序,如下所示

 DI.Container.RegisterType<IBootstrapper, Bootstrapper>(
    new InjectionConstructor(DI.Container));

            var bootstrapper = DI.Container.Resolve<IBootstrapper>();

            bootstrapper.StartUp();

现在问题如下,MainApp 需要对 Bootloader 的引用,因为那是类和接口所在的位置 - 但我不能,因为 Bootloader 有一个引用到 MainApp,因为引导加载程序会注册 mainapp 中的一些内容。

我想我在某个地方感到困惑,主要思想是将引导程序从主应用程序移出到它自己的程序集中,并且容器也有自己的程序集;引导程序需要它的所有引用,包括对 MainApp 的引用,但当然 Mainapp 需要对引导程序的引用来创建它并启动它。

编辑

我当然可以通过直接访问容器来注册主应用程序的类型,但这是否会破坏让我的引导加载程序处理一切的目标?

I have a serious problem with my Unity and bootloader. It's a circular reference.

Let me explain what I have currently:

MainApp (winforms)

ServiceLayer - C# project

DependencyInjection - C# Project for the Container

Bootloader - C# project for the bootloader to do mappings for automapper and Unity.

Now the the bookloader has references to everything i.e. The service, the container etc the mainApp (winforms) and it does its job just great.

The problem is that I am starting the bootloader from my MainApp (winforms) like so

 DI.Container.RegisterType<IBootstrapper, Bootstrapper>(
    new InjectionConstructor(DI.Container));

            var bootstrapper = DI.Container.Resolve<IBootstrapper>();

            bootstrapper.StartUp();

Now the problem here is the following, MainApp needs a reference to Bootloader because that's where the class and interface is - but I can't because Bootloader has a reference to the MainApp as the bootloader registers some stuff that is in the mainapp.

I think I am getting confused somewhere, the main idea was to move the bootstrapper out of the mainapp into its own assembly and the Container has its own assembly too; the bootstrapper needs all its references including to MainApp but of course Mainapp needs a reference to bootstrapper to create it and start it up.

Edit

I could of course register the types from the mainapp by accessing the container directly but doesn't this defeat the object of having my bootloader, which takes care of everything?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓝色星空 2024-12-04 00:24:20

我不是这个主题的专家,我找到你的帖子的原因是因为我正在寻求类似问题的帮助,但我认为我们的问题之间可能存在一些重叠,也许我到目前为止尝试过的东西也许能够帮助你,或者为你指明一个可以让你取得一些进步的方向。

如果您想查看我写的所有内容,包括一些图表,您可以在这里找到:
使用 StructureMap 是这些项目组织中更好的一个比另一个?

如果有更好的答案,希望有人指出,我也会从中学习。

基本上,我认为您有几个选择。

您可以将引导加载程序和 MainApp 合并到同一个项目中。 (正如您所说,这似乎是“错误的”)

您可以将 BootLoader 所需的代码从 MainApp 移动到另一个程序集中,然后从 BootLoader 和 MainApp 引用它。

您可以从 MainApp 中删除对 BootLoader 的引用,然后查看是否有其他方法可以使用 Unity 在运行时自动获取对 BootLoader 的引用。我正在使用 StructureMap,它有一个功能,您可以告诉它扫描一些程序集(在应用程序启动时),然后您可以告诉它应用“默认约定”,这意味着如果有一个名为 IWhatever 的接口和一个类Whatever:IWhatever,然后它会自动设置它,因此询问 IWhatever 的实例将为您提供 Whatever 的具体实例。

这样,您就可以让 BootLoader 在编译时引用 MainApp,但 MainApp 中仍然有可以在运行时访问 BootLoader 中内容的代码(通过使用 IOC 并通过接口)。

我希望这有帮助。

I'm not an expert on this topic, the reason I found your post was because I was looking for help with a similar issue, but I think maybe there is some overlap between our problems, and maybe the stuff I've tried so far might be able to help you, or point you in a direction that can get you some progress.

If you want to see all the stuff I wrote, including some diagrams, you can find it here:
Using StructureMap, is one of these project organizations better than another?

If there is a better answer, hopefully someone will point it out and I'll learn from this as well.

Basically, I think you have a couple options.

You can merge your bootloader and MainApp into the same project. (as you said, that seems "wrong")

You can move the code from MainApp that is needed by BootLoader into another assembly, and then reference it from both BootLoader and MainApp.

You can remove your reference from MainApp to BootLoader, and then use see if there are other ways to automatically get a reference to BootLoader at runtime using Unity. I'm using StructureMap, which has a feature where you tell it to scan some assemblies (at app startup), and then you can tell it to apply "default conventions", which means if there is an interface called IWhatever, and a class Whatever:IWhatever, then it will automatically set it up so asking for an instance of IWhatever will give you a concrete instance of Whatever.

So this way, you can have BootLoader reference MainApp at compile time, but still have code in MainApp that can access stuff in BootLoader at runtime (by using IOC and going through an interface).

I hope this helps.

不乱于心 2024-12-04 00:24:20

为什么不将 DependencyInjection 项目设置为启动项目,在其中编写 Main 方法,然后从那里调用引导加载程序?唯一显着的变化是您需要将 DI 项目设为 EXE 而不是 DLL,对于 UI 项目反之亦然。

Why not set your DependencyInjection project as the startup project, write a Main method in that, and then call your bootloader from there? The only significant change is that you'd need to make the DI project an EXE instead of a DLL, and vice versa for the UI project.

梦途 2024-12-04 00:24:20

这可能是一个设计问题。另一个间接层怎么样? :)

但说真的:你的引导加载程序在主应用程序中使用什么?是否不可能将其移至其自己的程序集中?

其他看起来很奇怪的事情是主应用程序和引导加载程序之间的关系。它们看起来相当紧密耦合,那么引导加载程序有什么作用呢?是否不能简单地将引导加载程序类放在主应用程序中?

只需要了解有关您的设置的更多信息即可。

This may be a design issue. How about another layer of indirection? :)

But seriously: what is your bootloader using in the main app? Is it not possible to move that out into it's own assembly perhaps?

Something else that looks weird is the relationship between the main app and the bootloader. They seem rather tightly coupled so what purpose does the bootloader serve? Is it not possible to simply place the bootloader class in the main app?

Just need a touch more info on your setup.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文