Reputation: 332
Got a bit of an issue with MVC and using Entity Framework POCO classes.
I've used database first in a separate project to represent my domain layer which has been referenced by my MVC project as the MVC side of things only represents a small UI portion of a larger solution.
Issue being is that the UI is not a true representation of my domain layer, so I've been creating ViewModel classes that bridge together UI objects with domain objects. I use translation methods in between to convert domain objects in the controller into the relevant UI object.
To me this seems a bit of a long winded process and is quite error prone as when an update is made the the UI, or database you have to reflect it in the UI object and vice versa, and the translate methods get increasingly complex as the UI grows.
Would this be the right approach? Is there an alternative mapper for these kinds of objects?
Domain Object
public class Ticket
{
public int Id { get; set; }
public int ClientRef { get; set; }
public int PriorityRef { get; set; }
public int CategoryRef { get; set; }
public DateTime CreatedDate { get; set; }
}
View Model Object
public class TicketViewModel
{
public int Id { get; set; }
public int ClientRef { get; set; }
public int PriorityRef { get; set; }
public int CategoryRef { get; set; }
public DateTime CreatedDate { get; set; }
public SelectList PrioritySelectList { get; set; }
public SelectList CategorySelectList { get; set; }
public static TicketViewModel Translate(Ticket ticket)
{
return new TicketViewModel
{
Id = ticket.Id,
ClientRef = ticket.ClientRef,
PriorityRef = ticket.PriorityRef,
CategoryRef = ticket.CategoryRef,
CreatedDate = ticket.CreatedDate,
PrioritySelectList = GetPrioritySelectList(),
CategorySelectList = GetCategorySelectList()
}
}
}
Upvotes: 0
Views: 195
Reputation: 6335
I wouldn't worry about it too much to be honest.
I also agree that this is the way to go. If you want to be as safe as possible when changes happen, just make sure that you only have to change one layer and that's the translation layer.
The UI never or seldom changes, because it is a right pain in the proverbial to have to go and change every single view when any data bit changes. So, anything changes in the DB, like a field renamed for example or even newly added, you deal with it on the translation layer.
The UI will never be a 100% mirror of your business layer and that's totally fine.
Upvotes: 1