3

Say I have two classes A and B, with B depending on A.

public class A {}

public class B {
    public B(A a) {}
}

It's easy to resolve B in a single PicoContainer.

    final MutablePicoContainer root = new PicoBuilder().build();
    root.addComponent(new A());
    root.addComponent(B.class, B.class);
    System.out.println(root.getComponent(B.class));

But I'd like to have different instances of B for different sessions, with variable instances of A. I'm thinking about something like this.

    // during startup
    final MutablePicoContainer root = new PicoBuilder().build();
    root.addComponent(B.class, B.class);

    // later, initialize sessions
    final MutablePicoContainer session = new PicoBuilder(root)
        .implementedBy(TransientPicoContainer.class)
        .build();
    session.addComponent(new A());

    // some request
    System.out.println(session.getComponent(B.class));

Above code isn't working, because when asking session for a B it asks the parent container root for it. B is found there, but resolved only within root and its parents, leading to an UnsatisfiableDependenciesException.

Is there any good way to make this work? Or is this an anti-pattern, and I'm solving the problem in the wrong way?

Ronald Blaschke
  • 4,036
  • 2
  • 22
  • 16
  • You should better use Spring! It's business proven. So much people have afford with a picoc=>spring migration because pico container was to unstable and buggy... – Martin K. May 12 '09 at 12:47
  • 3
    I disagree. Pico is a *much* better container than Spring's. The advantage in Spring is its community, not its technology. – erickson May 12 '09 at 14:51
  • @Ronald - I don't understand exactly what your goal is. Do you want a new A and a corresponding new B to be created for each session? Why not use your "single container" pattern to register A and B in the session container rather than the root? – erickson May 12 '09 at 14:54
  • @erickson: Yes, a new B for each session, and there can be multiple sessions at the same time. A single container would work, and would arguably be even more correct. But the sessions are rather short lived, so I thought it would be better (CPU and memory wise) if there's a "static" parent and multiple "dynamic" transient session containers. Maybe a proto-container from which each session container is copied would be better, instead of a root/sessions hierarchy that acts like one big container. – Ronald Blaschke May 12 '09 at 15:32

3 Answers3

1

Solving a performance problem that doesn't exist isn't a good approach. Have you done any profiling to verify the problem?

If not, consider doing that first.

Jörn Zaefferer
  • 5,665
  • 3
  • 30
  • 34
1

Did you enable caching on your container (or are you using Pico 1.x)?

Try reading this, maybe disabling the cache is enough to solve this problem.

0

I would register B within the session container as well. Any other dependency of B you can leave in the root container. Assume B has another dependency on C. So you can do the following:

// during startup
final MutablePicoContainer root = new PicoBuilder().build();
root.addComponent(C.class, C.class);

// later, initialize sessions
final MutablePicoContainer session = new PicoBuilder(root)
    .implementedBy(TransientPicoContainer.class)
    .build();
session.addComponent(B.class, B.class);
session.addComponent(new A());

// some request
System.out.println(session.getComponent(B.class));
ordnungswidrig
  • 3,140
  • 1
  • 18
  • 29