7.0: CreateRefactor

Refactor CreateNoteService

  1. Open ElevenNote.Web -> Controllers -> NoteController

  2. Click and drag down the button at the top right corner of the window so it splits the code into 2 sections. This is so you can see things at the top and bottom of the file at the same time.

    Drag Button

  3. Notice that the same 2 lines are repeated

    Repeated Lines

  4. Select this code in the Create method

    Code to Select

  5. Go to Edit -> Refactor -> Extract Method

    Refactor

  6. Rename the new helper method CreateNoteService()

    New Method

  7. Change NoteService to var

    Var

  8. Notice how it abstracts out the code so that it is usable in all ActionResult methods

  9. TIP: Do not try to instantiate a Service inside of the constructor. The MVC framework does not have the data available that may be needed.

Next, we'll add some validation messages.

Last updated