In this section, I am going to use Repository Pattern for illustrating extensibility via interfaces. Below is the sample image of Repository Pattern from high level.
Basically, Repository Patterns are a way to isolate your application from data store. This way application will only make standard calls to repository. How these get executed behind the scenes is not app’s concern. In other words, application only cares about the contract defined by the repository. Generally, either Business Layer or Domain Layer interacts with Repository, but here we are skipping these layers just to demonstrate the contract implementation.
Let’s suppose we would like to buid a system where in we can easily interact with different data sources viz databases like MS-Sql Server, Oracle, NoSql, Mongo, Offline data storage, cloud storage. The easiest way to prepare all these different data sources is to use repository pattern. Basically, it’s layer to separate our app from data storage.
Below, in the snippet I have just created one interface for CRUD operations, which will act as an abstraction here between app and different data sources.
and below is the view model which will get filled by from different data-sources.
And, here is the class which is implementing the above interface. Since, this is just for demo purpose, hence I have not fetched data from SQL Source. Rather than that, I just returned dummy data assuming data already fetched from particular data-source.
On similar lines, I have classes for AzureRepo and AWSRepo. But, underlying implementation is just like the above one. I have also created Repository Factory, to serve different requests based on different inputs. Below is the snippet for the same.
Finally, main program looks like
With the above changes in places, when I run the program, it will produce the following output.
Therefore, the idea behind this demo is to illustrate, how easy it is to extend your application, if you are coding on contracts. And, here interface obviously is our contract. I hope you would have liked this discussion. Thanks for Joining me.
Code Download Link:- https://github.com/rahulsahay19/Extending-Interfaces-Part-2
1,076 total views, 1 views today