I'm developing services with a micro service architecture using .NET Core 6. I'm trying to follow clean architecture advice and have created 3 layers, Core
(I put my use cases, repository interfaces, and my dto models), API
layer, and infrastructure
(connection with DB and implementation of repository interfaces).
The problem is the number of repositories is increasing, because I'm creating a separate repository for each different job, for example IStoreCarPricesRepository
, IStoreCarSparePartRepository
,..... I was thinking, would it not be wiser to have a generic repository and and just create a class to call it and do the jobless say you are getting different messages from different queues and was to store them in the respective tables?
As far as I searched for generic repository is kind of outdated, I would appreciate your comments
CodePudding user response:
Yes, you can use Generic repository:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Data.Entity;
using ContosoUniversity.Models;
using System.Linq.Expressions;
namespace DAL
{
public class GenericRepository<TEntity> where TEntity : class
{
internal SchoolContext context;
internal DbSet<TEntity> dbSet;
public GenericRepository(SchoolContext context)
{
this.context = context;
this.dbSet = context.Set<TEntity>();
}
public virtual IEnumerable<TEntity> Get(
Expression<Func<TEntity, bool>> filter = null,
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
string includeProperties = "")
{
IQueryable<TEntity> query = dbSet;
if (filter != null)
{
query = query.Where(filter);
}
foreach (var includeProperty in includeProperties.Split
(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{
query = query.Include(includeProperty);
}
if (orderBy != null)
{
return orderBy(query).ToList();
}
else
{
return query.ToList();
}
}
public virtual TEntity GetByID(object id)
{
return dbSet.Find(id);
}
public virtual void Insert(TEntity entity)
{
dbSet.Add(entity);
}
public virtual void Delete(object id)
{
TEntity entityToDelete = dbSet.Find(id);
Delete(entityToDelete);
}
public virtual void Delete(TEntity entityToDelete)
{
if (context.Entry(entityToDelete).State == EntityState.Detached)
{
dbSet.Attach(entityToDelete);
}
dbSet.Remove(entityToDelete);
}
public virtual void Update(TEntity entityToUpdate)
{
dbSet.Attach(entityToUpdate);
context.Entry(entityToUpdate).State = EntityState.Modified;
}
}
}
Then you can instantiate and use it for each table:
private GenericRepository<Department> departmentRepository;
private GenericRepository<Employee> employeeRepository;
Read more about generic repositories here.
CodePudding user response:
Repo doesn't works with jobs and messages.
The main responsibility of repository is a wrapper for common operations (CRUD) or maybe some work with tables for example count, any, some, all.
If u wish to create a microservice architecture, I think that great stuff will be check generic repositories in GitHub and create your own library and move generic-repository here. It should be something like "YourName.Microservice.Core".
After that connect it through nuget-extension to your Microservice.Core
In the future u need to extend them to avoid a duplication of code in a lot of microservices.