C# MVVM、依赖注入&;运行时对象创建
在DI中,当在运行时使用运行时参数创建不同的对象时,处理对象创建的最佳方法是什么?我已经读过这本书,它的效果很好,但我的问题涉及到一个场景,根据调用的命令,创建不同的视图模型需要许多抽象工厂 例如,对于如下所述的应用程序 存储库层C# MVVM、依赖注入&;运行时对象创建,c#,mvvm,dependency-injection,C#,Mvvm,Dependency Injection,在DI中,当在运行时使用运行时参数创建不同的对象时,处理对象创建的最佳方法是什么?我已经读过这本书,它的效果很好,但我的问题涉及到一个场景,根据调用的命令,创建不同的视图模型需要许多抽象工厂 例如,对于如下所述的应用程序 存储库层 public interface IMainRepository { } public interface IOtherRepository { } public interface IMainService { } public interface IOthe
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 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;
}
当从
MainViewModel
调用CreateOtherViewModelCommand
成员时,IOtherViewModelFactory
抽象工厂依赖项用于创建OtherViewModel
视图模型。当MainViewModel
没有变得比这更复杂时,这种方法可以很好地工作。如果在MainViewModel
中有许多其他命令创建其他视图模型类型,会发生什么情况?据我所知,我还需要为它们创建其他抽象工厂,但这不会导致构造函数膨胀,因为所有这些抽象工厂依赖项都是通过构造函数注入提供的吗?想象一下,我需要十个不同的抽象工厂来创建不同类型的视图模型!有没有更好的方法来实现我的目标?谢谢。您已经到了一个地步,像这样的IoC容器将对您有用。定义具体实现如何映射到接口,然后向IOC容器请求对象。它继续为您构建对象,并提供所有适当的实现 假设我无法将Ninject用于任何用途,我想知道一个合适的答案。不过,我会看看Ninject。