MVVM Linq2Sql and DataContext
I am trying to figure out how to work with Linq2Sql in an MVVM (WP7) application. I think I understand the basics of MVVM, I've written some simple testable apps with this pattern, but I'm not sure exactly how I should combine it with L2S.
I don’t understand something. How do I manage the DataContext? I have one application (how else am I going to track changes in the entity and propagate them to the DB)? Can I bind directly to the generated L2S classes? How to track changes made to the database? Or should I use the repository pattern for this, what are the event callback sets?
Another thing is to make VMs easy to test, I use a dependency injection and services system for some systems of the application. A typical example would be a service for downloading from the Internet - to make it available for testing, I create and interact, as well as two classes that implement it - one for production and one for testing, which returns dummy data. My question is, should I be doing this to access the database? Let's say all L2S requests will be executed only inside this service, completely protecting the rest of the application from L2S. Or is it pointless and I should just have a global DataContext / singleton and access it from anywhere?
As you can see, I have a lot of questions :) I would like to hear your opinions
source to share
I think you are on the right track, perhaps following the repository pattern. If you like IOC follow the same path. I would probably look at mocking your data interfaces, but I ended up getting better at it.
If you are worried about the behavior of the db context, it itself (like update tracking) is not ... Not from a unit test perspective. This is a more comprehensive integration test (kind of funny from a phone app perspective, but you know what I mean!)
Remember that there is only one user on your phone and remember what to do.
source to share