Add extension method for IRepository<TEntity> or (if async) IRepostioryAsync<TEntity>
Best practice, expose this through your service layer
Example in Web Api
publicstaticclass CustomerRepository { publicstaticdecimal GetCustomerOrderTotalByYear( this IRepository<Customer> customerRepository, int customerId, int year) { return customerRepository .Find(customerId) .Orders.SelectMany(o => o.OrderDetails) .Select(o => o.Quantity*o.UnitPrice).Sum(); } }
Best practice, expose this through your service layer
publicinterface ICustomerService : IService<Customer> { decimal CustomerOrderTotalByYear(int customerId, int year); } publicclass CustomerService : Service<Customer>, ICustomerService { privatereadonly IRepositoryAsync<Customer> _repository; public CustomerService(IRepositoryAsync<Customer> repository) : base(repository) { _repository = repository; } publicdecimal CustomerOrderTotalByYear(int customerId, int year) { return _repository.GetCustomerOrderTotalByYear(customerId, year); } }
Example in Web Api
publicclass CustomerController : ODataController { privatereadonly ICustomerService _customerService; public CustomerController( ICustomerService customerService) { _customerService = customerService; } publicdecimal CustomerOrderTotalByYear(int customerId, int year) { return _customerService.CustomerOrderTotalByYear(customerId, year); } }