开发者

How to structure my objects to clean up my controller code

I am trying to build an object to represent a ridiculously large form for a project I am working on. http://www.flickr.com/photos/namtax/5351091239/

I didnt want to build all the fields into one large object as I didnt think this was a good way to do things, so I have structured the model as so -

One contact object.

  • One work object - linked to the contact one to one.

  • One travel object - linked to the contact one to one.

  • One address object - linked to the contact one to many.

  • One emergency contact object - linked to the contact one to many.

Here is the sample code for one of these relationships - written in CF9 ORM

    property name="work"
             fieldtype="one-to-one"   
             cfc="work"     
             mappedby="contact";

The issue here is that it causes the saveContact function in my controller to be large and code smelly due to all the possible options you can choose in the form. Here is a rough translation of the code

VARIABLES.contact = contactService.getContact(form.iContactId);
contactService.set(contact:contact,argumentCollection:form);
contact = contactService.save(contact);

if(_emergencyContactService.userIsSubmittingAnEmergencyContact(form)){
  VARIABLES.emergencyContact = _emergencyContactService.getEmergencyContact(contact);

   emergencyContactService.setEmergencyContact(emergencyContact,form);

        if(! contact.hasEmergencyContact()){
            contact.setEmergencyContact(emergencyContact);
            emergencyContact.addarrContacts(contact);
        }

        _emergencyContactService.save(emergencyContact);
    }
// Repeat this logic to check if work object, travel object or address object have been selected in the form also, get and set accordingly.

I then repeat the if statement shown above for emergency contact, to check whether the work object, travel object or address object have been selected in the form also. If so, get and set the object, then link the contact object where necessary.

This doesnt seem efficient to me, especially as I need to repeat all this logic on another controller method as well. Is there a b开发者_如何学Pythonetter way to structure my objects, or a better way to handle my controller code?

Thanks.


My initial thoughts are to move any repeating logic from your controller to a service object that you can call from many places.

Bringing that along: you could break up the form into sub-forms - each represented by a 'sub-form' object. Each of these objects should be able to deal with information related to it's part of the form, and it should know what to save to the database. You could compose these form objects into a service object that you call from your controller.

One other thing I noticed is the way you actually pass your entire FORM scope into some functions. I tend to avoid this as you really are not describing what the function needs to do the job, and of course this scope can change when you don't expect it. I would suggest you specify the arguments required by each sub-form object and pass them to the service.

Example psuedocode:

// Controller code
travelSubFrm = new TravelForm(name, dob, address ...); // etc
workSubFrm = new WorkForm(position, dept ...); // etc

// Create service and save details - the service knows the logic
contactFormService.setTravelSubFrm(travelSubFrm);
contactFormService.setWorkSubFrm(workSubFrm);
contactFormService.process();

Just some quick thoughts.

Hope that helps!

EDIT: If you want to start looking into how to architect object-orientated code then I suggest you check out Head First Design Patterns. It's a great introduction to the topic and will help you organise code problem like the one you posted.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜