Olaf Krische
2009-05-11 17:36:57 UTC
Hello,
i have created a caching container for all my singletons:
MutablePicoContainer cachingContainer;
cachingContainer = new PicoBuilder().withCaching().build();
cachingContainer.addComponent(singletonIF1.class, singletonIF1Impl.class);
...
Now i have certain components which needs "runtime arguments", like a user.
At the moment i do something like this:
public I getInstance(Class impl, User user) {
Parameter[] params = new Parameter[]{new ConstantParameter(user)};
MutablePicoContainer instanceContainer = new
PicoBuilder(cachingContainer).build();
instanceContainer.addComponent(UserProviderIF.class, UserProvider.class,
params);
instanceContainer.addComponent(impl);
return instanceContainer.getComponent(impl);
}
This is pretty annoying and costly, that i have to create a new container at
each method call, because i can add Class only, when i have the user
available. I could have a cache to store the container for each user, but
thats overkill as well.
Is there an alternate way of doing this?
I am close to do something like that:
instanceContainer.addComponent(singletonIF1.class, singletonIF1Impl.class);
instanceContainer.addComponent(UserProviderIF.class.class,
ThreadLocalUserProvider.class);
instanceContainer.addComponent(MyIF.class, MyIFImpl.class);
On each getInstance() i will write the user into a thread local variable,
which will then be read by the ThreadLocalUserProvider, when it is being
instantiated.
But anyhow this does not seem to be very clean. No one can assure me, that
the creationg of the UserProvider instance will happen in the same thread as
the caller.
Does someone have another idea?
Big Thanks in advance!
i have created a caching container for all my singletons:
MutablePicoContainer cachingContainer;
cachingContainer = new PicoBuilder().withCaching().build();
cachingContainer.addComponent(singletonIF1.class, singletonIF1Impl.class);
...
Now i have certain components which needs "runtime arguments", like a user.
At the moment i do something like this:
public I getInstance(Class impl, User user) {
Parameter[] params = new Parameter[]{new ConstantParameter(user)};
MutablePicoContainer instanceContainer = new
PicoBuilder(cachingContainer).build();
instanceContainer.addComponent(UserProviderIF.class, UserProvider.class,
params);
instanceContainer.addComponent(impl);
return instanceContainer.getComponent(impl);
}
This is pretty annoying and costly, that i have to create a new container at
each method call, because i can add Class only, when i have the user
available. I could have a cache to store the container for each user, but
thats overkill as well.
Is there an alternate way of doing this?
I am close to do something like that:
instanceContainer.addComponent(singletonIF1.class, singletonIF1Impl.class);
instanceContainer.addComponent(UserProviderIF.class.class,
ThreadLocalUserProvider.class);
instanceContainer.addComponent(MyIF.class, MyIFImpl.class);
On each getInstance() i will write the user into a thread local variable,
which will then be read by the ThreadLocalUserProvider, when it is being
instantiated.
But anyhow this does not seem to be very clean. No one can assure me, that
the creationg of the UserProvider instance will happen in the same thread as
the caller.
Does someone have another idea?
Big Thanks in advance!
--
View this message in context: http://www.nabble.com/scoped-containers...-tp23488025p23488025.html
Sent from the NanoContainer - PicoContainer - Users mailing list archive at Nabble.com.
View this message in context: http://www.nabble.com/scoped-containers...-tp23488025p23488025.html
Sent from the NanoContainer - PicoContainer - Users mailing list archive at Nabble.com.