[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
[
List Home]
[aspectj-users] help and ideas on lazy loading thru aspectj
|
Hi all,
I'm managing a set of DAO-like objects, and I'm thinking of implementing a
lazy loading method thru AOP. Let me explain thru an example:
public class Person implements SerializableObject{
// a person has a set of roles
private List<Role> roles = null;
// a person has a set of skills
private List<Skill> skills = null;
// method to load roles
public void loadRoles(){ // a very long database operation }
// method to load skills
public void loadSkills(){ // a very long database operation }
// get skills and roles
public List<Skill> getSkills(){ return this.skills; }
public List<Roles> getRoles(){ return this.roles; }
.....
}
since the loadSkills and loadRoles method are very long to execute, I don't
like them to be executed as soon as the object is created, but I prefer to
have them loaded once a method that operates on such lists is used (lazy
loading). So I'd like to intercept the method call getSkills() and getRoles()
and prefix the method execution with a loadSkills() or loadRoles() method
execution. Please note that the Roles and Skills are objects that come from
the database too, and so are SerializableObjects too (SerializableObject is
for me a marker that an object come from the database).
Problem: this approach is not general and must be specialized for each kind of
complex/long-to-load attribute of the Person class, and such of other similar
classes. So I'm thinking of a general solution and I'd like to get ideas and
suggestions from you aspectj users.
First of all, does it make sense to use AOP for this kind of stuff? I'm in
doubt this is a real crosscutting concern, maybe it's just a proxy-like
problem.
By the way, how can I get a general solution that can work for any attribute
of my SerializableObjects? I'm thinking of:
1) creating an interface, called FilteredSerializableObject that has the
following method:
public interface FilteredSerializableObject<T>{
public List<T> loadFiltered(SerializableObject owner);
}
and force Skills and Roles to implement such interface, where the
loadFiltered(Person p) will load all the skills and roles of a person.
2) transfor each attribute that must lazy loaded as implementing
FilteredSerializableObject. So for instance the Role class will implement the
above interface.
3) intercept any access from a serializable object to a
filteredserializableobject thru Aspectj (this should intercept statements like
return this.skills)
4) if the filteredserializableobject is not loaded yet, load it thru the
loadFiltered() method.
Besides the fact that I don't know if there's a simple method (maybe I can
also use annotations to mark attributes that must lazy loaded), I've got
several problems:
a) how can I intercept access to filteredserializableobject (step 3)
b) how can I know if the object list has been already loaded? Maybe
introducing a map of loaded instances, but I've not a clear view of this.
Hope someone can suggest me a way.
Thanks,
Luca