You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user-java@ibatis.apache.org by Dmitry Neverov <ne...@nkmk.ru> on 2007/10/23 11:20:49 UTC

question on com.ibatis.jpetstore.service.*

Hi all!

Can anyone explain why should we use static method getInstance()? Why
not just use static methods for accesing DB? Is it some kind of design
pattern?

-- 
Best regards,
   Dmitri Neverov


Re: question on com.ibatis.jpetstore.service.*

Posted by Larry Meadors <lm...@apache.org>.
It makes testing much easier, and also makes it possible to inject a
proxy between the real service and the client of it.

It's just much more flexible this way.

Larry


On 10/23/07, Dmitry Neverov <ne...@nkmk.ru> wrote:
> Hi all!
>
> Can anyone explain why should we use static method getInstance()? Why
> not just use static methods for accesing DB? Is it some kind of design
> pattern?
>
> --
> Best regards,
>    Dmitri Neverov
>
>

RE: question on com.ibatis.jpetstore.service.*

Posted by Clinton Begin <cl...@gmail.com>.
Ack!  If you're just starting out, don't use the DAO framework.  :-)

Use Spring or use your own simple classes for DAOs.  They don't really need
a framework.  But yes, I'd instantiate the DAOs within the service.

If you're already using the DAO framework, then the answer to both of your
questions are:

>>Do you have dao initialization (if you use dao) in MyService
>>constructor? Something like this:

Yes

>>Is dao initialization cheap operation too? 

Yes

>> Should I use singleton,

No

Cheers,
Clinton


-----Original Message-----
From: Dmitry Neverov [mailto:neverov_dg@nkmk.ru] 
Sent: October-23-07 8:13 PM
To: user-java@ibatis.apache.org
Subject: Re: question on com.ibatis.jpetstore.service.*

Thanks for answers!

Do you have dao initialization (if you use dao) in MyService
constructor? Something like this:

  private MyService(){
    someDao = (SomeDao) daoManager.getDao(someDao.class);
  }

Is dao initialization cheap operation too? Should I use singleton,
if I use dao (to create new dao only once)?

-- 
Best regards,
   Dmitri Neverov


Re: question on com.ibatis.jpetstore.service.*

Posted by Dmitry Neverov <ne...@nkmk.ru>.
Thanks for answers!

Do you have dao initialization (if you use dao) in MyService
constructor? Something like this:

  private MyService(){
    someDao = (SomeDao) daoManager.getDao(someDao.class);
  }

Is dao initialization cheap operation too? Should I use singleton,
if I use dao (to create new dao only once)?

-- 
Best regards,
   Dmitri Neverov


RE: question on com.ibatis.jpetstore.service.*

Posted by Clinton Begin <cl...@gmail.com>.
Yes, it's singleton, not the best of patterns, but it works here.  

My preference now is to instantiate a new service for every request, similar
to how many modern web frameworks also instantiate a new controller class
for each request.  Object creation is fairly cheap and it makes concurrent
programming simpler in that there cannot be any shared data (other than
statics which are not recommended for anything other than constants anyway).
To summarize simply:

HTTP Request > New Controller > New Service > New DAO* 

*I don't always use the DAO layer.  

A simple dependency injection pattern uses two simple constructors:

// used for most code
public void MyController() {
  this.service = new MyService(); 
}

// allows easy mocking for unit testing
public void MyController(MyService service) { 
  this.service = service;  
}

Of course, Spring does something like this for you. 

This works very well for unit testing and simplifies concurrent programming
(or at least makes it harder to make a mistake).

Cheers,
Clinton

-----Original Message-----
From: Dmitry Neverov [mailto:neverov_dg@nkmk.ru] 
Sent: October-23-07 3:21 AM
To: user-java@ibatis.apache.org
Subject: question on com.ibatis.jpetstore.service.*

Hi all!

Can anyone explain why should we use static method getInstance()? Why
not just use static methods for accesing DB? Is it some kind of design
pattern?

-- 
Best regards,
   Dmitri Neverov