ASP.NET MVC - Controller Operation

I think that I'm starting to get confused with the work of the controller in MVC.

I have a service that provides five functions:

  • list of packets in the queue
  • get a package
  • remove package
  • accept package
  • deny package

My ASP.NET MVC controller is dependent on this service and can usually make a service call in Action. I am satisfied so far.

The second part then creates the result of the ViewModel. If I do this inside the controller, the controller now has a list of explosive dependencies - each action added increases the dependencies to build the view model, and all of them are inherited by the controller. I don’t really like it. I am building this controller, which depends on N different view model constructors, but only using one of them for each request.

So, I was thinking about pulling it all out and applying action filters specific to each view model. I have not done this yet, but everything is in order.

The question for me raises the question: what is the responsibility of the dispatcher? If I eventually pulled the creation of a view model into filters, my controller does a little more than the traffic making the service call (and providing the filter plugin). If I instead left my controller responsible for creating each view model, this would become a mess.

It seems like I almost want to instantiate an action for each request, not a controller, and am I just abusing filters to figure this out?

+3
2

ViewModels Poco-Models? , ViewModel. .

public class PackageViewModel()
{
    public PackageDetail{get;set;}
    public int PackageID{get;set;}
    public List<Package>Packages{get;set;}
    public string SomeFilterCriteria{get;set;}

    publlic void FillPackageList(IPackageService packageService)
    {       
        Packages = packageService.GetPackages(SomeFilterCriteria);      
    }
}

:

public ViewResult ListPackages(PackageViewModel model)
{
    model.FillPackageList(PackageService);
    return View("ListPackages",model);

}

, " ".

+4

, . , , .

+2

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


All Articles