Using MVVM and StructureMap

I have a fairly large number of ViewModels with parent drilldown in my MVVM application. Something like that:

SchoolsViewModel +- SchoolViewModel +- LessonViewModel +- PupilsViewModel +- PupilViewModel +- TeacherViewModel +- PupilsViewModel +- PupilViewModel +- LessonsViewModel +- TeachersViewModel 

And so on...

In addition, one viewing model can be displayed in more than one place, depending on whether the user is viewing a lesson or student, etc.

Each model of the child view is created by the model of the parent view, so many view models must have dependencies on the model of the child view. For example, the constructor for SchoolsViewModel could be:

 SchoolsViewModel(ISchoolsRepository schoolsRepository, ILessonsRepository lessonsRepository, IPupilsRepository pupilsRepository, ITeachersRepository teachersRepository, ...) 

Now, the usual way to make all this manageable is to use a DI framework such as StructureMap to pass all the necessary arguments to the view model. However, since in this case my application will usually only create SchoolsViewModel, this will be limited use.

My first question is: in this case, could you make the SchoolsViewModel transition in each dependency to each child view model, or would you use each ViewFactory.GetInstance () view model to create child view models? Perhaps through the factory class to abstract the dependency on the DI framework?

There is another question related to this: MVVM: searching for other ViewModels

EDIT: I discovered the generosity on this, as I would like more opinions.

+4
source share
3 answers

Another alternative ...

Take a look at this LessonViewModel. It depends only on the students and teachers, and knows nothing about PupilParents or any other child object.

 public class LessonViewModel { private IPupilsFactory _pupilsFactory; private ITeachersFactory _teachersFactory; public LessonViewModel(IPupilsFactory pupilsFactory, ITeachersFactory teachersFactory) { _pupilsFactory = pupilsFactory; _teachersFactory = teachersFactory; } public string Name { get; set; } public List<string> PupilNames { get; set; } public string TeacherName { get; set; } public PupilViewModel GetPupil(string name) { return _pupilsFactory.Create(name); } public TeacherViewModel GetTeacher() { return _teachersFactory.Create(TeacherName); } } 

The factory lesson contains all the necessary dependencies, but it also knows nothing about PupilParents.

 public interface ILessonsFactory { LessonViewModel Create(string name); } public class LessonsFactory : ILessonsFactory { private ILessonsRepository _lessonsRepository; private IPupilsFactory _pupilsFactory; private ITeachersFactory _teachersFactory; public LessonsFactory(ILessonsRepository lessonsRepository, IPupilsFactory pupilsFactory, ITeachersFactory teachersFactory) { _lessonsRepository = lessonsRepository; _pupilsFactory = pupilsFactory; _teachersFactory = teachersFactory; } public LessonViewModel Create(string name) { Lesson lesson = _lessonsRepository.Read(name); return new LessonViewModel(_pupilsFactory, _teachersFactory) { Name = lesson.Name, PupilNames = lesson.PupilNames, TeacherName = lesson.TeacherName }; } } 
+1
source

The advantage of using dependency injection would be that if SchoolsViewModel itself did not need knowledge, say, teacherRepository, then it would not even need a reference to it in the constructor. The ViewModel child will still be able to get a call to the Repository teacher, although the parent did not know anything about it. This prevents the parent ViewModel from getting dirty with dependencies that it really doesn't need.

0
source

Perhaps I do not see a big picture here? Can't you use StructureMap and all its fundamentals to take care of this dirty job for you? You can use the StructureMap constructor to do all this work. By connecting the interface and all the interfaces to which it and it depend on the StructureMap structure, and placing various dependent interfaces in the constructor of the various objects that they need ... when you create an instance of object 1, which has a dependency on object 2, which, in its the queue is object-dependent 3 ... StructureMap will take care of all this.

Maybe I missed something?

0
source

All Articles