Working with a Database in Enterprise
When talking about databases, you usually imagine SQL or another language to talk with them. On top of that, another language (C#, in this case) is most often used to connect to a database to execute SQL queries. If not controlled, C# gets mixed with SQL, and it causes a mess of your code. Over the years, there have been a few patterns refined to implement the communication with a database in a clean way. Two such patterns, namely, Repository and CQRS, are commonly used to this day.
Repository Pattern
The Repository is a pattern that targets a model and defines all (if needed) possible CRUD operations. For example, if you take a Product
model, you could have a repository with this interface:
public interface IProductRepository { Â Â Â Â int Create(Product product); Â Â Â Â void Delete(int id); Â Â Â Â void Update(Product product); Â Â Â Â Product Get(int id); Â Â ...