While working on a LOB desktop application with a lot of CRUD operations using PRISM and Enterprise Library, I have noticed a recurring pattern that seems annoying. For every domain model entity (eg. Contact) I find my self wrapping it with a view model (eg. ContactVM) then I introduce a new ContactsVM
(notice the 's') where the latter class accepts a repository interface which is used to populate an ObservableCollection<ContactVM>
and for every Contact
entity that I read from the repository, I wrap it in a ContactVM
which I pass the entity to via the constructor along with other enterprise library services needed by my ViewModel.
The problem is that all my view model constructors started taking this pattern like this:
ViewModel(EntityToWrap e, DependencyFromEntLib, OtherDependencies ...)
Now that is a problem because most tools and libraries require a default parameterless constructor (eg. some commercial data grids need that to provide filtering support), plus you can't use design data to visualize entities because they need parameterless constructors too. and finally the question: What is the right way to build view models and should Entlib services be provided via constructors or via the ServiceLocator ?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…