I have an application. This application uses an interface to access the database. This interface can be implemented by many classes. For example, one uses EF 4.4, but other classes can use EF5 that is more efficient. In the future perhaps I will use EF6 because it uses async methods. In this example all the methods use EF, but perhaps other options can be use other ways.
The application is coded once, using the interface, and according to the config file, use one implementation or the other, so I only need to modify the code in one place, the constructor, to add the new option in the instantiation of the class that is assigned to the interface.
At the moment all the methods of the classes are not async
, but in the future if I use EF6 I would like to use the async methods, so I don't know if it is possible that the class that use EF6 and implements the interface can use the async
methods.
For the async methods of EF6, I would use the async/awiat pattern, so in the method of my class I need to use the async attribute. This lets me use the await
keyword when I call to the async method of EF6.
But this class can implement the interface that in a first time is for synchronous methods?
Is there some way that in the main application I can use many implementations without the need to modify the code? Some implementations will use async methods while others will be synchronous.
async
isn't a part of the signature; an async and a non-async method can both implement the same interface method.