MVVM-Light 工具包 V4 Beta 中的 IDataService
我正在学习 Silverlight for WP7,并偶然发现了 MVVM Light 工具包。我认为学习最新的东西是个好主意,所以我安装了 V4 Beta。遗憾的是还没有任何文档(还?)。 在模型文件夹中有 3 个文件,DataItem、DataService 和 IDataService。
public class DataItem
{
public DataItem(string title)
{
Title = title;
}
public string Title { get; private set; }
}
public class DataService : IDataService
{
public void GetData(Action<DataItem, Exception> callback)
{
// Use this to connect to the actual data service
var item = new DataItem("Welcome to MVVM Light");
callback(item, null);
}
}
public interface IDataService
{
void GetData(Action<DataItem, Exception> callback);
}
MainViewModel 使用这些类来获取属性的值。 现在的问题是:这些是您应该使用的类(特别是 IDataService)吗?我似乎找不到有效使用它们的方法,因为 DataItem 仅包含一个字符串(或者它意味着用作基类?)。
I'm learning Silverlight for WP7 and stumbled upon the MVVM Light toolkit. I thought it would be a good Idea to learn the newest thing so I installed the V4 Beta. Sadly there isn't any documentation to it (yet?).
In the Model-Folder there are 3 Files, DataItem, DataService and IDataService.
public class DataItem
{
public DataItem(string title)
{
Title = title;
}
public string Title { get; private set; }
}
public class DataService : IDataService
{
public void GetData(Action<DataItem, Exception> callback)
{
// Use this to connect to the actual data service
var item = new DataItem("Welcome to MVVM Light");
callback(item, null);
}
}
public interface IDataService
{
void GetData(Action<DataItem, Exception> callback);
}
These classes are used by the MainViewModel for getting the value of a property.
Now to the question: Are these the classes you should use (specifically IDataService)? I can't seem to find a way to use them effectively because the DataItem only contains a string (or is it meant to be used as a base class?).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我已成功使用 IDataService 来提供测试和虚拟数据挂钩。您拥有的代码是您应该如何使用它的示例。 DataItem 是表示来自数据库或服务的数据的“实体”或“DTO(数据传输对象)”的示例。如果您使用 WCF,它将是您执行“添加服务引用”时生成的对象。 DataService 类是接口的表示。该 DataService 类将具有调用真实 Web 服务并执行 CRUD 操作的方法。您还可以拥有具有相同方法的 DesignTimeDataService: IDataService,但使用内存中的 foreach 创建数据。然后,您可以使用 IoC 或其他依赖注入在运行时注入实现。
在 Silverlight 中的 App.xaml.cs 中,我创建一个 IDataService 并在整个应用程序中使用它:
I've used the IDataService successfully to provide a testing and dummy data hook. The code that you have is an example of how you should use it. The DataItem is an example of an "Entity" or "DTO (Data Transfer Object)" that represents data from the database or service. If you're using WCF it would be the object that is generated when you do "Add Service Reference". The DataService class is a representation of the interface. This DataService class would have methods to call the real web service and do CRUD actions. You could also have a DesignTimeDataService: IDataService that has the same methods, but creates the data using a foreach in memory. You could then use IoC or other Dependency injection to inject the implementation at runtime.
In my App.xaml.cs in Silverlight, I create an IDataService and use that throughout my application:
MVVM Light 提供的结构以及示例是根据您的应用程序连接到服务来考虑的。
Laurent 在他的视频了解模型-视图-视图模型模式中对此进行了解释
,这里< a href="http://blog.galasoft.ch/archive/2011/02/11/deep-dive-mvvm-at-mix11.aspx" rel="nofollow">DEEP DIVE MVVM at #MIX11
对模型有很好的解释。
我所做的是将 DataService 替换为我的 onw 库,以通过异步 HTTPRequest 访问 REST 模式。
由于异步通信的性质,我决定不使用回调(),因此我将其删除,但使用消息通过消息的有效负载发回数据。
根据我的理解,您必须使用这些类,因为 ViewModel 是使用 IDataService 作为参数进行初始化的,而且我还没有找到任何方法来更改它。
希望有帮助。
The structure provided form MVVM Light, as well as the example, is thought with your application connecting to a service.
Laurent explains it in his video Understanding the Model-View-ViewModel Pattern
and here DEEP DIVE MVVM at #MIX11
there is a very good explanation of the model.
What I've done is to replace the DataService with my onw libraries to access the REST mode via async HTTPRequest.
Due to the nature of the async communication I decided not to use the callback(), so I've removed it, but used messages to send back the data via the payload of the message.
In my understanding you have to use those classes as the ViewModels are initialized with the IDataService as parameter and I've not found any way to change it.
Hoping it helps.