WPF MVVM共享数据源 [英] WPF MVVM Shared Data Source

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

问题描述

我被分配了一个新项目. 我熟悉WPF和mvvm,并且已经完成了一些利用它的生产项目.

此最新请求很简单–但是需要在两个视图之间共享传入的数据源(每30秒刷新一次).这是我想到的几种方法:

1)在两个视图之间创建一个共享视图模型.我不太喜欢这个选项-尽管它们确实使用相同的数据源,但是两个视图对数据的处理却大不相同.

2)创建两个视图模型,其中一个是父"视图模型,一个是子视图模型.子视图模型注册来自父视图的消息.父视图模型发送该消息.尽管这是最容易启动和运行的代码-我觉得会有很多代码围绕应用程序的状态运行(是否刚刚加载?是否已经加载?这是数据刷新吗?). /p>

理想情况下,我想要的是某种广播模式,在该模式中检索并广播数据-创建每个视图模型,然后(通过某种类型的Messenger)获取广播的消息.然后,在随后的更新中,Messenger使视图模型知道即将进行更新.

我已经研究了中介模式(使用消息传递),但这并不能完全满足我的需求.我不想创建视图模型来注册消息.

我知道,可能没有某种灵丹妙药"解决方案.只是寻找最佳的方法来解决这个问题.

解决方案

使用控制反转(IOC)和服务定位器模式来创建共享的数据服务,它们也都在通话.我注意到您的mvvm-light标签;我知道默认的Mvvm-light项目使用ViewModelLocator类和SimpleIOC,因此您可以注册如下数据服务.

public class ViewModelLocator
{
    static ViewModelLocator()
    {
        ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
        SimpleIoc.Default.Register<IDataService, DataService>();
    }
}

使用了一个接口,因此我们可以随意换出DataService,甚至在设计时也可以换一个接口.因此,您将创建一个接口,该接口定义将要使用的方法,并确保您的DataService实现了它们.在您的DataService实现中,您将使用共享上下文/源.

public class DataService: IDataService
{
     //USE SAME SOURCE (example EF)
     private SharedSourceEntities context;

     (blah blah)...
}

在构造器或调用服务定位器中将其注入到两个视图模型中之后.

依赖注入:

public class ViewModelOne: ViewModelBase
{
    private readonly IDataService dataService;

    public ViewModelOne(IDataService dataService) 
    { 
        this.dataService = dataService;
    }
}

public class ViewModelTwo: ViewModelBase
{
    private readonly IDataService dataService;

    public ViewModelTwo(IDataService dataService) 
    { 
        this.dataService = dataService;
    }
}

服务位置:

SimpleIoc.Default.GetInstance<IDataService>();

I have been tasked with a new project. I am familiar with WPF and mvvm and have completed a few in production projects utilizing it.

This latest request is simple – but requires sharing an incoming datasource (that is refreshed every 30 seconds) between two views. Here are a few of the ways I have thought of achieving this:

1) Create one shared view model between the two views. I don’t really like this option – while they do use the same data source, what the two views do with the data is quite different.

2) Create two view models where one of them is the "parent" view model and one is the child. The child view model registers for a message from the parent. The parent view model sends that message. While this one would be the easiest to get up and running – I feel like there would be a lot of code revolving around the state of the application (was it just loaded? Is it already loaded and this is a data refresh?).

Ideally, what I would like is some sort of broadcast pattern, where the data is retrieved and broadcast – each viewmodel is created and then grabs the broadcasted message (via some type of messenger). Then on subsequent updates, the messenger lets the viewmodels know an update is coming.

I’ve looked at the mediator pattern (using messaging) but this doesn’t quite meet what I need. I don’t want to create the viewmodels in order to register for a message.

I am aware that there probably isn’t some "silver bullet" solution. Just looking for ideas on the best way to approach this.

解决方案

Use Inversion Of Control (IOC) and the Service Locator pattern to create a shared data service they both talk too. I notice your mvvm-light tag; I know a default Mvvm-light project uses a ViewModelLocator class and SimpleIOC so you can register a data service like below.

public class ViewModelLocator
{
    static ViewModelLocator()
    {
        ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
        SimpleIoc.Default.Register<IDataService, DataService>();
    }
}

An interface is used so we can swap out the DataService at will and even have a different one for design time. So you will create an interface that defines methods you will use and just make sure your DataService implements them. It is in your DataService implementation you will use the shared context / source.

public class DataService: IDataService
{
     //USE SAME SOURCE (example EF)
     private SharedSourceEntities context;

     (blah blah)...
}

After you can inject it into both view models either in the constructor or calling the service locator.

Dependency Injection:

public class ViewModelOne: ViewModelBase
{
    private readonly IDataService dataService;

    public ViewModelOne(IDataService dataService) 
    { 
        this.dataService = dataService;
    }
}

public class ViewModelTwo: ViewModelBase
{
    private readonly IDataService dataService;

    public ViewModelTwo(IDataService dataService) 
    { 
        this.dataService = dataService;
    }
}

Service location:

SimpleIoc.Default.GetInstance<IDataService>();

这篇关于WPF MVVM共享数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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