应用程序中的构造函数注入可能无法初始化依赖项

发布于 2024-09-13 21:53:53 字数 595 浏览 10 评论 0原文

我的应用程序无需数据库连接即可启动,如何通过 IoC 和构造函数注入来处理此问题?

示例:

public class ApplicationShellPresenter(IRepository repository, IView view)
{

}

在这种情况下,当构造 IRepository 时,由于底层 DAL 找不到配置/文件、错误的用户名/密码等,将引发异常。

考虑到这一点,我得出的结论是,我无法将存储库注入到构造函数或注入最终以 IRepository 作为依赖项的任何内容。

我需要在没有 IRepository 依赖的情况下启动,当用户进行正确的数据库设置时,在容器中注册 IRepository。但那时我已经离开了作文根。

编辑:

我的问题实际上并不是 IoC/构造函数注入问题,而是底层 DAL 中的设计缺陷。

我们的 DAL 是在创造的基础上构建自身的。这就是为什么该设计不起作用,因为如果不构建我们的 DAL 引擎,我就无法构建 IRepository 依赖项。

我的简单解决方案是包装我们的 DAL,这样它就不会在创建时自行构建。

My application is able to start without a database connection, how do I handle this with IoC and constructor injection?

Example:

public class ApplicationShellPresenter(IRepository repository, IView view)
{

}

When IRepository in this case will be constructed an exception will be thrown due to underlaying DAL cannot find config/file, wrong username/password etc. etc.

With this in mind I came to the conclusion that I cannot inject the repository in the constructor OR inject anything that eventually along the line have IRepository as dependency.

I need to start without IRepository dependency and when the user have made the correct database settings, register the IRepository in the container. But then I have already left the composition root.

Edit:

My problem was not really a IoC/Constructor injection - problem but rather a design flaw in our underlaying DAL.

Our DAL is constructing itself upon creation. And thats why that design didn't work because I couldn't construct a IRepository dependency without constructing our DAL-engine.

My simple solution was to wrap our DAL so it didn't construct itself upon creation.

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

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

发布评论

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

评论(2

余生再见 2024-09-20 21:53:53

更进一步。如果我理解正确的话,我们有:

Application Starts

User specifies some database settings

Repository initialises, passwords etc are checked

Application now uses repository

我的直接想法是,嗯,想知道我们是否也可以得到

User decides "I don't like that repository"

User specified new database settings

New repository is initialised

Application closes previous repository, and starts working with new one.

现在,这可能比您计划的稍微灵活一些,但它让我想到您实际上有两个应用程序,他们之间的短暂关系。有一个“shell”,它独立于存储库,它是在没有存储库的情况下启动的部分。然后还有一个额外的部分,只有当它有一个存储库时才起作用,我认为它似乎是可插入的,至少在概念上是这样。

因此,我认为您的 shell 与其说是被“注入”,不如说是响应有趣的事件,例如:

Here is a repository, if you already have one please close it

Shut down, please close down your repository, and then yourself.

如果您有一个事件接口,我认为您会得到您需要的东西。事件的到来实际上是一次注入。

Take this a stage further. If I understand you correctly we have:

Application Starts

User specifies some database settings

Repository initialises, passwords etc are checked

Application now uses repository

My immediate thought is, hmmm, wonder whether we can also get

User decides "I don't like that repository"

User specified new database settings

New repository is initialised

Application closes previous repository, and starts working with new one.

Now it may be that this is slightly more flexible than you had planned, but it leads me to thing that you have effectively two applications, with a transient relationship between them. There's the "shell", which is independent of the repository, that's the bit that starts up without a repository. Then there's the extra piece that works only when it's got a repository, and I think is seems to be pluggable, at least in concept.

Hence I don't think your shell is so much being "Injected" as responding to interesting events such as:

Here is a repository, if you already have one please close it

Shut down, please close down your repository, and then yourself.

If you have a Event Interface I think you get what you need. The arrival of an Event is in effect an Injection.

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