MVVM - Model Tutorials?
I'm currently creating a WPF app in C# and have been following this diagram . I've read several articles on MVVM but I 'm specifically looking for details on everything below the dotted box (so the Model and Data access). I already have a local database 开发者_Python百科setup (SQLite) and I'm looking for an efficient way of connecting it to my application.
Are there any good articles or demos of creating this specific part for MVVM?
There are many ways to approach this problem and which one you choose will be determined by the complexity of your application and your data model.
The standard way to approach this from a .NET point of view would be to create an Entity Framework model on top of your SQLite database. This will generate your object model and allow you to attach behaviour to it without too much fuss. It also somewhat abstracts the database/persistence from your application code (ViewModels). For details on this approach, search Google for 'Entity Framework' - there is a ton of information out there. EF has improved dramatically since the release of EF 4.0.
If you don't want to go with EF (due to complexity, overhead or just the desire to roll your own) then you're going to want to read up on popular data source architectural patterns. Start with Martin Fowler's Patterns of Enterprise Application Architecture - this book is the bible of data-driven application architecture. If you don't want to buy the book then read through the pattern descriptions, find an appropriate one and do a Google search - there is a ton of information about this stuff on the web.
If you have a complex model with loads of behaviour and patterns then you'll want to look at the Domain Model pattern for your model and probably the Data Mapper data source pattern for getting the data out of your DB and into your model. This is the most complicated approach, but the complexity pays off in flexibility down the road, if you need it.
If your data model is simpler then you may want to use either Table Data Gateway or Row Data Gateway for data access and probably Transaction Script or Service Layer for communication between the model and database.
I apologize for essentially bombarding you with links but the reality is when deciding on an architecture for an application it's important to do lots of research and above all learn and rely upon known solutions. Good luck.
The diagram linked to is a bit more complicated than MVVM. MVVM refers only to Model, View and ViewModel. It leaves the data access strategy up to the programmer.
I use MVVM, but I don't like my Model classes to contain logic for loading data from the Data Access layer (in fact, I don't like Model classes to contain any programming logic apart from update notifications via INotifyPropertyChanged).
The pattern I'm using at the moment has an additional package named Repository. The ViewModel calls into the Repository telling it to get data. The Repository then get's the data from the database (or Web Service, or File System etc) and populates the Model objects. The ViewModel is observing the Model objects, so changes get propagated to the UI.
精彩评论