The Repository and Unit Of Work Pattern are two very useful Design Pattern used when working with data. The Repository pattern is used with logical collections of objects and it acts as an in-memory collection for business entities, abstracting away the underlying data infrastructure. That is, when you use the repository pattern, you don’t have to design your Data Access logic/layer based on the the specifics of what kind of data store or what kind of technologies will be used to query it.
The Unit Of Work pattern, on the other hand, is designed to maintain a list of business objects that have been changed by a business transaction, be it adding, updating or deleting, then it coordinates the changes as one atomic action. If there are problems the entire transaction rolls back.
There are lots of blog posts and articles, that shows examples on how to implement this in C# & Entity Framework, and you will find out that different people with different ways in which they’ve used this pattern, and in this post I won’t be doing any code demonstration instead, I’m sharing a link to my code implementation on GitHub and links to other sources where you can read more about it.
I use the code in GitHub to kickstart my repository implementation by creating Repositories related to the specific need of my application which inherits from the BaseRepository class (which is a Generic class that implements my basic CRUD operations) and then implement specific operation in those repository classes e.g GetFollowersByID(id), GetCommentByTopicId(topicId), just anything related to that repository and as time goes by, if I find a better way to perform this kind of task, my code on GitHub will change.