Thanks for the reply.  I actually did something similar to this before
I saw your response.  I made the following class

DaoManagerLoader.java
public class DaoManagerLoader {

    public static final String DAO_XML_PATH =
"com/intrust/anykey/database/dao/iBatis/dao.xml";
    public static DaoManager daoManager = null;

    public static DaoManager getInstance() {
        if (daoManager == null){
            System.out.println("Need a new one");
             try{
                 final Reader reader =
Resources.getResourceAsReader(DAO_XML_PATH);
                 daoManager = DaoManagerBuilder.buildDaoManager(reader);
            }catch(IOException e) {
                e.printStackTrace();
            }
        }else{
            System.out.println("Don't need a new one");
        }
        return daoManager;
    }
}

And then in my XxxxServiceImpl I do:

daoManager = DaoManagerLoader.getInstance();

That seems to work pretty good.  With that being said, can I ask
another question. If I have my UserService, UserServiceImpl, UserDao,
and UserDaoImpl all setup like the documentation states, and then in
my application/web application I need to access the UserDAO, is it
appropriate to instantiate it like this....

 UserService userDAO = new UserServiceImpl();

Or am I supposed to do it a differnet way.  I have attached the files.
 I'd really like to know if I am doing this correctly.  Thanks.

Gregg



On 5/2/05, Brandon Goodin <[EMAIL PROTECTED]> wrote:
> Saw a typo! oopsie :-)
> 
> " I would not place the dao in my base service class."
> 
> Should be:
> "I would not place the dao manager in my base service class."
> 
> On 5/1/05, Brandon Goodin <[EMAIL PROTECTED]> wrote:
> > Hi Gregg,
> >
> > The dao manager does not parse it every time. It only parses once.
> >
> > Also, I would do things a bit different than you are doing. I would
> > not place the dao in my base service class. I would use a DaoConfig
> > class that holds the DaoManager reference as an static final instance
> > variable. I would setup my Service classes to contain instance
> > variable DAO classes that are loaded via the DaoConfig class in the
> > constructor. See the examples below.
> >
> > --- DaoConfig.java ---
> > public class DaoConfig {
> >
> >   private static final DaoManager daoManager;
> >
> >   static {
> >
> >     try {
> >       String resource = "org/apache/ibatis/jgamestore/dao/sqlmap/dao.xml";
> >       Reader reader = Resources.getResourceAsReader(resource);
> >       daoManager = DaoManagerBuilder.buildDaoManager(reader);
> >     } catch (Exception e) {
> >       throw new RuntimeException("Could not initialize DaoConfig.
> > Cause: " + e);
> >     }
> >   }
> >
> >   public static DaoManager getDaoManager() {
> >     return daoManager;
> >   }
> >
> > }
> >
> > --- CatalogServiceImpl.java ---
> >
> > public class CatalogServiceImpl implements CatalogService {
> >
> >   private DaoManager daoManager;
> >   private CategoryDao categoryDao;
> >   private ProductDao productDao;
> >   private ProductImageDao productImageDao;
> >   private ImageDao imageDao;
> >
> >   /**
> >    * regular empty constructor
> >    */
> >   public CatalogServiceImpl() {
> >     this.daoManager = DaoConfig.getDaoManager();
> >     this.categoryDao = (CategoryDao) daoManager.getDao(CategoryDao.class);
> >     this.productDao = (ProductDao) daoManager.getDao(ProductDao.class);
> >     this.productImageDao = (ProductImageDao)
> > daoManager.getDao(ProductImageDao.class);
> >     this.imageDao = (ImageDao) daoManager.getDao(ImageDao.class);
> >   }
> > ...
> >
> > public void addProduct(Product product) {
> >     try {
> >       daoManager.startTransaction();
> >
> >       // save product
> >       productDao.addProduct(product);
> > ...
> >       daoManager.commitTransaction();
> >     } finally {
> >       daoManager.endTransaction();
> >     }
> > }
> >
> > Hope that helps,
> >
> > It is a BAD policy to make your service layer dependent on the
> > view/web layer. Your web layer can depend on your service layer. But,
> > your service layer should never depend on your web layer. Likewise
> > your Service Layer can depend on your DAO layer. But, your DAO layer
> > should never depend on the Service layer. Make it a rule to maintain
> > healthy separation in your development.
> >
> > _______Model______
> > |                |             |
> > Web-->Service-->Dao
> >
> > Ciao,
> > Barndon
> >
> > On 5/1/05, Gregg D Bolinger <[EMAIL PROTECTED]> wrote:
> > > I am testing using iBatis DAO with iBatis SqlMaps in a web
> > > application.  Right now I have a BaseService that looks like this:
> > >
> > > public abstract class BaseService {
> > >
> > >     public static final String DAO_XML_PATH =
> > > "com/intrust/anykey/database/dao/iBatis/dao.xml";
> > >
> > >     protected DaoManager daoManager;
> > >
> > >     public BaseService(){
> > >         try{
> > >             final Reader reader = 
> > > Resources.getResourceAsReader(DAO_XML_PATH);
> > >             daoManager = DaoManagerBuilder.buildDaoManager(reader);
> > >         }catch(IOException e) {
> > >             e.printStackTrace();
> > >         }
> > >     }
> > > }
> > >
> > > All my Service class extend this base service.  What I would like to
> > > know is if there is a way to load the daoManager into the
> > > ServletContext somehow so that I am not parsing the dao.xml file with
> > > every service request.  I'd like to do it once when the Webapp loads
> > > and just pull the daoManager from the ServletContext.  Is it as easy
> > > as creating a ContextListener to do this?  Is there one in the API
> > > that I don't know about?  Writing my own isn't a problem though.
> > >
> >
>
package com.intrust.anykey.database.dao;


import com.intrust.anykey.web.modelbeans.User;
import com.ibatis.dao.client.Dao;

import java.util.List;


public interface UserDAO extends Dao {

    public User getUser(String username, String password);
    public void insert(User user);
    public void delete(User user);
    public List findAllUsers();

}
package com.intrust.anykey.database.dao.iBatis;

import com.intrust.anykey.database.dao.UserDAO;
import com.intrust.anykey.utils.MD5;
import com.intrust.anykey.web.modelbeans.User;
import com.ibatis.dao.client.template.SqlMapDaoTemplate;
import com.ibatis.dao.client.DaoManager;
import com.ibatis.dao.client.DaoException;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import java.util.HashMap;
import java.util.Map;
import java.util.List;
import java.sql.SQLException;

/**
 * Created by IntelliJ IDEA.
 * User: Gregg
 * Date: Apr 25, 2005
 * Time: 5:13:26 PM
 * To change this template use File | Settings | File Templates.
 */
public class UserDAOImpl extends SqlMapDaoTemplate  implements UserDAO {

    public UserDAOImpl(DaoManager daoManager) {
        super(daoManager);
    }

    public User getUser(String username, String password) {
        String passHash = MD5.getHash(password);
        final Map map = new HashMap();
        map.put("username", username);
        map.put("password", passHash);
        try {
            return (User)getSqlMapExecutor().queryForObject("getUser", map);
        }catch(SQLException e) {
            throw new DaoException("Error Finding User. Cause: " + e.getMessage());
        }
    }

    public void insert(User user) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    public void delete(User user) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    public List findAllUsers() {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }
}
package com.intrust.anykey.services;

import com.intrust.anykey.web.modelbeans.User;
import com.intrust.anykey.database.dao.UserDAO;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Gregg
 * Date: Mar 3, 2005
 * Time: 9:54:21 AM
 * To change this template use File | Settings | File Templates.
 */
public interface UserService {

    //public void setUserDao(UserDAO userDao);
    public User getUser(String username, String password);
    public void insert(User user);
    public void delete(User user);
    public List findAllUsers();
}
package com.intrust.anykey.services;

import com.intrust.anykey.web.modelbeans.User;
import com.intrust.anykey.database.dao.UserDAO;
import com.ibatis.dao.client.DaoException;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.MailException;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Gregg
 * Date: Mar 3, 2005
 * Time: 9:56:01 AM
 * To change this template use File | Settings | File Templates.
 */
public class UserServiceImpl extends BaseService implements UserService {

    private UserDAO userDao;

   public UserServiceImpl(){
       super();
       userDao = (UserDAO)daoManager.getDao(UserDAO.class);

   }
    public User getUser(String username, String password) {
        try{
            return userDao.getUser(username, password);            
        }catch(DaoException e){
            e.printStackTrace();
            return null;
        }
    }

    public void insert(User user) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    public void delete(User user) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    public List findAllUsers() {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }


}
package com.intrust.anykey.services;

import com.ibatis.dao.client.DaoManager;
import com.ibatis.dao.client.DaoManagerBuilder;
import com.ibatis.common.resources.Resources;
import com.intrust.anykey.utils.DaoManagerLoader;

import java.io.Reader;
import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 * User: Gregg
 * Date: May 1, 2005
 * Time: 10:28:00 PM
 * To change this template use File | Settings | File Templates.
 */
public abstract class BaseService {

    protected DaoManager daoManager;

    public BaseService(){
        daoManager = DaoManagerLoader.getInstance();
    }


}
package com.intrust.anykey.utils;

import com.ibatis.dao.client.DaoManager;
import com.ibatis.dao.client.DaoManagerBuilder;
import com.ibatis.common.resources.Resources;

import java.io.IOException;
import java.io.Reader;

/**
 * Created by IntelliJ IDEA.
 * User: Gregg
 * Date: May 1, 2005
 * Time: 11:05:11 PM
 * To change this template use File | Settings | File Templates.
 */
public class DaoManagerLoader {

    public static final String DAO_XML_PATH = "com/intrust/anykey/database/dao/iBatis/dao.xml";
    public static DaoManager daoManager = null;

    public static DaoManager getInstance() {
        if (daoManager == null){
            System.out.println("Need a new one");
             try{
                 final Reader reader = Resources.getResourceAsReader(DAO_XML_PATH);
                 daoManager = DaoManagerBuilder.buildDaoManager(reader);
            }catch(IOException e) {
                e.printStackTrace();
            }
        }else{
            System.out.println("Don't need a new one");
        }
        return daoManager;
    }
}

Reply via email to