I’m going to develop a web-based system with MVC4 which will use web services (WCF, exactly) for getting data from data provider (I can’t use direct connection to SQL)
So, I have a question about using the web services in this project. Is this correct to use directly web service models as MVC model or I should create separate models for my MVC project and then map web service models to the models in MVC with an object-mapper (such as EmitMapper)?
Note: Consider that the web services may changes a lot.
No, don’t use your WCF serializable proxy classes as MVC Viewmodels – this will couple the SOA back end to your MVC front end unnecessarily.
The 2 classes have entirely different concerns – e.g. you may want to decorate your
ViewModelswith DataAnnotations likeUIHintetc which aren’t applicable to your WCF classes (and similarly, your WCF Proxy classes may have serialization attributes).Also, as your screens evolve, you will typically find that you may need to diverge the 2 models significantly – e.g. your screens need properties which your Service doesn’t need, and vice versa.
So yes, separate classes for WCF data serialization and for MVC ViewModels, and as you’ve suggested, if you keep to a standard naming convention, mappers like AutoMapper will do most of the work for you.