Jaki jest najlepszy sposób na zbudowanie modelu widoku?
Korzystam z asp.net mvc z ramą podmiotu i zaczyna się uczyć DDD. Pracuję nad projektem, który zawiera ankiety. Oto mój model domeny:
public class Survey
{
public int? SurveyID { get; set; }
public string Name { get; set; }
public decimal MinAcceptanceScore { get; set; }
public int UserFailsCount { get; set; }
public IEnumerable<SurveyQuestion> Questions { get; set; }
public IEnumerable<Prize> Prizes { get; set; }
public IEnumerable<SurveyAttempt> UserAttempts { get; set; }
}
Potrzebuję różnych części ankiet dla różnych widoków, więc stworzyłem różne ViewModels:
public class ShortSurveyViewModel
{
public int? SurveyID { get; set; }
public string Name { get; set; }
public int UserFailsCount { get; set; }
public IEnumerable<SurveyAttempt> UserAttempts { get; set; }
}
public class ShortSurveyWithPrizesViewModel
{
public int? SurveyID { get; set; }
public string Name { get; set; }
public int UserFailsCount { get; set; }
public IEnumerable<SurveyAttempt> UserAttempts { get; set; }
public IEnumerable<Prize> Prizes { get; set; }
}
public class SurveyEditViewModel
{
public int? SurveyID { get; set; }
public string Name { get; set; }
public decimal MinAcceptanceScore { get; set; }
public int UserFailsCount { get; set; }
public IEnumerable<SurveyQuestion> Questions { get; set; }
public IEnumerable<Prize> Prizes { get; set; }
}
Jaki byłby najlepszy sposób na zbudowanie mojej architektury, jeśli chcę, aby moje repozytorium ankiet uzyskało informacje potrzebne do modelu widoku z aprobatą?
Różne solusje, które widzę:
Repozytorium może zwrócić IQueryable do SurveyService, a usługa może zwrócić model widoku appropriete, ale waham się, czy to jest właściwe, ponieważ uważam, że modele widoku powinny być tworzone w interfejsie użytkownika, a nie w warstwie usług.
Utwórz trzy odpowiednie klasy w mojej warstwie domeny. Ale teraz domena będzie zależna od reprezentacji i przy każdym nowym widoku należy utworzyć nową klasę domeny.
Pobierz pełny obiekt domeny i mapuj tylko właściwości, które są potrzebne do konkretnego widoku. To nie jest dobre, ponieważ w moim przykładzie pytania wymagały tylko jednej reprezentacji i może to być ciężka kolekcja.