Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [e4-dev] More Questions about DI

Hi Thomas.

On 22-Feb-2011, at 7:18 AM, Thomas Kratz wrote:
> My next question is: at Login time I do load a User Object graph with some rights stuff in Collections (Its a pojo). How would I provide this Object through the EclipseContext so I can get it injected in my views/editors?

The approach I use is to use an OSGi service that acts as a creation function.  The service first checks for a known value and if set, simply returns that value.  Otherwise it populates the item and sets the value in the context.  I make the object available under its class and interface names.  (I defined OSGi services for the "well-known" interface names too that simply use this same creation function.)

   public class RepositoryCreationFunction extends ContextFunction {
      protected static final String REPOSITORY = "kizby.repository";	// $NON-NLS-1$
	
      @Inject protected MApplication app;

      @Override
      public Object compute(IEclipseContext context) {
         if(context.get(REPOSITORY) != null) { return context.get(REPOSITORY); }
         Repository repo = openRepo(context);
         assert repo != null;
         context.set(REPOSITORY, repo);
         context.set(repo.getClass().getName(), repo);
         registerInterfaces(context, repo.getClass().getInterfaces(), repo);
         return repo;
      }

      private void registerInterfaces(IEclipseContext context, 
            Class<?>[] clazzes, RepositoryFacade facade) {
         for(Class<?> intf : clazzes) {
            context.set(intf.getName(), facade);
            registerInterfaces(context, intf.getInterfaces(), facade);
         }		
      }

      private Repository openRepo(IEclipseContext context) {
         //...
      }
   }

I define the service like the following:

   <?xml version="1.0" encoding="UTF-8"?>
   <scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0";
	name="ca.mt.kizby.repository.facade"> 
      <implementation class="ca.mt.kizby.ui.e4.RepositoryCreationFunction"/>
      <service>
         <provide interface="org.eclipse.e4.core.contexts.IContextFunction"/>
      </service>
      <property name="service.context.key" type="String"
          value="ca.mt.kizby.core.Repository" />
   </scr:component>


Consumers simply specify an injectable instance of Repository.

> Next question is: I do use some services in my EditorInputs (they are lazyly fetching content from the backend). I think to get the services injected there I would need to do some stuff. I have a central extensible factory where the inputs get created, could I use the DenedencyInjector at this time to get my services there ?

If you can get ahold of an IEclipseContext, you can use org.eclipse.e4.core.contexts.ContextInjectionFactory to either create an object or inject into an existing object.  Beware that if you #inject, you are responsible for calling #uninject.

Hope that helps.

Brian.

--
Rescue your task list with Kizby!  Free 30 day trial with no obligations.
Visit kizby.com for details.



Back to the top