1

Using the example from afBedSheet's documentation:

using afIoc
using afBedSheet

class HelloPage {
  Text hello(Str name, Int iq := 666) {
    return Text.fromPlain("Hello! I'm $name and I have an IQ of $iq!")
  }
}

class AppModule {
  @Contribute { serviceType=Routes# }
  static Void contributeRoutes(Configuration conf) {
    conf.add(Route(`/index`, Text.fromPlain("Welcome to BedSheet!")))
    conf.add(Route(`/hello/**`, HelloPage#hello))
  }
}

class Example {
  Int main() {
    return afBedSheet::Main().main([AppModule#.qname, "8080"])
  }
}

If I add this line as the first line of the main() method in the Example class

    registry := IocService([AppModule#]).start.registry

The afBedSheet application won't start and throws:

[21:40:10 11-Aug-14] [info] [afIoc] Adding module definition for Example_0::AppModule
[21:40:10 11-Aug-14] [info] [afIoc] Starting IOC...
[21:40:10 11-Aug-14] [err] [afIoc] Err starting IOC
  afIoc::IocErr: Service does not exist for Type 'afBedSheet::Routes' defined in contribution method Example_0::AppModule.contributeRoutes.
Ioc Operation Trace:
  [ 1] Building IoC Registry
  [ 2] Validating contribution definitions
Stack Trace:
    afIoc::Utils.stackTraceFilter (Utils.fan:45)
    afIoc::RegistryBuilder.build (RegistryBuilder.fan:108)
    afIoc::IocService.onStart (IocService.fan:76)
    fan.sys.Service$.start (Service$.java:206)
    afIoc::IocService.start (IocService.fan:10)
    Example_0::Example.main (/Users/coder/Documents/temp/testBedSheet/Example.fan:20)
    java.lang.reflect.Method.invoke (Method.java:483)
    fan.sys.Method.invoke (Method.java:559)
    fan.sys.Method$MethodFunc.callOn (Method.java:230)
    fan.sys.Method.callOn (Method.java:139)
    fanx.tools.Fan.callMain (Fan.java:175)
    fanx.tools.Fan.executeFile (Fan.java:98)
    fanx.tools.Fan.execute (Fan.java:37)
    fanx.tools.Fan.run (Fan.java:298)
    fanx.tools.Fan.main (Fan.java:336)
ERROR: fan.afIoc.IocService.onStart

How can I access the IocService of the afBedSheet app to get bound services?

LightDye
  • 1,174
  • 8
  • 14

1 Answers1

1

There is no need to create your own IoC Registry as BedSheet creates one for you.

Also, there should not be any need to access the registry outside of the web application.

If you need something to happen when the web app starts, contribute a func / closure to RegistryStartup:

class AppModule {
    @Contribute { serviceType=RegistryStartup# }
    static Void contributeRegistryStartup(Configuration conf, MyService myService) {
        conf.add |->| { 
            myService.soSomething() 
        }
    }
}

To access during a web request, @Inject services into your handlers:

class HelloPage {
  @Inject MyService? myService

  Text hello(Str name, Int iq := 666) {
    myService.doSomething()

    return Text.fromPlain("Hello! I'm $name and I have an IQ of $iq!")
  }
}
Steve Eynon
  • 4,308
  • 1
  • 23
  • 44