I need to refactor and tidy the following controller below, it's my first MVC application. I've successfully used AutoMapper as I began refactor controllers but I am not sure how to approach a scenario where the data for the ViewModel comes from multiple service calls. And that scenario seems to often present itself in slightly heavier view models where I have all kind of information to present.
Calling multiple mapping calls on top of the same view model seemed counter-intuitive and it feels like I am missing some good practice or common approach to the situation. Or maybe I am wrong and it's commonly used approach.
0) Thought of generally using AutoMapper somehow but as I said I cannot find a proper way to implement it, I'd like to keep the controller relatively thin but I also don't want to have too much unneeded/overkill abstraction.
1) Thought of having some build method on the view model and to pass needed data.
2) Thought of having a model between my database entities and the view model but I am not sure how to execute that either, I think I seen somewhere by examples that people return "Models" from service calls that contain complex data and then map those Models to the ViewModel with Automapper, instead of mapping service calls directly into the ViewModel like others are doing.
3) Saw people use a facade of sorts in one example and thought of that too.
It's just my first project under MVC and I would like to learn a robust general approach that is considered well written and good practice so I can use it to fully refactor this and continue to improve from there.
BKQEventCustomData BKQEventData = BKQHelper.ParseBase64EventData(Server.UrlDecode(Request.Url.Query));
var cutomData = JsonConvert.DeserializeObject<NopOrderData>(BKQEventData.custom.data.ToString());
var model = new TaskEventViewModel();
if (BKQEventData != null)
{
AuthenticateRequest(BKQEventData.xcm.clid, BKQEventData.xcm.opid);
model.PlacesList = new SelectList(_placesService.GetPlacesByClientId(BKQProfile.ClientId).Where(p => p.Active), "PlaceId", "Name");
model.ServicesList = new SelectList(_serviceService.GetServicesByClientId(BKQProfile.ClientId).Where(s => s.Active), "ServiceId", "Name");
model.ContractorsList = new SelectList(_contractorService.GetContractorsByClientId(BKQProfile.ClientId), "ContractorId", "BKQFullName");
model.StatusList = new SelectList(_statusService.GetStatusByClientId(BKQProfile.ClientId), "StatusId", "Name");
model.TaskEvent = new TaskEvent();
model.TaskEvent.Title = string.Empty;
model.TaskEvent.BKQCustomData = BKQEventData.custom.data.ToString();
model.TaskEvent.BKQCustomDataType = BKQEventData.custom.type.ToString();
model.TaskEvent.BKQClientId = BKQEventData.xcm.clid;
model.TaskEvent.BKQOperatorId = BKQEventData.xcm.opid;
model.TaskEvent.BKQInfProductId = BKQEventData.xcm.iprodid;
model.TaskEvent.BKQEventHistoryId = BKQEventData.xcm.evnhid;
model.TaskEvent.BKQEventTypeId = BKQEventData.xcm.evntype;
model.TaskEvent.BKQEventCaseId = BKQEventData.xcm.caseid;
model.TaskEvent.BKQQuota = BKQEventData.xcm.quotaid;
model.TaskEvent.BKQLangId = BKQEventData.xcm.langid;
model.TaskEvent.BKQContactId = BKQEventData.xcm.contid;
model.TaskEvent.CreatedBy = BKQEventData.xcm.opid;
model.TaskEvent.ModifiedBy = BKQEventData.xcm.opid;
model.BKQContact = BKQFramework.Contact.GetById(BKQEventData.xcm.contid);
}
else
{
return RedirectToActionPermanent("Login", "Login");
}