Selthien
Selthien

Reputation: 1258

How to properly setup controller injection with many related entities

I am wondering if what I am doing here is appropriate. I have an model in my database called Request. A Request has a lot of relationships with other tables (such as categories, notes, etc.)

So naturally I created CRUD services to wrap logic for each of those such as:

Now, I have a controller called RequestController. That naturally needs to get all the Requests along with the related properties/entities. This in turn causes my RequestController to have a substantial number of interfaces injected:

public RequestController(
    IMapper mapper,
    IRequestService requestService,
    IRequestorService requestorService,
    UserManager<ApplicationUser> userManager,
    IWebHostEnvironment environment,
    IRequestCategoryService requestCategoryService,
    IRequestNoteService requestNoteService,
    ICommitteeNoteService committeeNoteService)
    {
        _mapper = mapper;
        _requestService = requestService;
        _userManager = userManager;
        _requestorService = requestorService;
        _environment = environment;
        _requestCategoryService = requestCategoryService;
        _requestNoteService = requestNoteService;
        _committeeNoteService = committeeNoteService;
        //etc.
    }

With functions such as:

 public async Task<ActionResult> GetRequestKPIs() { }
 public async Task<JsonResult> GetRequests() { }
 public async Task<JsonResult> GetNotesForRequest(int RequestIdForNote) { }
 public async Task<JsonResult> UpdateNoteForRequest() { }
 public async Task<JsonResult> DeleteATGRequest(int Id) { }

etc....

My Question:

So my gripe at the end of the day is I inject a CRUD interface for a related model, but im only using the GET portions of it in this particular controller.

Would it be better for me to call separate controllers for related data which in turn uses those specific interfaces, like RequestNoteController, RequestorController, etc. which would contain functions like UpdateRequestNote()? Or do I need to create a specific class that implements the interfaces relevant to what I want in the RequestController.

Overall I am trying to understand what is the right way to organize controllers / service layer functions with a model that has LOTS OF RELATED ENTITIES. I have been having a hard time finding examples / explanations for this on google. If anyone could give me what the typical convention is for this or some articles that could be of help that would be a huge help for me understanding on others separate these things out.

Upvotes: 1

Views: 80

Answers (0)

Related Questions