Nhibernate is modifying property when returning by Web Api

Asked

Viewed 98 times

1

I have a web service api where a query to the bank is relaunched through nhibernate, but when returning the object, its referenced properties are overwritten, I believe it is because of the nhibernate proxy.

inserir a descrição da imagem aqui

How can I be doing to fix this problem?

Follows the classes Personal Clinic and then Person:

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

  • Could explain better and put the code from where the problem occurs. I can’t help if I can’t reproduce the problem.

  • Good afternoon @Miguelangelo the example is very simple to reproduce, I have a Personal class and inside it has a property of type Person, where it contains all the properties related to the person. I’m using Nhibernate to connect to the database, when I return the Personal object through a web api controller, the client arrives as per the image above, the correct one was Pessoa contain only the properties that are within _target.

  • I just posted some images of the Personal Clinical class and the Person class, see if it’s clearer now. I am making a consultation of Personal Clinic and in the return in the client is as the first image.

  • How you are doing class mapping: you are using Fluentnhibernate, the Fluent syntax of Nhibernate itself or XML file?

  • I am using fluentNHibernate, with structureMap.

1 answer

1


How well you suspected the culprit is the Proxy of Nhibernate. I don’t think it’s a good idea to serialize the proxy object exactly because you have little control over it.

Some options:

  • inform Nhibernate not to use proxy in property mapping Pessoa:

    this.References(x => x.Pessoa)
        .Not.LazyLoad();
    
  • use a cloned object from the original, for this you can use the library Automapper

    // inicialização do AutoMapper
    Mapper.Initialize(cfg =>
    {
        cfg.CreateMap<PessoaClinica, PessoaClinica>();
        cfg.CreateMap<Pessoa, Pessoa>();
    });
    Mapper.AssertConfigurationIsValid();
    
    // usando no controller
    Mapper mapper = new Mapper();
    PessoaClinica obj = ...; // obter o objeto do banco de dados
    return mapper.Map<PessoaClinica, PessoaClinica>(obj);
    

    Note: you will probably want to inject variable dependency mapper

  • use a specialized object (DTO) and copy the properties one by one, and you would return the DTO instead of the proxy from within the Webapi method. You can also use Automapper in this option.

  • Customizing the serialization process... that would be using a cannon to kill fly

  • It worked, but I had to use Dto, when I used the Automapper with the same class, it worked anyway, but using Dto it worked.

  • Actually, you would have to implement a custom converter, because you don’t just clone the object itself, you have to clone recursively. I will edit only to answer not be incomplete.

  • I made a mistake... you actually have to set up to clone the class Pessoa also, ... what I forgot to do.

Browser other questions tagged

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