tags:

views:

87

answers:

3

In your service layer, say you have a method that does XX, is this how you would reference your Dao classes?

public class SomeServiceImpl implements SomeService

    public void DoSomething(int someThingId){

    UserDao userDao = this.daoFactory().GetUserDao();
    SalesDao salesDao = this.daoFactory().GetSalesDao();
    ..
    ..
    ..

    }

It gets a bit much to do this, and was hoping if there was a easier more elegant way?

+7  A: 

I use Springframework to configure my application. This framework has the nice feature that enables me to Inject the dependencies into my service layer. Therefore the Service implementation looks something like that:

@Autowired
private UserDAO userDao;
public void doSomething(int someThingId) {
userDAO.findById(someThingId);
...
}
Ralf Edmund
+1 for dependency injection
Sapph
A: 

I am not sure how "Proper" it is but I often make private getters for the DAO objects (properties) and make them lazy loading. Makes it less verbose. You then can create a base service class if particular Dao are used in several places...

public class SomeServiceImpl implements SomeService
{
    private UserDao _UserDao

    private UserDao getUserDao()
    {
        if (_UserDao == null)
        {
            _UserDao = DaoFactory.GetUserDao();
        }

        return _UserDao;
    }

    public void DoSomething(int somethingId)
    {
        this.getUserDao().findById(somethingId);
    }
}
J.13.L
A: 

Either use annotation or xml configuration to inject dao dependency at run time.

fastcodejava