开发者

Constructor injection in application where dependencies might not be able to be initialized

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.


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.


See how I would do it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜