I am rusty in ASP.Net MVC and am struggling with the proper way to resolve a situation I have ran into on a large project that I inherited. The application has a large number of controllers that were originally well defined in their roles. As the application has grown I am running into issues with increasing overlap of functionality between them.
For example, there is now a Dashboard controller that gives a user an overview of key parameters happening throughout the rest of the application. As a result this controller relies on a lot of dependencies in order to populate the view. It also performs a lot of business logic/ calculations on that data also needed for the dashboard. Now we have the new additional requirement to archive these same values into the database at key milestones through the application process. This works great when the dashboard view is in use (it uses ajax and makes frequent calls for updates) - and I can have it store the values as needed when we hit those milestones.
But when the dashboard is not being used - and no calls to the dashboard view / controller are being made we still need to store the data at the milestones. I do not want to duplicate the same logic in every other controller where the various milestones may be hit, but am struggling to abstract it with all the necessary dependencies. What is the best way to handle this?
[Authorize]
public class DashboardController : SdControllerBase
{
private readonly ICoLabAssembler _assembler;
private readonly ICoLabDataMapper _colabDataMapper;
private readonly IClusterAssembler _clusterAssembler;
private readonly IClusterDataMapper _clusterDataMapper;
private readonly IStatementDataMapper _statementDataMapper;
private readonly IStatementAssembler _statementAssembler;
private readonly IActiveCoLabIdService _activeCoLabIdService;
private readonly IIsmAlgorithmAdapterService _ismAdapter;
private readonly IPrioritizationVoteActionDataMapper _prioritizationVoteActionDataMapper;
private readonly IVoteDataMapper<PrioritizationVote, Participant> _prioritizationVoteDataMapper;
private readonly IPrincipal _currentUser;
private readonly SdContext _context;
private int? _activeColabId;
protected int ActiveColabId
{
get
{
_activeColabId = _activeColabId ?? _activeCoLabIdService.GetColabId();
return _activeColabId.Value;
}
}
public DashboardController(ICoLabAssembler assembler, ICoLabDataMapper dataMapper,
IClusterAssembler clusterAssembler, IClusterDataMapper clusterDataMapper,
IStatementDataMapper statementDataMapper, IStatementAssembler statementAssembler,
IActiveCoLabIdService activeCoLabIdService, IPrioritizationVoteActionDataMapper prioritizationVoteActionDataMapper,
IVoteDataMapper<PrioritizationVote, Participant> prioritizationVoteDataMapper, IPrincipal currentUser, IIsmAlgorithmAdapterService ismAdapter, SdContext context)
{
_assembler = assembler;
_colabDataMapper = dataMapper;
_clusterAssembler = clusterAssembler;
_clusterDataMapper = clusterDataMapper;
_statementDataMapper = statementDataMapper;
_statementAssembler = statementAssembler;
_activeCoLabIdService = activeCoLabIdService;
_prioritizationVoteActionDataMapper = prioritizationVoteActionDataMapper;
_prioritizationVoteDataMapper = prioritizationVoteDataMapper;
_currentUser = currentUser;
_ismAdapter = ismAdapter;
_context = context;
}
public async Task<ActionResult> Index()
{
var model = await buildDashboardViewModelAsync();
return View(model);
}
[HttpPost]
public async Task<JsonResult> getDashboardAllData(int coLabID = 0)
{
var model = await buildDashboardViewModelAsync(coLabID);
return Json(model, JsonRequestBehavior.AllowGet);
}
private async Task<CoLabDashboardViewModel> buildDashboardViewModelAsync(int coLabID = 0)
{
//Lots of business logic here that uses the dependencies above
return model;
}