How do I use WCF reference with MVC3 (razor) model?

依然范特西╮ 提交于 2020-01-23 12:05:47

问题


I have a WCF Service and a MVC3(razor) web site. All my data comes from the WCF Service. I have my controllers and views but, how do I use the model with this structure ?

for now, I call my method from the service with a DataController and I called them in the Controllers..

Better solution ?


回答1:


Do you want to access a WCF service from your Model? Ouch...

Stick to the Controller. Controllers interact with services, models don't.




回答2:


1.You created web reference to your service - and entities was generated according to service data contract. This entities is your model.

2.If you need to decorate it by some ASP.NET MVC specific attributes - you can create other entities with same properties, create repository that get data from service and convert it to your entities and provide only methods needed - that's good practice.

3.Additionally, if you do some transformation and have some business rules - you can create business logic class, that call repository method, check and execute business rules. But if you only display data from service - don't create logic business class.

4.You call your logic/repository methods from controller and pass it to view. View is responsible only for displaying model, never call services from it!




回答3:


Better solution - create Service and Repository classes. For example, if you got the Person model, Create PersonRepository class which would do all the job related to calling wcf service and getting data. PersonService class would serve as business logic container, which would have reference to Repository class when in need for some data from repository. In this case, you would have great loose coupling between your models, business logic and wcf service repository. PersonService class would serve as validator, business logic container, etc



来源:https://stackoverflow.com/questions/6266682/how-do-i-use-wcf-reference-with-mvc3-razor-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!