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.

Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Jul 10, 2013 at 12:38
\$\endgroup\$

1 Answer 1

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.

answered Jul 10, 2013 at 14:52
\$\endgroup\$
1
  • \$\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\$ Commented Jul 10, 2013 at 15:10

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.