【问题标题】:MVVM, Dependency Injection & Runtime object creationMVVM、依赖注入和运行时对象创建
【发布时间】:2013-06-14 19:54:40
【问题描述】:

在 DI 中,当使用运行时参数在运行时创建不同的对象时,处理对象创建的最佳方式是什么?我读过Mark Seemann's answer regarding using abstract factories,它工作得很好,但我的问题涉及一个场景,即需要大量抽象工厂来创建不同的视图模型,具体取决于调用的命令。

例如,对于如下描述的应用程序

存储库层

public interface IMainRepository { }
public interface IOtherRepository { }

服务层

public interface IMainService { }
  public interface IOtherService { }

  public class MainService : IMainService
  {
    public MainService(IMainRepository mainRepository)
    {
      if (mainRepository == null)
      {
        throw new ArgumentNullException("IMainRepository");
      }
      _mainRepository = mainRepository;
    }

    readonly IMainRepository _mainRepository;
  }

  public class OtherService : IOtherService
  {
    public OtherService(IOtherRepository otherRepository)
    {
      if (otherRepository == null)
      {
        throw new ArgumentNullException("IOtherRepository");
      }
      _otherRepository = otherRepository;
    }

    readonly IOtherRepository _otherRepository;
  }

查看模型

public class MainViewModel
  {
    public MainViewModel(IMainService mainService, IOtherViewModelFactory otherViewModelFactory)
    {
      if (mainService == null)
      {
        throw new ArgumentNullException("IMainService");
      }
      _mainService = mainService;

      if (otherViewModelFactory == null)
      {
        throw new ArgumentNullException("OtherViewModelFactory");
      }
      _otherViewModelFactory = otherViewModelFactory;

      InitializeCommonds();
    }

    readonly IMainService _mainService;
    readonly IOtherViewModelFactory _otherViewModelFactory;

    public RelayCommand<int> CreateOtherViewModelCommand { get; set; }

    void InitializeCommonds()
    {
      CreateOtherViewModelCommand = new RelayCommand<int>(CreateOtherViewModel);
    }

    void CreateOtherViewModel(int otherId)
    {
      var otherVM = _otherViewModelFactory.Create(otherId);

      //Do other fantastic stuff...
    }
  }

  public class OtherViewModel
  {
    public OtherViewModel(IOtherService otherService, int otherId)
    {
      if (otherService == null)
      {
        throw new ArgumentNullException("IOtherService");
      }
      _otherService = otherService;

      _otherId = otherId;
    }

    readonly IOtherService _otherService;
    readonly int _otherId;
  }

查看模型工厂

public class OtherViewModelFactory : IOtherViewModelFactory
  {
    public OtherViewModelFactory(IOtherService otherService)
    {
      if (otherService == null)
      {
        throw new ArgumentNullException("IOtherService");
      }
      _otherService = otherService;
    }

    readonly IOtherService _otherService;

    public OtherViewModel Create(int otherId)
    {
      return new OtherViewModel(_otherService, otherId);
    }
  }

当从MainViewModel 调用CreateOtherViewModelCommand 成员时,IOtherViewModelFactory 抽象工厂依赖项用于创建OtherViewModel 视图模型。当MainViewModel 没有比这更复杂时,这工作得很好。当我在MainViewModel 中有许多其他命令创建其他视图模型类型时会发生什么?据我了解,我还需要为它们创建其他抽象工厂,但这不会导致构造函数膨胀,因为所有这些抽象工厂依赖项都是通过构造函数注入提供的?想象一个案例,我需要十个不同的抽象工厂来创建不同类型的视图模型!有没有更好的方法来实现我想要实现的目标?谢谢。

【问题讨论】:

    标签: c# mvvm dependency-injection


    【解决方案1】:

    您已经达到了一个点,像 Ninject 这样的 IoC 容器将对您有用。你定义你的具体实现如何映射到你的接口,然后向 IOC 容器请求一个对象。它继续为您构造对象,并提供所有适当的实现。

    【讨论】:

    • 假设我无法使用 Ninject 做任何事情,我很想知道一个合适的答案。不过,我会看看 Ninject。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-31
    • 1970-01-01
    • 1970-01-01
    • 2015-01-22
    • 2012-11-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多