Vyechi<p>Have you ever dreaded writing the same boilerplate repository code repeatedly? You’re not alone; I will share my implementation of the Code First Generic Repository Pattern in C#. I will only include some of the code because it will make the post incredibly long. At the end of the post, I will share a Goal donation to post a Git download link so you can take it for a spin. As a bonus, the repository uses <a href="https://learn.microsoft.com/en-us/ef/ef6/modeling/code-first/workflows/new-database" rel="nofollow noopener noreferrer" target="_blank">Code First</a> and Unit of Work.</p><p><strong>Generic Repository Pattern in C#</strong></p><p>Let’s begin with the Entity interface. The <strong>IEntity </strong>interface is a typical pattern used in software development to define a contract for entities, typically in the context of data access and persistence. It usually includes a property for the entity’s unique identifier. The Generic Type: The <strong>IEntity </strong>interface uses a generic type parameter <strong>TKey</strong> to allow flexibility in the type of the identifier (e.g., <em>int, Guid, string</em>). The <strong>ID</strong> property will identify each entity uniquely. Feel free to read more about the implementation at <a href="https://codingblast.com/entity-framework-core-generic-repository" rel="nofollow noopener noreferrer" target="_blank">the entity framework core generic repository</a> and <a href="https://learn.microsoft.com/en-us/windows/win32/api/structuredquery/nn-structuredquery-ientity" rel="nofollow noopener noreferrer" target="_blank">Structured Query IEntity</a>.</p> <pre>public interface IEntity<TKey>{ TKey Id { get; set; }}</pre> <p>You might find the following definition of the generic repository pattern on the interwebs.</p><blockquote><p>The Generic Repository pattern in C# is a design pattern that abstracts the application’s data layer, making it easier to manage data access logic across different data sources. It aims to reduce redundancy by implementing typical data operations in a single, generic repository rather than having separate repositories for each entity type.</p></blockquote> <pre>public interface IGenericRepository<TEntity, TKey> where TEntity : class, IEntity<TKey>{ void Delete(TEntity entityToDelete); TEntity? GetFirstOrDefault(Expression<Func<TEntity, bool>> predicate); void Update(TEntity entityToUpdate); void Save(); void Dispose();}</pre> <p>The interface header has two generic types. <strong>TEntity </strong>is the domain class, and the <strong>TKey </strong>is<strong> </strong>the ID type, int, or string. Note that <strong>IEntity </strong>abstracts away the type <strong>TKey</strong>. It looks complex for the moment, but you will see benefits later.</p><p>Moving away from the Generic Repository, let’s focus on the pattern of the Unit of Work. According to Copilot:</p><blockquote><p>The <strong>Unit of Work</strong> is a design pattern used in software development to manage and coordinate changes to a database. It ensures that all operations within a single business transaction are treated as a single unit, which means they either all succeed, or all fail together. This helps maintain data integrity and consistency.</p><p><a href="https://learn.microsoft.com/en-us/aspnet/mvc/overview/older-versions/getting-started-with-ef-5-using-mvc-4/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application" rel="nofollow noopener noreferrer" target="_blank">Implementing the Repository and Unit of Work Patterns in an ASP.NET MVC Application (9 of 10) | Microsoft Learn</a></p><p><a href="https://code-maze.com/csharp-unit-of-work-pattern/" rel="nofollow noopener noreferrer" target="_blank">https://code-maze.com/csharp-unit-of-work-pattern/</a></p></blockquote> <pre>public interface IUnitOfWork : IDisposable{ IGenericRepository<TEntity, TKey> Repository<TEntity, TKey>() where TEntity : class, IEntity<TKey>; void Save(); Task<int> SaveAsync();}</pre> <p>The Unit of Work will allow us later to inject it as a service for any repository. If you inspect the interface closely, you will notice it has three fields. The most vital of the three is the <strong>Repository</strong>. The method returns a domain of type repository and key. You can specify the type when using it.</p><p>Moving on to the Code First portion, we must tell Entity Framework how to build our database. To do so, we can create a “BloggingContext”.</p> <pre>public class BloggingContext: DbContext{ // Use design time factory public BloggingContext(DbContextOptions<BloggingContext> dbContextOptions) : base(dbContextOptions) { } public DbSet<Blog> Blogs { get; set; } public DbSet<Post> Posts { get; set; }}</pre> <p>If you inspect the code closely, you will notice that we inherited from DbContext, which allows us to manipulate the database. The DbContext will also enable us to use DbSets and CRUD methods. You can read more about <a href="https://learn.microsoft.com/en-us/dotnet/api/system.data.entity.dbcontext?view=entity-framework-6.2.0" rel="nofollow noopener noreferrer" target="_blank">DbContext Class on the Microsoft website</a>. </p><p>Before using your Database Context with Code, you must set up migrations. Migrations allow you to evolve your database as you code and change the schema. I recommend you read up on <a href="https://learn.microsoft.com/en-us/ef/core/managing-schemas/migrations/?tabs=vs#evolving-your-model" rel="nofollow noopener noreferrer" target="_blank">Migrations managing schemas</a> because it has excellent examples to help you start quickly.</p><p>At the high level, in Visual Studio, open up the Tools menu, then the NuGet Package Manager menu, followed by the Console. In the Console, you must install the following package.</p><blockquote><p>Install-Package Microsoft.EntityFrameworkCore.Tools</p></blockquote><p>The Microsoft.EntityFrameworkCore.Tools will allow you to make the following commands.</p><blockquote><p>Add-Migration InitialCreate</p><p>Update-Database</p></blockquote><p>Meanwhile, you might want to consider making a DesignTimeDbContextFactory because it aids in setting up the connection string for the database.</p> <pre>public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<BloggingContext>{ public BloggingContext CreateDbContext(string[] args) { // Build configuration IConfigurationRoot configuration = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.Development.json") .Build(); // Get connection string var connectionString = configuration.GetConnectionString("DefaultConnection"); var optionsBuilder = new DbContextOptionsBuilder<BloggingContext>(); optionsBuilder.UseSqlServer(connectionString); return new BloggingContext(optionsBuilder.Options); }}</pre> <p>I’m considering publishing the fully working example on Git with the following components.</p><ul><li>Services</li><li>Domain</li><li>Interfaces</li><li>Repository</li><li>Console App with basic CRUD operations</li><li>Dependency Injection</li></ul><p><strong>Current Donation Amount</strong></p><p>$2.41 – PayPal fees 🤑</p><p>Note that the donation amount is being updated manually on my end. I have asked WordPress; see the image for details. I will list your name and last name initials. If you like the website backlink, donate $10 or more, and let me know in the comments your web address.</p> <p>Donate to Goal of $666 to unlock Code First Generic Repository Pattern 🚀✨</p> <p>Upon reaching the $666 Goal, I will share the code.</p> <p>$3.00</p> <p><a class="" href="http://vyechi.com/?p=237713" rel="nofollow noopener noreferrer" target="_blank">Click here to purchase.</a></p> <p><strong>Donators List</strong></p><ul><li>Nicholas S 8.20.2024</li></ul><p></p><p><span></span></p><p><a href="https://vyechi.com/2024/08/20/generic-repository-pattern-in-c/" class="" rel="nofollow noopener noreferrer" target="_blank">https://vyechi.com/2024/08/20/generic-repository-pattern-in-c/</a></p><p><a rel="nofollow noopener noreferrer" class="hashtag u-tag u-category" href="https://vyechi.com/tag/codeefficiency/" target="_blank">#CodeEfficiency</a> <a rel="nofollow noopener noreferrer" class="hashtag u-tag u-category" href="https://vyechi.com/tag/codefirst/" target="_blank">#CodeFirst</a> <a rel="nofollow noopener noreferrer" class="hashtag u-tag u-category" href="https://vyechi.com/tag/crudoperations/" target="_blank">#CRUDOperations</a> <a rel="nofollow noopener noreferrer" class="hashtag u-tag u-category" href="https://vyechi.com/tag/csharp/" target="_blank">#CSharp</a> <a rel="nofollow noopener noreferrer" class="hashtag u-tag u-category" href="https://vyechi.com/tag/databasemanagement/" target="_blank">#DatabaseManagement</a> <a rel="nofollow noopener noreferrer" class="hashtag u-tag u-category" href="https://vyechi.com/tag/dbcontext/" target="_blank">#DbContext</a> <a rel="nofollow noopener noreferrer" class="hashtag u-tag u-category" href="https://vyechi.com/tag/dependencyinjection/" target="_blank">#DependencyInjection</a> <a rel="nofollow noopener noreferrer" class="hashtag u-tag u-category" href="https://vyechi.com/tag/designpatterns/" target="_blank">#DesignPatterns</a> <a rel="nofollow noopener noreferrer" class="hashtag u-tag u-category" href="https://vyechi.com/tag/entityframework/" target="_blank">#EntityFramework</a> <a rel="nofollow noopener noreferrer" class="hashtag u-tag u-category" href="https://vyechi.com/tag/genericrepository/" target="_blank">#GenericRepository</a> <a rel="nofollow noopener noreferrer" class="hashtag u-tag u-category" href="https://vyechi.com/tag/migrations/" target="_blank">#Migrations</a> <a rel="nofollow noopener noreferrer" class="hashtag u-tag u-category" href="https://vyechi.com/tag/repositorypattern/" target="_blank">#RepositoryPattern</a> <a rel="nofollow noopener noreferrer" class="hashtag u-tag u-category" href="https://vyechi.com/tag/softwaredevelopment/" target="_blank">#SoftwareDevelopment</a> <a rel="nofollow noopener noreferrer" class="hashtag u-tag u-category" href="https://vyechi.com/tag/unitofwork/" target="_blank">#UnitofWork</a></p>