2
I am making a generic repository and 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’m having difficulties because it uses Express. Being that 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
http://eduardopires.net.br/2014/10/tutorial-asp-net-mvc-5-ddd-ef-automapper-ioc-dicas-e-truques/
I am beginner in C# I came across this situation where I have to implement these generic CRUD.
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();
}
I moved your edit to your question, this is where the edit should be. I will analyze the code.
– Filipe Oliveira
Look, in this tutorial project there is no
Find
. Did you implement it on the outside? Could you add the implementation ofFind
of the Repositorybase?– Filipe Oliveira
The people who passed me this project already passed this way, my function would just implement. I will analyze.
– Gilmar
Shows how Find() in
RepositoryBase
that I help you.– Filipe Oliveira
public virtual Ienumerable<Tentity> Find(Expression<Func<Tentity, bool>> predicate) { Return Dbset.Where(predicate); }
– Gilmar
See the edition of my reply, this was because you are using the
Expression<Func<PaisViewModel,bool>>
and the Find doPaisService
is expecting aExpression<Func<Pais,bool>>
– Filipe Oliveira
Thanks for the help that’s right. It worked
– Gilmar