7.0: CreateRefactor
Last updated
Last updated
CreateNoteService
Open ElevenNote.Web -> Controllers -> NoteController
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.
Notice that the same 2 lines are repeated
Select this code in the Create
method
Go to Edit -> Refactor -> Extract Method
Rename the new helper method CreateNoteService()
Change NoteService
to var
Notice how it abstracts out the code so that it is usable in all ActionResult
methods
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.