So I've created a base repository class which returns domain models and uses entity objects to manipulate data in the database. Here is the repository class:
public abstract class Repository<TDomain, TEntity, TMapper> : IReadWriteRepository<TDomain>
where TDomain : Model
where TEntity : Entity
where TMapper : IMapper<TDomain, TEntity>
{
private readonly ApplicationDbContext _context;
public Repository(ApplicationDbContext context)
{
_context = context;
}
public IQueryable<TDomain> Get()
{
var entities = _context.Set<TEntity>();
// Return domain models
}
public void Add(TDomain item)
{
// Convert to entity model
_context.Set<TEntity>().Add(item);
}
public void Update(TDomain item)
{
// Convert domain to entity model
var entry = _context.Entry<TEntity>(entity);
if (entry.State == EntityState.Detached)
{
_context.Set<TEntity>().Attach(entity);
entry.State = EntityState.Modified;
}
}
public void Delete(int id)
{
var entity = _context.Set<TEntity>().Find(id);
_context.Set<TEntity>().Remove(entity);
}
}
As you can see at this point I'm struggling to convert the entity objects to domain and vice versa.
I have defined an IMapper interface which when implemented will convert entity into domain and vice versa.
public interface IMapper<TDomain, TEntity> where TDomain : Model where TEntity : Entity
{
public TDomain ToDomain(TEntity entity);
public TEntity ToEntity(TDomain domain);
}
Problem:
I can't seem to use any of these methods in the Repository base class through TMapper, I basically want to be able to just have 1 simple Repository implementation which performs all CRUD operations but using multiple Mappers to assist with mapping objects.