I am building a silverlight application using MVVM pattern but I don't understand what I am missing here...
- ViewsProject (Silverlight Application) contains user controls and page. References ViewModelsProject.
ViewModelsProject (Silverlight class library) contains backend code of views. Also the business logic will reside in here. 开发者_Go百科 It will reference Models project.
ModelsProject (Windows class library) contains: Ado.net entity model of a local database and a WCF RIA DomainService built on the entity model. It's purpose is to get data from the local database and the entities are exposed via domain service.
Web project: Contains xap of ViewsProject.
Have I done everything correct so far? If yes then please help me understand:
How do I call the methods of the domain service in the ViewsModel project? If I reference the ModelsProject, I get the error that only silverlight projects can be added. If I change ModelsProject to a silverlight class library project then I can't add the entity model of my database.
For client projects to use WCF RIA Service you need to set WCF RIA Services Link in project property.
As for modularity and separation in project architecture when using WCF RIA Services, you can use WCF RIA Class libraries.
There isn't much sense in separating your views from your viewmodels IMHO. I've seen this approach used before but the view model (in my mind at least) represents a more 'view appropriate' abstraction of the data than the domain model can offer. The ViewModel provides a layer of indirection to allow the presentation to change without the model doing the same but it's little more than a sort of Adapter/Controller hybrid and lives alongside the view.
To answer your question though...
Typically, you'll have your SL project call your domain service (a web service usually) via commanding from your view to the viewmodel. The viewmodel command will then directly or indirectly make the web service call. This web service does not need to be a SL class library since it sits on the web server away from your client.
I get the feeling your thinking in terms of a desktop app here. Remember that the SL project runs client side and your data is not there. To wire all this up your gonna need some remote service to get the data from. A regular library reference is no good since the data is not on the client.
HTH, Stimul8d
I worked out the answer.
Change ViewModelsProject (Silverlight class library) to Silverlight Application type. Check Enable Ria services during creation of the project.
In the view model class, add using ModelsProject. Compile the ViewModelsProject.
All of the methods of the WCF Ria service will then appear in the code.
精彩评论