Sorry, I'm trying to understand enough well to develop my application
correctly.
So, imagine I have 2 classes ClassA and ClassB from 2 differents layers.
With the IoC in ClassA I should have just a member named ServiceClassB sab
with its getter and setter. ServiceClassB is the interface ClassB
implements.

Then the IoC should have the role to create the ClassB instance and call
classA.setServiceClassB(classBInstance). But, today in my MVC I have to do
that in ClassA :

public class ClassA {
  @Inject
  ServiceClassB scb;

  String onSuccess() {
     User user = scb.getAuthenticatedUser(...);
     ...
  }
}

is THE way to use the IoC? This is an example for pages. I think it's ok but
what about this second example, out of pages.

So I need to create a ServiceFactory class.

public class ServiceFactory {
    private ServiceClassB scb;
    private static ServiceFactory instance;

    static {
        instance = new ServiceFactory();
    }

    public static ServiceFactory getInstance() {
        return instance;
    }

    private ServiceFactory() {
        scb = new ClassB();

    }

    public ServiceClassB getSab() {
        return scb;
    }

}

In a ClassA (not anymore a Tapestry page) I have to do that :

public class ClassA {
  private ServiceClassB sab;
  public ServiceClassB getSab() { return sab; }
  public void setSab(ServiceClassB sab) { this.sab = sab; }

  public String aMethod() {
    sab = ServiceFactory.getInstance().getUserService();
    User user = sab.getAuthenticatedUser(...);
    ...
  }
}

I think this is not IoC. It's just management of services. So my goal is to
make that IoC and "something" have to call setSab for me, or maybe it's
should be the role of the ServiceFactory?

Regarding what Hugo explained to me, (thanks a lot for your time!)  one of
the way is this :

public class UserModule {
  public static ServiceClassB build(ServiceUserDao userDao){
    ClassB cb = new ClassB(userDao);
    return cb;
  }

  public static ServiceUserDao build() {
    UserDao dao = new UserDao();
    return dao;
  }
}

But I still don't know how to implement ClassA to get my ServiceClassB
instance provided by the IoC. I cannot call the Module without creating a
strong link between ClassA and the IoC, or maybe I have to create this
link?? In my mind, nothing have to call the IoC since it's the IoC who
should call the classes. So I have to say to the IoC which class need it and
for now I just know how to tell the IoC which Services I have and the
dependencies they have.

Or maybe, I'm completely wrong, lol, and in fact the role of IoC is just to
expose services to make them available to the all application and I have in
ClassA to call explicitely the services via their builder in the module and
use them via their interface!

2007/12/17, Hugo Palma <[EMAIL PROTECTED]>:
>
> Ok, as you can see in the tapestry-ioc documentation there are three
> ways of injecting dependencies into services. Only one of them is
> actually intrusive, the "As parameters to the service implementation
> class' constructor (for autobuilt services)" one. If you use this way
> then you will need to use the Inject annotation in your service
> implementation class which will create the dependency you don't want.
>
> So, this leaves you with the other two choices. The "As parameters to
> the service builder method" way has an example right in the beginning of
> the "Injecting Dependencies" sections of the docs. This way your service
> class can be completely independent of your IoC container. All the
> injection stuff is done in the module builder class. All you have to do
> is declare the dependencies in your service build method in the module
> builder and use the service class setters to set it's dependencies.
>
> Finally, you can use the "As parameters passed to the constructor of the
> service's module builder (cached inside instance variables)" way. This
> in not intrusive also because, again, all the injection stuff is done in
> the module builder. Only this time instead of the dependencies being
> injected as parameters in the build method they are injected in you
> module builder class constructor.
>
> Hope this helps...
>
> Michael Bernagou wrote:
> > Hi Hugo,
> >
> > It is exactly that and I didn't understand how to do that. Whatever I
> test,
> > it works but it is intrusive.
> > I re-read the documentation about tapestry-ioc and I still not
> understand
> > how to do that. I need an example of webapp with 3 layers around the ioc
> to
> > see how tapestry-ioc gives the services instance to the layers.
> >
> > @Thiago, I read this part but I don't know enought Spring to decide if
> > tapestry-ioc is really better than Spring. I'm not sure "better" is the
> > word. I would prefer "more adapt for Tapestry webapp" but it's just my
> > feelling. Just to argue a little more, if it is really "better", why the
> way
> > to use service injection is different between ioc - MVC and ioc - DAO or
> ioc
> > - WS (example, declare a Logger in these all 3 layers and compare the
> > howto)? Learning a way to do the same thing for each layer is not
> something
> > "better".
> > I'm quite sure it can be "better" than Spring but for some developper,
> XML
> > is better than Pojo... just a point of view ;o)
> > For now, I want to try to use Tapestry-ioc because I think again it is
> more
> > adapt to my needs
> >
> > 2007/12/14, Hugo Palma <[EMAIL PROTECTED]>:
> >
> >> mmmmm, i think there's something missing here....
> >> If i'm understanding correctly you want tapestry-ioc to manage all your
> >> services (either presentation stuf, db stuff, etc) and you don't want
> it
> >> to be intrusive. You don't want to reference any tapestry-ioc API
> inside
> >> you service classes. Does that describe the problem ?
> >> If so, tapestry-ioc can solve your problem really easily.
> >>
> >> Michael Bernagou wrote:
> >>
> >>> So I have to call the current Registry (or to manage it myself) and
> call
> >>>
> >> the
> >>
> >>> myRegistry.getService(MyService.class);
> >>>
> >>> Hum, I think it's not ioc anymore if I have to setup my containers
> >>>
> >> (Axis2,
> >>
> >>> Hibernate) to call the tapestry-ioc or I have to create a sort of
> >>>
> >> "middle
> >>
> >>> layer" between the layer and the tapestry-ioc, and then my classes
> have
> >>>
> >> to
> >>
> >>> call this middleLayer... hum...
> >>>
> >>> How do you do yourself? Webapp have usually more than just a MVC
> layer.
> >>>
> >> Are
> >>
> >>> you using the tapestry-ioc only for the MVC layer?
> >>> Its seems to me strange for one class having to use @Inject Logger
> log;
> >>>
> >> and
> >>
> >>> for another class Logger log = Logger.getLogger(
> >>> ApplicationDaoImpl.class.getName());
> >>>
> >>> 2 ways to get the logger in the same application for the same target
> log
> >>> file... Maybe tapestry-ioc should be extended to take care also to the
> >>>
> >> other
> >>
> >>> layers.
> >>>
> >>> Maybe I'm totally wrong ;o) so, how should it be coded to respect ioc
> >>> pattern between tapestr-ioc and other layers such as Axis2 and
> >>>
> >> Hibernate3
> >>
> >>> (for T5 we know ;o) )
> >>>
> >>> Thanks
> >>>
> >>>
> >>> 2007/12/14, Michael Bernagou <[EMAIL PROTECTED]>:
> >>>
> >>>
> >>>> Ok I'm going to investigate about that Registry.
> >>>> So, what's the interrest to use tapestry-ioc instead of Spring ioc?
> If
> >>>>
> >> I
> >>
> >>>> need my ioc to provide all the services I need, I cannot use
> >>>>
> >> tapestry-ioc
> >>
> >>>> unless I need my other layers to call explicitely the ioc which is
> not
> >>>> recommended when you want independant layers working together.
> >>>>
> >>>> @Hugo, ok I'm going to reread again this part.
> >>>>
> >>>> 2007/12/14, Kristian Marinkovic <[EMAIL PROTECTED] >:
> >>>>
> >>>>
> >>>>> hi michael,
> >>>>>
> >>>>> @Inject only works in Tapestry pages and components (and mixins).
> >>>>> By default Tapestry will use the field type as an id and lookup the
> >>>>> corresponding ioc service.
> >>>>>
> >>>>> I assume your Web Service Container was not started by the ioc
> >>>>> container so it runs outside its control. Therefore you have to
> first
> >>>>> obtain an ioc Registry first and then ask it explicitly for the
> >>>>>
> >> service
> >>
> >>>>> you want to call. The builder methods are not meant to be called
> >>>>> except by the container :)
> >>>>>
> >>>>> g,
> >>>>> kris
> >>>>>
> >>>>>
> >>>>>
> >>>>>
> >>>>> "Michael Bernagou" < [EMAIL PROTECTED]>
> >>>>> 14.12.2007 10:51
> >>>>> Bitte antworten an
> >>>>> "Tapestry users" <users@tapestry.apache.org >
> >>>>>
> >>>>>
> >>>>> An
> >>>>> "Tapestry users" <users@tapestry.apache.org>
> >>>>> Kopie
> >>>>>
> >>>>> Thema
> >>>>> IoC global question
> >>>>>
> >>>>>
> >>>>>
> >>>>>
> >>>>>
> >>>>>
> >>>>> Hi,
> >>>>>
> >>>>> I have a sort of general question about tapestry-ioc. In fact, I
> would
> >>>>> like
> >>>>> it to manage all the services of my webapp. In theory it should be
> the
> >>>>> case,
> >>>>> shouldn't be?
> >>>>>
> >>>>> So to provides services to my MVC, no problem the syntax @Inject
> >>>>> MyService
> >>>>> _myService; work perfectly. But, to provide services the same way
> for
> >>>>>
> >> my
> >>
> >>>>> Dao
> >>>>> it's different. The same syntax doesn't provide any instance.
> >>>>> I used the tapestry-hibernate and declare my dao as services :
> >>>>>
> >>>>>   import org.hibernate.Session;
> >>>>>
> >>>>>   public static UserDao buildUserDao(@InjectService("Session")
> Session
> >>>>> session) {
> >>>>>       return new UserDaoImpl(session);
> >>>>>   }
> >>>>>
> >>>>>   public static UserService
> buildUserService(@InjectService("UserDao")
> >>>>> UserDao dao) {
> >>>>>     return new UserServiceImpl(dao);
> >>>>>   }
> >>>>>
> >>>>> So my implementation can work. But to use the service, all the
> callers
> >>>>> have
> >>>>> to know a session object!
> >>>>> Ok, if my caller is a web service, and this web service want to call
> >>>>>
> >> the
> >>
> >>>>> UserService, @Inject UserService _userService; DOES NOT work and
> >>>>> _userService is null! So I have to call explicitely the builder of
> my
> >>>>> service FROM the web service... If I do that, I bypass the IoC
> purpose
> >>>>> because I link it directly :
> >>>>>
> >>>>> UserService us = AppModule.buildUserService ( ... and then I have a
> >>>>> problem!
> >>>>> buildUserService need a UserDao, but my web service doesn't care
> about
> >>>>> knowing any kind of dao!
> >>>>>
> >>>>> So, I should be able to use @Inject MyService _myService; anywhere
> in
> >>>>>
> >> my
> >>
> >>>>> project and the tapestry-ioc should be able to give instance to all
> >>>>>
> >> the
> >>
> >>>>> class...
> >>>>>
> >>>>> Did I (probably) missed something?
> >>>>>
> >>>>> --
> >>>>> Michael Bernagou
> >>>>> Java Developper
> >>>>>
> >>>>>
> >>>>>
> >>>>>
> >>>> --
> >>>> Michael Bernagou
> >>>> Java Developper
> >>>>
> >>>>
> >>>
> >>>
> >>>
> >>>
> >
> >
> >
> >
>



-- 
Michael Bernagou
Java Developper

Reply via email to