I’m trying to create an abstracted layer for ObjectContext. I understand OC is a Unit of Work, but I’m just not entirely how to write a good interface for it. Ideally I’d like to be able to swap out my ‘RealDataContext’ that implements IDataContext for something like a ‘FakeDataContext’ that would be fully in-memory.
The reason for this is that I want to be able to test my repositories against an in-memory database.
Thanks!
You should be able to create an extended class that derives from ObjectContext, and implements an IDataContext interface. To truly be able to mock the ObjectContext, your IDataContext interface would need to include matching signatures (or properties) for any member of ObjectContext that you are using and wish to mock. Something like the following should suffice:
Technically speaking, since DataContext inherits everything from ObjectContect, the implementation of IDataContext is taken care of by ObjectContext. You should not need any additional implementation in the DataContext class. So long as you always inject (or use a factory to create) instances of IDataContext rather than ObjectContext, you should be able to mock IDataContext when testing: