Use of async with . NET Core and nhibernate

Asked

Viewed 274 times

3

No. NET Core I know we have the async and await for request async.

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

We can use async thus

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

Remembering that in internal methods _fachadaGrupo.BuscarTodos(). there is no method async it uses a connection to nhibernate database and it does not have the method Async() at the moment. Soon then wanted to know if this still has the same efficiency and the same logic happens internally in which the thread and released for new requests. Or is it 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>();
        }
  • if you want to continue using ASP.NET Core, I advise you to leave Nhibernate, it has a dependency with the Iesi.Collections which has not been updated for some years, the ISession are not thread safe, which makes it impossible to implement the async/wait with the same, and finally, the same was not done in the .net standard, you can even use the same in compatibilidade with the 4.6.1, but you will have no guarantees... so I advise you to use the EF Core 2.0, which is much more efficient and has a much more mature API.

  • A compatibility with . net standard , and in the version about to be released we will already have support for async/Wait (version 5.0 of it) https://github.com/nhibernate/nhibernate-core/pull/693 (version 5.0) and in version 5.1 we probably already have support with . net standard https://github.com/nhibernate/nhibernate-core/pull/633

  • I’m sorry, but this NH 5.0 has been in development for quite some time now, and has always been delayed. Unless your project only goes into production in 2022, it is not a good idea to wait for a stable version of it.

1 answer

3


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

The asynchronicity you want has to be performed within the BuscarTodos(), more precisely in this connection with the database. His real utility will probably be to have an asynchronous method that makes access to the external resource within him and it seems to me that Nhibernate should provide, but then I start to get into the speculation, I have not seen the code.

With the edition you can see that it is inside the Lista()that "magic" should happen, it is there in LINQ that should be asynchronous to obtain gains.

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

  • I just added more code for better detail, I could explain how this works?

  • I understood, thank you, in my specific case I use for a long time nhibernate. It was using Entity core in version 1.2 or 1.1 if I am not mistaken. I remember bumping into a relationship limitation 1.. 1 I didn’t like it one bit. I remember that the same limitation was on the roadmap for 2.0 so everything should be OK. But as I saw the pull on nhibernate and I compiled it myself at the time, so yes I use it for . net standard (Ahh Roberto this far from being 100% has almost 6thousand tests, I assumed that in the bank that use passed in all ta ok). Still no pull . net standard lacks the async. I will relegate that there.

  • Thank you so much for your attention, I’ve been reading about the async await I found this here, I’m bad in English yet. But I am trying to understand this article by reading it more than once. https://msdn.microsoft.com/en-us/magazine/dn802603.aspx

Browser other questions tagged

You are not signed in. Login or sign up in order to post.