PicoContainer
  1. PicoContainer
  2. PICO-45

Add a registerComponentInstanceIfNotNull convenience method for optional deps

    Details

    • Type: Improvement Improvement
    • Status: Closed Closed
    • Priority: Trivial Trivial
    • Resolution: Cannot Reproduce
    • Affects Version/s: 1.0-alpha-2, 1.0-beta-1, 1.0-beta-2, (2)
      1.0-beta-3, 1.0-beta-5
    • Fix Version/s: None
    • Component/s: PicoContainer (Java)
    • Labels:
      None
    • Number of attachments :
      0

      Description

      Suppose I have CompA, CompB (depends on CompBConf).
      CompA depends optionaly on CompB. If i don't register a CompBConf instance, i don't want CompB to be available, then a CompA less greedy constructor will be called and its member compB ref will be null.

      To avoid registering a CompBConf I have to test explicitely if the method that creates this instance returns null or not.

      It would be useful then to have an additional method on picocontainer to avoid every null checking :
      registerComponentInstanceIfNotNull(Object obj)

        Activity

        Hide
        Aslak Hellesøy added a comment -

        Is this right?

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

        class B {
        public B(C c) {}
        }

        class C {
        public C() {}
        }

        I don't quite understand what you want. Both of these should work:

        a)
        // A without B
        pico.registerComponentImplementation(A.class);
        A a = (A) pico.getComponentInstance(A.class);

        b)
        // A with B
        pico.registerComponentImplementation(A.class);
        pico.registerComponentImplementation(B.class);
        pico.registerComponentImplementation(C.class);
        A a = (A) pico.getComponentInstance(A.class);

        Remember that we discourage registering instances (objects). The recommended way is to register classes and have pico instantiate them.

        Show
        Aslak Hellesøy added a comment - Is this right? class A { public A() {} public A(B b) {} } class B { public B(C c) {} } class C { public C() {} } I don't quite understand what you want. Both of these should work: a) // A without B pico.registerComponentImplementation(A.class); A a = (A) pico.getComponentInstance(A.class); b) // A with B pico.registerComponentImplementation(A.class); pico.registerComponentImplementation(B.class); pico.registerComponentImplementation(C.class); A a = (A) pico.getComponentInstance(A.class); Remember that we discourage registering instances (objects). The recommended way is to register classes and have pico instantiate them.
        Hide
        Aslak Hellesøy added a comment -

        Feel free to reopen this if you have more information.

        Show
        Aslak Hellesøy added a comment - Feel free to reopen this if you have more information.
        Aslak Hellesøy made changes -
        Field Original Value New Value
        Status Unassigned [ 1 ] Closed [ 6 ]
        Resolution Cannot Reproduce [ 5 ]

          People

          • Assignee:
            Unassigned
            Reporter:
            Gilles Philippart
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Time Tracking

              Estimated:
              Original Estimate - 1 minute
              1m
              Remaining:
              Remaining Estimate - 1 minute
              1m
              Logged:
              Time Spent - Not Specified
              Not Specified