I am trying to implement a base controller, app services, repository design. I am new to this level of OO design but have gotten it pretty much complete. The remaining hurdle is how to handle extending my base controller.
My Goals:
- have a base controller that can stand on its own…i.e. you could have a specific controller inherit from it but itself be empty and have it work.
- In cases where I need to extend the base controller, have the
ability to do so in the specific controller.
Here is what I have:
Base Controller:
public class BaseControllerSingle<TRepository, TViewModelSingle> : BaseController
...
// GET: /InventoryMgmt/ManageItems/Edit/5
public virtual ActionResult Edit(
int id = 0,
string pageTitleNoun = "",
Expression<Func<TRepository, bool>> predicate = null
)
{
TViewModelSingle viewModelSingle = new TViewModelSingle();
ViewBag.Mode = "EDIT";
if (id == 0) //Add new company mode
{
viewModelSingle = _baseAppServSingle.CreateNewViewModel(CurrentUserId);
ViewBag.Header = "Create New " + pageTitleNoun;
ViewBag.Mode = "NEW";
return View(viewModelSingle);
}
//else edit existing record mode
viewModelSingle = _baseAppServSingle.CreateEditViewModel(id, predicate);
ViewBag.Header = "Edit " + pageTitleNoun;
return View(viewModelSingle);
}
And my specific controller. Note the code in this is incorrect (specifically”ItemViewModel viewModel = actionResult(ItemViewModel);”…but trying to show what I am attempting, which is essentially to extract the view model from the action result, manipulate it further, then return it.
public class ManageItemsController : BaseController
...
// GET: /InventoryMgmt/ManageItems/Edit/5
public ActionResult Edit(int id = 0)
{
ActionResult actionResult = GetBaseControllerSingle().Edit(
"Material",
id,
x => x.Id == id && x.CompanyId == CurrentCompanyId);
ItemViewModel viewModel = actionResult(ItemViewModel);
if (id == 0)
{
viewModel = _manageItemsAppServ.CreateNewViewModel(viewModel, CurrentCompanyId);
}
viewModel.DDLOptions = _manageItemsAppServ.CreateFilterOptionsViewModel(CurrentCompanyId);
return View(viewModel);
}
I have decided to re-write some of the code to remove this necessity. I think maybe it could be done with Ninject…but that’s not something I wanted to get into right now