In my MVC web application the controller calls a service layer which takes any relevant request parameters and builds the model for the view e.g.
Controller
var model = _modelService.Build(id);
return View("ViewName", model);
If there are multiple reasons why the model service will fail to build a valid model, how do I communicate that to the controller so it can return an appropriate response?
Below is a contrived example.
Model Service
var entity = _dataService.Get(id);
if (entity == null) {
return null;
}
var entityMeta = _dataService.GetMeta(id);
if (entityMeta == null) {
return null;
}
return new Model(entity);
Controller
var model = _modelService.Build(id);
// Null because of invalid id
if (model == null) {
return View("Invalid Id");
}
// Null because of missing meta data
if (model == null) {
return View("Invalid meta data");
}
return View("ViewName", model);
In the example above there are two reasons why the model is null, but there is no way to inform the controller what has happened because all it gets is a null object back.
I can think of some possible solutions: throw an exception (which doesn't feel right - this isn't necessarily an unexpected outcome), move some of this logic to the controller (the general advise is to keep controllers slim), create a generic ModelResult object which contains the model or some information about why it has failed etc.
I feel like this is a common situation so there must be a standard pattern to apply.