AutoMapper with Expression

2

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();
    }
    
asked by anonymous 10.04.2015 / 16:50

2 answers

1

In case of making AutoMapper with expression, you have to use .Where() and not .Find() .

The .Find() returns a single element and you are trying to map to a collection.

It would look like this:

public IEnumerable<PaisViewModel> Find(Expression<Func<PaisViewModel, bool>> predicate)
{
   return Mapper.Map<IEnumerable<Pais>, IEnumerable<PaisViewModel>>(_paisService.Where(predicate));
}

Or

public PaisViewModel Find(Expression<Func<PaisViewModel, bool>> predicate)
{
   return Mapper.Map<Pais, PaisViewModel>(_paisService.Find(predicate));
}

Edit

Apparently the reason for the error is that you are using:

Expression<Func<PaisViewModel,bool>>

When you should actually use:

Expression<Func<Pais,bool>>
    
10.04.2015 / 16:53
1

You can make the predicate mapper:

var newPredicate = Mapper.Map<Expression<Func<Class, bool>>>

Example:

public IEnumerable<PaisViewModel> Search(Expression<Func<PaisViewModel, bool>> predicate)
{
var newPredicate = Mapper.Map<Expression<Func<Pais, bool>>>(predicate);
return Mapper.Map<IEnumerable<PaisViewModel>>(_paisService.Search(newPredicate));
}
    
29.11.2016 / 17:00