简体   繁体   中英

Getting static data out of a container

I currently have a Cache object (which is a wrapper around a dictionary) implemented as a Singleton.

I have just been reading this article regarding the Singleton pattern: http://ayende.com/blog/159426/design-patterns-in-the-test-of-time-singleton?key=e4eedc32-f43f-44e9-966d-90a6ff792ddf

And it got me thinking that perhaps I should leverage my IoC container instead of having a Singleton. As I'm finding it very hard to test parts of the code that access the Singleton.

So the questions I have are:

Is the Singleton lifestyle in Castle (or any IoC container for that matter) the same as having a hand-rolled GoF Singleton?

This creates the slighly weird situation that I

  1. register all my dependancies in the container,
  2. then resolve some kind of bootstrapper which populates the Cache,
  3. then add the cache as another registration to the container

is that a legitimate way to initialise the container? It seems a little odd to not have all the registrations done at the same time

Singleton is about having only one instance of object, which is achieved by dependency-injection framework in this case. Singleton is not about time of initialization. You can create singleton and initialize/modify it later. And you can pass reference to your singleton to other objects at any time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM