Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
868 views
in Technique[技术] by (71.8m points)

windows phone 7 - Does the MVVM Light SimpleIoC support singletons?

I'm using SterlingDB in my current Windows Phone project, and I would like to be able to resolve the Sterling database from various places in my application using the new SimpleIoC container in MVVM Light v4.

However, I'm not sure if SimpleIoC supports registering singletons. The SterlingDB engine should only be created when the app first launches, and I don't want to be spinning up new instances every time the container injects a reference to it.

If there's a different way I should be thinking about this problem, I'd be glad to entertain alternatives as well.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

SimpleIoc returns instances based on a key that you pass to it. If you call GetInstance() without a key, you will always get the default instance of your object. The instance is only created when you call GetInstance the first time (lazy creation). If you call GetInstance with a key, I look up if this named instance already exists in the registry. If it doesn't yet, I create it and then I return it. If there is an instance with that key already, I just return it.

In the alpha version (BL16 MIX edition), there is a bug that caused Register to create the instance too early, when a key was used. This bug is fixes in V4 beta1 which I will publish this week.

So as you see you will get the same instance from SimpleIoc if you always use the same key (or simply the default instance if you don't use a key at all).

Does it make sense? Laurent


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...