.Net Core Repository Pattern

.Net Core Repository Pattern

Mediates between the domain and data mapping layers using a collection-like interface for accessing domain objects.
Repositor pattern is useful with big teams where you need to break your work into modules.

Benefits

  1. Minimizes duplicate query and database logic.
  2. Decouples your implementation from persistence frameworks or solutions.
  3. Provides a flexible architecture.
  4. Code centralization.
  5. Avoids code redundancy.

Let jump into coding so you may understand how really we going to implement repository in .net core.

Create IRepository interface under the Data folder with the following code.

This interface is planned to be an abstraction layer over different ORMs.

Now, we will create an abstract class for EF Core which implements this interface. 


using DBHandler.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace RepositoriesExample
{
    public interface IRepository<TEntitywhere TEntity : class
    {
        TEntity Get(decimal id);
        TEntity Get(string id);
        TEntity Get(int id);
        IEnumerable<TEntityGetAll();
        IQueryable<TEntityFind(Expression<Func<TEntitybool>> predicate);

        TEntity SingleOrDefault(Expression<Func<TEntitybool>> predicate);
        void UpdateRange(IEnumerable<TEntityentities);
        void Add(TEntity entity);
        void AddRange(IEnumerable<TEntityentities);
        void UpdateEntity(TEntity entitydynamic obj);
        void Remove(TEntity entity);
        void RemoveRange(IEnumerable<TEntityentities);

        // void RemoveRangeByid(Id id);

        void RemovebyId(decimal id);
        void RemovebyId(int id);
        void RemovebyId(string id);


        void Update(TEntity entity);

        bool Save();
    }
}



Next, add Models folder to the project and add ErrorMessages class to this folder:

[Table("t_ErrorMessages")]

public class ErrorMessages
{
    public string ErrorCode { getset; }
    public string ErrorMessage { getset; }
    public string LocalCode { getset; }
    public bool isError { getset; }
    public string data { getset; }
}


In the root of the Entities project, we are going to create the RepositoryContext class and modify it:

using Entities.Models;
using Microsoft.EntityFrameworkCore;
 
namespace RepositoriesExample
{
    public class RepositoryContextDbContext
    {
        public RepositoryContext(DbContextOptions options)
            :base(options)
        {
        }
 
        public DbSet<ErrorMessagesOwners { getset; }
    }
}

Now define an interface for the entity.

using Code.RepositoryPattern.Model;
using System.Collections.Generic;
using Code.RepositoryPattern.Repository.Base;

namespace RepositoriesExample
{
    public interface IErrorMessagesRepository : IRepository<ErrorMessages>
    {
    }
}

Now define class where you can  have methods which you may define in the above interface 

namespace RepositoriesExample
{
    public class ErrorMessagesRepository:Repository<ErrorMessages>,IErrorMessagesRepository
    {
    }
}


Now go to Startup of the webapi solution and add the following.


namespace RepositoriesExample
{
    public class Startup
    {
       
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }
        
        public IConfiguration Configuration { get; }
        public void ConfigureServices(IServiceCollection services)
        {
                        services.AddScoped<IErrorMessagesRepositoryErrorMessagesRepository>();
        }
    }
}

this is a basic example of repository you can utilize it according to your need.





Comments

Popular posts from this blog

Setup source code of Visual studio on Bitbucket

.Net Core WebClient and proxy setup

Tables with ledger in sql server 2022