I am developing a WPF desktop app that uses Entity Framework 4 and SQL Compact 4. I have seen two distinct styles of Repository
classes:
The
Repository
instantiates anObjectContext
, which is disposed of when theRepository
is garbage-collected. The lifetime of theObjectContext
is the same as the lifetime of the application.A separate
DataStoreManager
class creates and holds anObjectContext
for the life of the application. When a repository is needed, a command gets anObjectContext
reference from theDataStoreManager
and passes it to the constructor for the New Repository. The lifetime of theObjectContext
is the lifetime of the application.
Is either approach considered a bad practice? Does either present any absolute advantages over the other? Is either approach considered best practice? Is either more widely accepted or used by developers than the other? Thanks for your help.
See Question&Answers more detail:os