How do I use WCF reference with MVC3 (razor) model?
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 struct开发者_开发百科ure ?
for now, I call my method from the service with a DataController
and I called them in the Controllers..
Better solution ?
Do you want to access a WCF service from your Model? Ouch...
Stick to the Controller. Controllers interact with services, models don't.
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!
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
精彩评论