Reputation: 6839
I'm developing a generic controller to handle File Upload and Download. My domain contains a general entity that will hold any file, and other child entities that will reference this one to hold the specific files reference.
I have a service for the general entity and lots of services for child specific entities.
Heres my scratch:
public class FileController : BaseController
{
public JsonResult GetAll(IService childService) { /*lot of code*/}
public FileResult Download(int id, IService childService) { /*lot of code*/}
public JsonResult Post(IService childService) { /*lot of code*/}
public JsonResult Delete(IService childService) { /*lot of code*/}
}
How can I inject the specific service into the action?
If I don't do this way, I will get lot of junkie code doing the same stuff everywhere. Is there another way? Maybe a pattern that I did't realize?
Upvotes: 0
Views: 771
Reputation: 43254
Rather than inject into the action, inject the service into the class:
public class FileController : BaseController
{
private IService _childService;
public IService ChildService { set { _childService = value; } }
public JsonResult GetAll() { /*lot of code*/}
public FileResult Download(int id) { /*lot of code*/}
public JsonResult Post() { /*lot of code*/}
public JsonResult Delete(IService childService) { /*lot of code*/}
}
Then, create a custom ControllerFactory
to extend DefaultControllerFactory
and specify that in your Global.asax
file.
Finally, have your custom ControllerFactory
class override the CreateController
method and inject the IService instance when creating the controllers.
Upvotes: 3