Search code examples
c#asp.net-corerepository-patternmediatr

Some services are not able to be constructed in ASP.NET Core


having hard time with CQRS because of this exception. I have a Movie model and also MovieDTO model

//EDIT Okay I've just realized that in GetMoviesQuery I don't use IRepository< MovieDTO > and when I change MovieRepository to IRepository<MovieDTO> then it works

IRepository.cs

public interface IRepository<TEntity>
{
    IEnumerable<TEntity> GetAll();

    TEntity Get(int id);

    TEntity Save(TEntity entity);

    void Delete(int entityId);
}

MovieRepository.cs

public class MovieRepository : IRepository<MovieDTO>
{
    private MyContext _context;
    private IMapper _mapper;
    public MovieRepository(MyContext context)
    {
        _context = context;
        var config = new MapperConfiguration(cfg => {
            cfg.CreateMap<Movie, MovieDTO>();
        });
        _mapper = config.CreateMapper();
    }

    public IEnumerable<MovieDTO> GetAll()
    {
        return _context.Movies.ToList().Select(movie => _mapper.Map<Movie, MovieDTO>(movie));
    }
}

GetMoviesQuery.cs

public class GetMoviesQuery
{
    public class Query : IRequest<IEnumerable<MovieDTO>> { }

    public class Handler : RequestHandler<Query, IEnumerable<MovieDTO>>
    {
        private MovieRepository _repository;

        public Handler(MovieRepository repository)
        {
            _repository = repository ?? throw new ArgumentNullException(nameof(_repository)); 
        }

        protected override IEnumerable<MovieDTO> Handle(Query request)
        {
            return _repository.GetAll();
        }
    }
}

Startup.cs

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllersWithViews();
        services.AddRazorPages();
        services.AddTransient<IRepository<MovieDTO>, MovieRepository>();
        services.AddHttpClient();
        services.AddDbContext<MyContext>(options => options.UseSqlite("Data Source = blogging.db"));
        services.AddMediatR(typeof(Startup));
    }

Exception: System.AggregateException: 'Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: MediatR.IRequestHandler2[TicketReservationSystem.Server.CQRS.Queries.GetMoviesQuery+Query,System.Collections.Generic.IEnumerable1[TicketReservationSystem.Server.Models.DTO.MovieDTO]] Lifetime: Transient ImplementationType: TicketReservationSystem.Server.CQRS.Queries.GetMoviesQuery+Handler': Unable to resolve service for type 'TicketReservationSystem.Server.Data.Repository.MovieRepository' while attempting to activate

I have no idea how to actually find out where's the problem.


Solution

  • public Handler(MovieRepository repository)

    should be changed to

    public Handler(IRepository<MovieDTO> repository), since you registered your container with interface, not implementation.

    services.AddTransient<IRepository<MovieDTO>, MovieRepository>();
    

    If you want to use your original code, register class itself instead

    services.AddTransient<MovieRepository>();