0

I have a web application, where on a number of different pages, the user is able to upload, download, or delete files. The action methods for these operations are very similar among the different controllers, with the exception that they utilize different file management services, which all implement a common interface.

I was thinking of refactoring the code by placing the upload, download and delete methods in a generic file management controller, which can be used from the various views.

The difficulty I face, is that the correct file management service would have to be injected somehow, dependent on the current page (or route).

Is there a clean way this can be done in ASP.NET MVC 4?

  • I think that this question is very similar to yours and could be of help https://stackoverflow.com/questions/39174989/how-to-register-multiple-implementations-of-the-same-interface-in-asp-net-core?page=1&tab=votes#tab-top In short there is no neat way to do this but some workarounds are possible. Regards! – Anton Kovachev Feb 06 '22 at 15:51
  • What dependandy injection library are you using, and do the different management services that need to be injected depend implement the same interface? – Jon Ryan Feb 06 '22 at 21:35
  • Ninject. Yes, they implement the same interface. – Codisattva Feb 07 '22 at 08:17
  • Sounds like it could be a good idea to separate the buisness logic, i.e. file management, from the controll logic, i.e. the controller. Then you can have some common file management core, and the extend that into different derived classes that sets up the underlying core with the right dependencies – Artog Feb 08 '22 at 13:16

0 Answers0