2
\$\begingroup\$

In my ASP.NET MVC code, I like to use controller service classes for my controllers. These service classes contain methods for retrieving viewmodel objects.

Here is an example controller snippet:

public SubscriptionsController(ISubscriptionsControllerService service)
{
    _service = service;
}

public ActionResult Index(Guid id)
{
    return View("Subscriptions", _service.GetSubscriptionsViewModelOnGet(id));
}

[HttpPost]
public ActionResult Index(SubscriptionsViewModel viewModel)
{
    _service.SaveSubscriptions(viewModel);

    return View("Subscriptions", _service.GetSubscriptionsViewModelOnPost(viewModel));
}

As you can see, I have a method for retrieving the subscriptions viewmodel on a GET request, as well as the equivalent for a POST request.

The POST method takes in an existing viewmodel object and updates any relevant data e.g. a list of subscription items, that need to be refreshed before passing back to the view.

My question is whether the naming of the methods (GetSubscriptionsViewModelOnGet() and GetSubscriptionsViewModelOnPost()) makes sense. They seem OK to me, but I'm interested in other people's views.

\$\endgroup\$
3
\$\begingroup\$

Why not name them both the same? The difference is the type of parameter you're passing. This leaves you free to do some method overloading:

public SubscriptionsViewModel GetSubscriptionsViewModel(Guid id)
{
    //GET Logic here...
}

public SubscriptionsViewModel GetSubscriptionsViewModel(SubscriptionsViewModel viewModel)
{
    //POST Logic here...
}

Why call them the same? They both do the same: return a SubscriptionsViewModel.

\$\endgroup\$
  • \$\begingroup\$ Yeah, I was thinking the same thing this morning, just before I posted this question. I think it does not matter about the context of when the code is getting the viewmodel e.g. GET/POST. I can make the method names easier i.e. the same. \$\endgroup\$ – Jason Evans Jul 10 '13 at 15:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.