2009-07-16 48 views
4

我在我的MVVM應用程序中有相當多的parent-detail ViewModels。事情是這樣的:MVVM和StructureMap使用

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

等等......

此外,單一的視圖模型可以出現在多個位置,這取決於用戶是否被課或學生等瀏覽

每個子視圖模型是由父視圖模型創建的,那麼多視圖模型的需要有傳入的子視圖模型的依賴性例如,對於SchoolsViewModel的構造可能是:

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

現在,使所有這些易於管理的通常方法是使用一個DI框架(如StructureMap)將所有必需的參數傳遞給視圖模型。但是,因爲在這種情況下,我的應用程序通常只會創建SchoolsViewModel,這是有限的使用。

我的第一個問題是,在這種情況下,您是否將SchoolsViewModel傳遞給每個子視圖模型的每個依賴項,還是讓每個視圖模型使用ObjectFactory.GetInstance()創建子視圖模型?也許通過工廠類來抽象出DI框架的依賴關係?

有與此另一個問題:MVVM: locating other ViewModels

編輯:我已經打開了這個賞金,因爲我想更多的意見。

回答

0

使用依賴注入的好處是,如果SchoolsViewModel本身不需要知道關於,例如,教師庫,那麼它甚至不需要在構造函數中引用它。儘管父母對此一無所知,但子ViewModel仍然能夠處理teachersRepository。這可以防止父視圖模型受到其實際並不需要的依賴關係的污染。

1

另一種替代方案...

看看這個LessonViewModel。它僅取決於學生和教師,並且對PupilParents或其他任何兒童對象一無所知。

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); 
    } 
} 

課程工廠包含所有必需的依賴項,但它對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 
     }; 
    } 
} 
+0

但我恐怕這太複雜了......最好有一個教育工廠結合所有的工廠。 – alex2k8 2009-07-23 13:16:42

0

也許我沒有看到大局嗎?你不能使用StructureMap和它的所有基礎來幫你照顧這個骯髒的工作嗎?您可以使用StructureMap的構造函數注入來處理所有這些工作。通過連接一個接口以及它和它的子接口所依賴的所有接口,並將各種依賴接口放入需要它們的各種對象的構造函數中...當實例化具有依賴關係的對象1時對象2反過來依賴於對象3 ... StructureMap將全​​部處理。

也許我錯過了什麼?