模型仅在 WCF 服务中,或者是否需要客户端中的副本?
我正在构建一个应用程序。我正在 MVVM Light 的帮助下创建 Silverlight 4 客户端。我正在从 WCF 服务获取数据。至少,这是计划。
在 WCF 服务中,我定义了我的应用程序中需要使用的“实体”。当我在 Silverlight 客户端中添加对 WCF 服务的引用时,Visual Studio 会在客户端重新创建服务中标有属性 [DataContract] 的所有类。
我想知道这是否是一种不好的做法,以及在客户端内创建模型是否更好。据我了解,在第一种情况下,我应该只在 Silverlight 客户端中创建 ViewModel 和视图,而在第二种情况下,我应该在 Silverlight 客户端中创建视图、ViewModel 和模型,并使用以下内容填充模型实例:来自 WCF 服务的值。
感谢您的帮助。
干杯, G.
更新
好吧,我认为我的问题不够清楚,因为我还没有收到很多反馈。 不过,我想提供有关此事的最新情况。我正在寻找的答案是“不!数据传输对象!”。
我正在考虑使用我的实体类(映射到数据库表的实体类)作为 WCF 服务中的 DataContract。在客户端中添加对此 WCF 服务的引用也会在客户端上创建所有用 DataContract 修饰的类。
在我的例子中,最大的问题是数据层基于 Hibernate,它有时在运行时广泛使用“数据代理”类(请参阅 Castle Proxy)。好吧,事实证明这些数据代理存在序列化问题,据我了解,最好的方法是采用数据传输对象模式,以便将“复杂”实体映射到类似但“更轻”的类(DTO)。
我希望这可以帮助别人。
祝你今天过得愉快! 吉安卢卡.
I am building an application. I am creating a Silverlight 4 client with the help of MVVM Light. I am acquiring data from a WCF Service. At least, this is the plan.
In the WCF Service I have defined the "entities" that I need to use in my application. When in the Silverlight Client I add a reference to my WCF Service, Visual Studio recreates on the client-side all classes that were marked with the attribute [DataContract] in the service.
What I would like to know is if this is a bad practice and if it were better to create the Models inside the Client. As far as I understand, in the first case I should only create the ViewModels and the Views in the Silverlight client, whereas in the second case I should create the Views, ViewModels and Models inside the Silverlight client, and populate the Models instances with the values coming from the WCF Service.
Thank you for your help.
Cheers,
G.
UPDATE
Ok, I don't think my question was clear enough as I havent not received many feedbacks.
However, I'd like to provide an update on this. The answer I was looking for is "No! Data Transfer Objects!".
I was thinking to use my entity classes (the ones mapped to the DB tables) as DataContract in a WCF Service. Adding a reference to this WCF service in a client would have created all the classes decorated with DataContract on the client too.
The big problem in my case is that the data layer is based on Hibernate, which soemtimes makes extensive use at runtime of "data proxy" classes (see Castle Proxy). Well, it turned out that there is a serialization issue with these data proxies, and as far as I understood the best approach is to adopt the Data Transfer Objects pattern in order to map the "complex" entities to a similar but "lighter" class (the DTO).
I hope this can help someone else.
Have a nice day!
Gianluca.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你看过 WCF RIA 和 Nhibernate 吗?不过,为了尝试回答这个问题:我不会尝试直接从 WCF 服务返回实体,我个人会创建 DTO。然后我可能会将这些 DTO 映射到某种客户端模型。因此,如果我无法利用 RIA,这就是我会尝试做的事情。
Have you looked at WCF RIA with Nhibernate? To try and answer the question though: I wouldn't try and return entities from the WCF service directly, I personally would create DTO's. And then I would probably map those DTO's to some sort of a client side model. So, that's what I would try and do if I wasn't able to take advantage of RIA.