简体   繁体   English

MVVM Light 创建多个 DataService 实例

[英]MVVM Light create multiple instance of DataService

right now I am using MVVM Light to achieve the MVVM Pattern.现在我正在使用 MVVM Light 来实现 MVVM 模式。 So in my view I create multiple tabs and bind them to multiple instances of one ViewModel.因此,在我看来,我创建了多个选项卡并将它们绑定到一个 ViewModel 的多个实例。 I achieve this with:我通过以下方式实现了这一目标:

ServiceLocator.Current.GetInstance<ViewModel>(key);

When I do this, every instance of ViewModel is connected to the same one instance of DataService registered in the ViewModelLocator:当我这样做时,ViewModel 的每个实例都连接到在 ViewModelLocator 中注册的同一个 DataService 实例:

SimpleIoc.Default.Register<IDataService, DataService>();

But I want to have for every instance of the Viewmodel also one instance of Dataservice.但我希望 Viewmodel 的每个实例也有一个 Dataservice 实例。 Why?为什么? Because each instance of ViewModel has the same function but requires other data.因为 ViewModel 的每个实例都有相同的功能,但需要其他数据。

How do I create in MVVM Lights ViewModelLocator a new instance of DataService when for a new Instance of ViewModel?对于新的 ViewModel 实例,如何在 MVVM Lights ViewModelLocator 中创建 DataService 的新实例? Is this possible or isn't that a good approach in the MVVM Pattern and I failed to understand DataService correctly?这是可能的还是不是 MVVM 模式中的一个好方法,我未能正确理解 DataService?

You can use the overloaded version of Register method to create multiple instances of the data service.您可以使用Register方法的重载版本来创建数据服务的多个实例。

SimpleIoc.Default.Register<IDataService>(()=>new DataService(),"viewmodel1");
SimpleIoc.Default.Register<IDataService>(()=>new DataService(),"viewmodel2");

All the answers above did not work for me too, so i remastered them a bit.上面的所有答案对我也不起作用,所以我重新掌握了它们。

You register your Data Service instance normally:您通常注册您的数据服务实例:

SimpleIoc.Default.Register<IDataService, DataService>();

Afterwards you insert factory method by registering your ViewModel instance to get a new Instance (and not the cached one) of your Data Service directly in the constructor of ViewModel:之后,您通过注册 ViewModel 实例插入工厂方法,以直接在 ViewModel 的构造函数中获取数据服务的新实例(而不是缓存的实例):

SimpleIoc.Default.Register<ViewModel>(() => new ViewModel(SimpleIoc.Default.GetInstanceWithoutCaching<IDataService>()));

SimpleIoc will return the same cached instance , if you want a new fresh instance on every call, use one of the Register method overloads: SimpleIoc 将返回相同的缓存实例,如果您希望在每次调用时都有一个新的新实例,请使用 Register 方法重载之一:

public void Register<TClass>(Func<TClass> factory) where TClass : class{}

So, in your case will be something like所以,在你的情况下会像

SimpleIoc.Default.Register<IDataService>(() => new DataService());

EDIT- You're right, probably this answer will guide you in the right direction.编辑 - 你是对的,这个答案可能会引导你走向正确的方向。 I'd recommend you to use a full featured IOC container (I've used Autofac and SimpleIoc with success) where the lifestyle can be properly assigned.我建议您使用功能齐全的 IOC 容器(我成功地使用了AutofacSimpleIoc ),其中可以正确分配生活方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM