repository pattern in csharp

The Repository Pattern in C# is a design pattern that separates the application logic and data access logic, allowing you to write more modular and easier-to-maintain code. The following are the steps to implement the Repository Pattern in C#:

  1. Define your repository interfaces: Define an interface for each entity that your application needs to interact with. For example, if you're building a blog application, you might have an interface called IBlogPostRepository that defines methods for creating, reading, updating, and deleting blog post entities.
main.cs
public interface IBlogPostRepository
{
    void Create(BlogPost post);
    BlogPost GetById(int id);
    void Update(BlogPost post);
    void Delete(int id);
}
160 chars
8 lines
  1. Implement your repository interfaces: Implement each interface in a concrete class that interacts with your database or data store. For example, you might have a class called SqlBlogPostRepository that uses Entity Framework to interact with a SQL Server database.
main.cs
public class SqlBlogPostRepository : IBlogPostRepository
{
    private readonly MyDbContext _dbContext;

    public SqlBlogPostRepository(MyDbContext dbContext)
    {
        _dbContext = dbContext;
    }

    public void Create(BlogPost post)
    {
        _dbContext.BlogPosts.Add(post);
        _dbContext.SaveChanges();
    }

    public BlogPost GetById(int id)
    {
        return _dbContext.BlogPosts.SingleOrDefault(p => p.Id == id);
    }

    public void Update(BlogPost post)
    {
        _dbContext.Entry(post).State = EntityState.Modified;
        _dbContext.SaveChanges();
    }

    public void Delete(int id)
    {
        var postToDelete = _dbContext.BlogPosts.SingleOrDefault(p => p.Id == id);

        if (postToDelete != null)
        {
            _dbContext.BlogPosts.Remove(postToDelete);
            _dbContext.SaveChanges();
        }
    }
}
871 chars
38 lines
  1. Use dependency injection to provide instances of your repository classes: In your application's composition root, configure a dependency injection container to provide instances of your repository classes to other parts of your application. For example, you might use Microsoft's built-in dependency injection container in ASP.NET Core to register your repository classes.
main.cs
services.AddScoped<IBlogPostRepository, SqlBlogPostRepository>();
66 chars
2 lines
  1. Use your repository classes to interact with your data store: In the classes that need to interact with your data store, use constructor injection to get an instance of the appropriate repository interface. For example, in a controller action that displays a blog post, you might use the IBlogPostRepository interface to retrieve the post from the database.
main.cs
public class BlogController : Controller
{
    private readonly IBlogPostRepository _blogPostRepository;

    public BlogController(IBlogPostRepository blogPostRepository)
    {
        _blogPostRepository = blogPostRepository;
    }

    public IActionResult Show(int id)
    {
        var post = _blogPostRepository.GetById(id);

        return View(post);
    }
}
367 chars
17 lines

gistlibby LogSnag