Base solution for your next web application
Open Closed

Use AppService In Controller #846


User avatar
0
papinaser created

Hi. How I should use AppService Class defined in Application Layer in MVC Web Project? Exactly my problem is constrictor of AppService that take a IRepository as parameter.

[AbpAuthorize]    
    public class MenuGroupAppService: WebUIAppServiceBase, IMenuGroupAppService
    {
        private readonly IRepository<MenuGroup> _menuGroupRepository;   
                 
        public MenuGroupAppService(IRepository<MenuGroup> menuGroupRepository)
        {
            _menuGroupRepository = menuGroupRepository;
        }        

        public IQueryable<MenuGroup> GetAllCreateMenuGroupInputs()
        {
            return _menuGroupRepository.GetAll();
        }

        public async Task<ListResultOutput<MenuGroupListDto>> GetList(GetMenuGroupListInput input)
        {
            var menuGroups = await _menuGroupRepository
                .GetAll()
                .OrderByDescending(e => e.MenuOrderNO).ToListAsync();

            return new ListResultOutput<MenuGroupListDto>(menuGroups.MapTo<List<MenuGroupListDto>>());
        }
    }

and in controller how I should use MenuGroupAppService class for get list of menu groups ?

[HttpGet]
        public List<GetMenuGroupListInput> GetAllMenuGroups()
        {
           
            return new MenuGroupAppService(WHAT I SHOULD PASS HERE?).
            GetList(new GetMenuGroupListInput {JustEspMenus = true});

        }

Thakyou.


3 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team

    Don't create instances of services yourself. Use dependency injection always. So, you can inject app service into your controller. Please see dependency injection document, otherwise you will have more questions without understand DI well: <a class="postlink" href="http://www.aspnetboilerplate.com/Pages/Documents/Dependency-Injection">http://www.aspnetboilerplate.com/Pages/ ... -Injection</a>

  • User Avatar
    0
    papinaser created

    Hi. Thank you . my problem solved by using DI and your default awesome classes for DI framework. But this is another problem , running this block of code :

    var menuGroups = await _menuGroupRepository
                    .GetAll()
                    .OrderByDescending(e => e.MenuOrderNO).ToListAsync();
    

    is very slow and sometimes project fails. Can you help me about this.

  • User Avatar
    0
    hikalkan created
    Support Team

    It queries to database and seems not related to the framework. You should investigate the problem (you my use Sql Server profiler for example).