Announcement Announcement Module
Collapse
No announcement yet.
Where to Place an Extended List of Domain Objects Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Where to Place an Extended List of Domain Objects

    Thanks in advance for any help/guidance that can be provided.

    Here's my issue. We have a business rule that says a queue cannot have more than X number of entries in it, think no more than 10 people in a line as an example. This line needs to be cognizant of each Person's position and the overall size of the queue. So what I want to do is logically code the queue to contain all the business logic around this concept.

    Let's assume the Person domain object, which is in the grails-app/domain folder, looks like this, where the ellipsis covers up all the database configuration stuff that is irrelevant to this issue because I can save and get the Person data.

    Code:
    class Person {
       String name
       Long position
       ...
    }
    My thought is to extend the generic ArrayList for this specific instance and bake in all the queue logic. For brevity I placed minimal code in here just to illustrate my intent. The core of my question comes at the reorder() method. Reorder exists when people are done being in the line for whatever reason, say they are done with their transaction or just get tired of waiting.

    Code:
    class PersonQueue extends ArrayList<Person> {
         @Override
         boolean add(Person p)
         {
             //check queue size to make sure it is less than specified size
             if(this.size() >= 10)
             {
                 //handle exception case
             }
             super.add(p)
             reorder()
         }
    
         void reorder()
         {
             int placeInLine = 1
             this.each {
                it.position = placeInLine
                //ideally I would like to do the save right after this line
                if(it.save(flush:true))
                { 
                    ...
                    placeInLine++
                }
                else
                {
                    //handle errors
                }
              ...
         }
         ...
    }
    The extended ArrayList is not a domain object. It merely wraps a list of domain objects in specific business logic. Currently it is stored in the src/groovy... package and as such has no concept of transactions. The lack of a transaction means that it cannot save/update/read (if something would be lazily loaded off the Person object) the Person objects it contains. It would be extremely nice if this object could handle all the relevant database transactions in one localized place along with the business logic to indicate if a new Person could be added to the queue. Is there a way to get this object to be transactional aware? My initial thought was to drop this class into the grails-app/domain package but I am not sure if that is the best solution. Another thought is this is kind of like a "service" class in it handles a bunch of specific business logic but I am hesitant to put it in the grails-app/services folder. Again, any feedback/suggestions would be very helpful and appreciated.
Working...
X