MVVM和StructureMap的用法 [英] MVVM and StructureMap usage

查看:61
本文介绍了MVVM和StructureMap的用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的MVVM应用程序中有大量的父级细节ViewModel。像这样的东西:

I have quite a large number of parent-detail ViewModels in my MVVM application. Something like this:

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

依此类推...

此外,单视图模型还可以

In addition, a single view model can appear in more than one place, depending on whether the user is browsing by lesson or pupil, etc.

每个子视图模型是由父视图模型创建的,依此类推。许多视图模型都需要传入子视图模型的依赖关系。例如,SchoolsViewModel的构造函数可能是:

Each child view model is created by the parent view model, and so many of the view models needs to have the dependencies of the child view model passed in. For example the constructor for SchoolsViewModel might be:

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

现在,使所有这些易于管理的常用方法是使用DI框架(例如StructureMap)将所有必需的参数传递给视图模型。但是,由于在这种情况下,我的应用程序通常只会创建SchoolsViewModel,因此用途有限。

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

在这种情况下,我的第一个问题是,是让SchoolsViewModel将每个依赖项传递给每个子视图模型,还是让每个视图模型都使用ObjectFactory.GetInstance( )创建子视图模型?也许通过工厂类来抽象出对DI框架的依赖?

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

还有另一个与此相关的问题: MVVM:查找其他ViewModel

There is another question relating to this: MVVM: locating other ViewModels

编辑:我已经为此悬赏了希望获得更多意见。

I've opened a bounty on this as I'd like more opinions.

推荐答案

另一个替代方案...

One other alternative...

查看此LessonViewModel。它仅取决于学生和教师,对PupilParents或任何其他子对象一无所知。

Look at this LessonViewModel. It depends only on Pupils 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 lesson factory contains all required 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
        };
    }
}

这篇关于MVVM和StructureMap的用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆