I'm doing a generic repository and I came across the following situation.
In this call I can do the mapping and it works perfect.
public IEnumerable<PaisViewModel> GetAll()
{
return Mapper.Map<IEnumerable<Pais>, IEnumerable<PaisViewModel>>(_paisService.GetAll());
}
But in this case I am having difficulties because it uses expression. Since my entity is Parents and I use ViewModelPais. How do I automapper with expression?
public IEnumerable<PaisViewModel> Find(Expression<Func<PaisViewModel, bool>> predicate)
{
return Mapper.Map<IEnumerable<Pais>, IEnumerable<PaisViewModel>>(_paisService.Find(predicate));
}
This project follows this architecture
/ a>I'm a beginner in C # and I came across this situation where I have to implement these generic CRUDs.
Domain Layer
namespace Sistema.Domain.Entities
{
public class Pais
{
public int PaisId { get; set; }
public string Codigo { get; set; }
public string Nome { get; set; }
}
}
Service Layer
namespace Sistema.Domain.Interfaces.Services
{
public interface IServiceBase<TEntity> where TEntity : class
{
IEnumerable<TEntity> GetAll();
TEntity GetById(object id);
IEnumerable<TEntity> Find(Expression<Func<TEntity, bool>> predicate);
void Add(TEntity entity);
void Update(TEntity entity);
void Remove(TEntity entity);
}
}
namespace Sistema.Domain.Services
{
public class ServiceBase<TEntity> : IDisposable, IServiceBase<TEntity> where TEntity : class
{
private readonly IRepositoryBase<TEntity> _repository;
public ServiceBase(IRepositoryBase<TEntity> repository)
{
_repository = repository;
}
public virtual IEnumerable<TEntity> GetAll()
{
return _repository.GetAll();
}
public TEntity GetById(object id)
{
return _repository.GetById(id);
}
public virtual IEnumerable<TEntity> Find(Expression<Func<TEntity, bool>> predicate)
{
return _repository.Find(predicate);
}
public virtual void Add(TEntity entity)
{
_repository.Add(entity);
}
public virtual void Update(TEntity entity)
{
_repository.Update(entity);
}
public virtual void Remove(TEntity entity)
{
_repository.Remove(entity);
}
public virtual void Dispose()
{
_repository.Dispose();
}
}
}
Application Layer
namespace Sistema.Application.ViewModels
{
public class PaisViewModel
{
public int PaisId { get; set; }
public string Codigo { get; set; }
public string Nome { get; set; }
}
}
namespace Sistema.Application.Interfaces
{
public interface IPaisAppService : IDisposable
{
IEnumerable<PaisViewModel> GetAll();
PaisViewModel GetById(object id);
IEnumerable<PaisViewModel> Find(Expression<Func<PaisViewModel, bool>> predicate);
void Add(PaisViewModel paisViewModel);
void Update(PaisViewModel paisViewModel);
void Remove(PaisViewModel paisViewModel);
}
}
namespace Sistema.Application
{
public class PaisAppService : AppServiceBase<SistemaContext>, IPaisAppService
{
private readonly IPaisService _paisService;
public PaisAppService(IPaisService cidadeService)
{
_paisService = cidadeService;
}
public IEnumerable<PaisViewModel> GetAll()
{
return Mapper.Map<IEnumerable<Pais>, IEnumerable<PaisViewModel>>(_paisService.GetAll());
}
public PaisViewModel GetById(object id)
{
return Mapper.Map<Pais, PaisViewModel>(_paisService.GetById(id));
}
public IEnumerable<PaisViewModel> Find(Expression<Func<PaisViewModel, bool>> predicate)
{
// Não estou conseguindo implementar
throw new NotImplementedException();
}
public void Add(PaisViewModel paisViewModel)
{
throw new NotImplementedException();
}
public void Update(PaisViewModel paisViewModel)
{
throw new NotImplementedException();
}
public void Remove(PaisViewModel paisViewModel)
{
throw new NotImplementedException();
}
public void Dispose()
{
throw new NotImplementedException();
}
}
}
public IEnumerable<PaisViewModel> Find(Expression<Func<PaisViewModel, bool>> predicate)
{
// Não estou conseguindo implementar
throw new NotImplementedException();
}