Im trying to get my head around how to work with Linq2Sql in an MVVM (WP7) application. I think I understand the basics of MVVM, I wrote a few simple testable apps with this pattern, but I am not sure how exactly should I combine it with L2S.
I dont understand a few things. How do I manage the DataContext? Do I have one per app (how else would I track the changes to the entities and propagate them to the DB)? Can I bind directly to the generated model L2S classes? How do I track changes done on the database? Or should I use the repository pattern for that, which sets of event callbacks?
Another thing, to have the VMs easily testable, I use a system of dependency injections and services for some systems of the app. Typical example would be a service for downloading from the internet - to make it testable, I make and interface and two classes that implement it - one for production and one for testing, that returns dummy data. My question is, should I do that for the database access too? Say all L2S queries would be done only inside this this service, shielding the rest of the app from the L2S altogether. Or is is pointless and I should simply have a global DataContext object / singleton and access it from anywhere?
As you can see, I have many question :) I would love to hear your opinions
I think you are on the right track, perhaps follow the repository pattern. If you are comfortable with IOC follow the same path. I would probably look into mocking your data interfaces though, I find it easier in the long run.
If you are concerned about the db context behaviour it self (eg update tracking) don't... Not from a unit test perspective. That's more a full blown integration test (kind of amusing from a phone app perspective but you know what I mean!)
Remember that there is only one user on your phone and keep the toomstoning in mind.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With