All,
I am always struggling with the best way to refactor so I would like your thoughts on this. Let us say your trying to save a category that looks like this.
component persistent=“true” extends=“catalogadmin.model.BaseEntity” {
property name=“categoryid” type=“numeric” ormtype=“int” fieldtype=“id” generator=“increment”;
property name=“name” type=“string” ormtype=“string”;
property name=“description” type=“string” ormtype=“string”;
property name=“metakeywords” type=“string” ormtype=“string”;
property name=“metadescription” type=“string” ormtype=“string”;
property name=“metaPageTitle” type=“string” persistent=“false”;
property name=“isactive” type=“boolean” ormtype=“int”;
property name=“sort” type=“numeric” ormtype=“integer”;
property name=“alias” type=“string” ormtype=“string”;
property name=“categories” fieldtype=“one-to-many” cfc=“Category” fkcolumn=“parent”;
property name=“parent” fieldtype=“many-to-one” cfc=“Category” fkcolumn=“parent”;
property name=“template” fieldtype=“many-to-one” cfc=“catalogadmin.model.template.CategoryTemplate” fkcolumn=“templateid”;
public Category function init(){
setIsActive(true);
return this;
}
}
Your first attempt might look something like this (parent & template are many-to-one relationships)
public void function save(){
var rc = event.getCollection();
var template = categoryTemplateService.get(1);
var parent = categoryService.get(rc.parent);
rc.category = categoryService.get( rc.id );
rc.category.setName(rc.name);
rc.category.setAlias(rc.alias);
rc.category.setDescription(rc.description);
rc.category.setMetaKeywords(rc.metakeywords);
rc.category.setMetaDescription(rc.metadescription);
rc.category.setIsActive( event.getValue(“isactive”,false) );
rc.category.setParent(parent);
rc.category.setTemplate(template);
categoryService.save(rc.category);
getPlugin(“messagebox”).setMessage(“info”,“Category saved!”);
setNextEvent(‘category/list’);
}
That is a little messy and doesn’t take into account 2 big steps, data validation and error handling. In the next go around I am just going to mock validation to keep this simple
public void function save(){
var rc = event.getCollection();
// populate the model with all the properties
rc.category = populateModel( model=categoryService.get(rc.id),exclude=“parent,template,isactive” );
// many-to-one
rc.category.setParent(categoryService.get(rc.parent));
// many-to-one
rc.category.setTemplate(categoryTemplateService.get(rc.template));
// checkbox does not exist
rc.category.setIsActive(event.getValue(“isactive”,false));
// validation mock will be replaced with actual validation later
if( isValidModel(rc.category) ){
try {
categoryService.save(rc.category);
getPlugin(“messagebox”).setMessage(“info”,“Category saved!”);
setNextEvent(‘category/list’);
} catch(any e) {
// write to log
// tell user
getPlugin(“messagebox”).setMessage(“error”,“There was a problem trying to save the category, please try again.”);
setNextEvent(event=‘category/edit’,persist=“category”);
}
} else {
getPlugin(“messagebox”).setMessage(“error”,“Your form had validation errors”);
setNextEvent(event=‘category/edit’,persist=“category”);
}
}
I guess my question is what here gets moved into the service layer save method? Validation is usually done at the controller so if you are going to validate here you need to populate your model here. What changes would you make to this?
Dan