开发者

Refactoring code in Symfony 1.4

Specifically, for example, what is the best way to开发者_开发问答 refactor code to be used in the model class?


In the MVC Pattern, the model should manipulate only the data stored in the database and all behaviour related to it. So:

  • if you have an action (part of the controller layer) that is doing lots of object manipulation, try to move such behaviour to a method or a group of methods so the Data Handling logic remains in the Model layer, leaving the controller as simple as possible ( it should only manipulate relations between data and view)
  • Same as above, the view should only take care of showing the information not processing it!

Now, the refactoring process i usually apply is:

  1. Find the code that seems related
  2. Try to create a function that could represent it's behaviour ( look for parameters possibilities). Sometimes you will end up with things like Template Method DesignPattern (Here you should test your code to see if it does what it should)
  3. Replace the original code with the function call (or in this case, calling an instance/static method of a certain class) (Check once again for functionality)

This process to any other layer with some differences:

  • in the Controller layer, you will need to use Components and Actions instead of static/instance methods.
  • in the view layer, you should create partials, slots and templates to refactor them.

A real example: You have a query which will check for the existence of a particular record with a given 'name' field value and you want to use it all over, then:

  1. check what type of info your handling. In this case is a PeerStructure that should in some way filter a table result depending on a table value. If you check i said: "Peer...","Table" twice so i think about creating a static method on the Peer class that handles the values i want to filter, lets say ApplicationUser.
  2. Now that i know what i'm going to build, check for parameters that may or may not be present. In this case, the "name" is a possible parameter (it could also be a criteria). Also you should think about what values are you going to return. And as we are talking about a static method on a peer class, i guess there are two possible values: a Criteria or a ResultSet( with this i mean a set of results rather than the structure).
  3. Finally, build the static method.

    public static function countUsername($username) { $c = new Criteria(); $c->add(ApplicationUserPeer::NAME,$name,Criteria::EQUAL); return ApplicationUserPeer::doCount($c); }

Now that you have your function its time to replace the old logic to use the new method. Let's say that you need to check if a name is available to be used by a new user that trying to register. In some action you should have a piece of code that looks like:

 public function executeCreateUser(sfWebRequest $request)
 {
  [...]
  $c = new Criteria();
  $c->add(ApplicationUserPeer::NAME,$request->getParameter('username'),Criteria::EQUAL);
  ;  
  if(ApplicationUserPeer::doCount($c) == 0)
  {
    //Then do some stuff that saves
  }
  [...]
 }

After using your function your code could look like this:

 public function executeCreateUser(sfWebRequest $request)
 {
  [...]
  if(ApplicationUserPeer::countUsername($request->getParameter('username')) == 0)
  {
    //Then do some stuff that saves
  }
  [...]
 }

So that's the process in action, remember that you can always refactor even more. For example, if you think about the parameter I choose for this example and its relations with functionality, you could also add a "CriteriaMethodParameter" that's default values is Equal, but that if you now need all usernames with letter A, you could set the CriteriaMethodParameter to be Like, anytime you want by simply changing parameter.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜