Clarity about helpers in MVC3?

Do assistants in MVC3 help in the controller as well as in the views?

Is the helper a suitable place to host commonly used controller methods?

I want to create a generic method to get all child identifiers in a database and make sure that it is in the correct scope. I just want to make sure that I use my logic in the right area.

thanks

+4
source share
4 answers

You can implement the Controller base for this logic. Helpers or extension methods are useful when you do not want to change something for the interface.

+4
source

HtmlHelper is not available for the controller, since the controller should not be responsible for generating HTML, but UrlHelper is available in the controller.

The method of obtaining certain data from your database does not belong to your controller in either UrlHelper or HtmlHelper . You must create a separate class for this logic, and then call the method in this class from your controller. If you are using Dependency Injection, which I suggest, your controller code might look like this:

 public class MyController { IMyDataUtil _dataUtil; public MyController(IMyDataUtil dataUtil) { _dataUtil = dataUtil; } public ActionResult SomeAction(int parentId) { var childIds = _dataUtil.GetChildIds(parentId); ... } } 

As you can see, this allows you to save the data access code in a class specially designed for this purpose. The fact that this controller depends on this class of utility immediately becomes obvious and does not require much more code than calling the extension method for the assistant. Controllers that are not related to these class methods will not need access to them.

On the other hand, if there are methods that are likely to be used by a group of different controllers, injecting the same data class into all of them can become cumbersome. In this case, you can:

  • Extend a base class that has an instance of a data access class introduced into it using a method or property insertion, and which then subclasses it through a protected or public property or
  • Create your own helper class that wraps classes and methods that you are likely to use in all your controllers, and introduce this class so that you have only one dependency for many common functions or
  • Combine steps 1 and 2.
+2
source

If using “helpers” you refer to things like HtmlHelper , then no, they are not used by the controller, since theoretically you can use your controllers and reuse them with a completely different rendering mechanism (for example, WPF), because the controller does not respond for rendering.

If you say, it seems to me, helper classes / methods that manipulate your data, ready to be placed in the model using the controller, and then passed to the presentation for presentation, then you can consider the “business logic” layer . For example, if you were talking about a (typical) example of a bank account, you could:

 public class BankAccountService { public IEnumerable<string> GetAllAccountIdsForCustomer(int customerId) { // Talk to the database here and retrieve the account id for a customer } public string GetCustomerName(int customerId) { // Talk to the database here and retrieve the customer name } } 

Then your controller:

 public ActionResult AccountNumbers(int customerId) { var model = new AccountNumbersModel(); model.CustomerId = customerId; model.AccountNumbers = BankAccountService.GetAllAccountIdsForCustomer(customerId); return View(model); } 

Obviously, in this example, you will need to define a class called AccountNumbersModel , and you probably also want to consider using Dependency Injection to provide an instance of BankAccountService for your controller, but describing how to do everything that is beyond the scope of this answer .

The advantages of this approach give you testability and separation , each part of the code is responsible for one task, and you reduce the complexity of each individual part and make it easier to make changes without breaking anything.

+1
source

I want to create a generic method to get all child identifiers in a database and make sure that it is in the correct scope. I just want to make sure that I use my logic in the right area.

This seems to work for ActionFilter .

0
source

Source: https://habr.com/ru/post/1395281/


All Articles