Using async with .NET Core and nHibernate

3

In the .NET Core I know that we have async and await to request async .

So far so good, but many frameworks on the market are creating methods with the MetodoAsync() signature and have the method without being async Metodo() .

We can use async so

[HttpGet]
public async Task<JsonResult> Get()
{
    return await Task.Run(() => Json(_fachadaGrupo.BuscarTodos()));
}

Remembering that in internal methods _fachadaGrupo.BuscarTodos() . the async method does not exist, it uses a connection to the nHibernate database, and it does not have the Async() method at the moment. So I wanted to know if this still has the same efficiency and the same logic happens internally in which thread is freed up for new requests . Or it is the same as not having.

 // facade
    public IEnumerable<GrupoDto> BuscarTodos()
    {
        return _buscarTodos.Buscar();
    }


    // Serviço
    public IEnumerable<GrupoDto> Buscar()
            {
                IRepositorioGrupo repositorioGrupo = new RepositorioGrupo(_nHibernateHelper);

                return repositorioGrupo.Lista(_usuario);
            }

// repositorio 
public IEnumerable<GrupoDto> Lista(Usuario usuario)
        {
            Usuario usuarioAlias = null;
            Grupo grupo = null;
            GrupoDto grupoDto = null;

            return Sessao.QueryOver(() => grupo).JoinAlias(() => grupo.Usuario, () => usuarioAlias)
                .SelectList(list => list.Select(() => grupo.Id).WithAlias(() => grupoDto.Id)
                .Select(() => grupo.Descricao).WithAlias(() => grupoDto.Descricao))
                .TransformUsing(Transformers.AliasToBean<GrupoDto>())
                .Where(g => g.Usuario == usuario).OrderBy(g => g.Id).Desc.List<GrupoDto>();
        }
    
asked by anonymous 09.10.2017 / 13:33

1 answer

3

The BuscarTodos() method obviously, unless modified, will never run asynchronously. Your Get() will do so when called with a await . Since it is very simple and only calls another synchronous method, there will be no gain. The gain would be given only in this method, between your own code that is almost indivisible.

The asynchronicity you want has to be done within BuscarTodos() , more precisely in this connection to the database. The real utility of it will probably be to have an asynchronous method that makes access to the external resource within it and this seems to me that nHibernate should provide, but then I start getting into speculation, I did not see the code.

With the release note that it is within Lista() that the "magic" should happen, it is there in LINQ that it should be asynchronous for gains.

I was going to write the same as Tobias Mesquita, but I avoided it because EF Core is not 100% ready yet, even in 2.0, but I would trade ORM, but I still consider it better. I like the courage of those who recognize that an architecture is wrong and starts from scratch as Microsoft did. It would be great if nHibernate did the same. He has no isolated problems. These products were created when you did not know everything you needed about ORM, the more modern ones are much more advantageous because they have learned lessons.

    
09.10.2017 / 13:50