How to hide property in Efcore listing

Asked

Viewed 484 times

2

Hello,
I have a question regarding the Efcore Webapi of ASP.NET Core.

I have my Service, Professional, User and Scheduling entities, where in the service I have a foreign key for the professional and in scheduling for professional and user.

When I make a request for the endpoint /services, I receive the following JSON:

[
  {
    "descricao": "Servico Primário",
    "valor": 109.99,
    "profissional": {
      "servicos": [],
      "nome": "Profissional",
      "cpf": "00000000001",
      "endereco": "Rua A",
      "idade": 22,
      "email": "[email protected]",
      "senha": "123456789",
      "id": 1
    },
    "agendamentos": [
      {
        "data": "2019-08-22T19:30:40.573",
        "finalizado": false,
        "usuario": null,
        "id": 1
      }
    ],
    "id": 1
  }
]

The above structure is generated by following this code:

using System.Collections.Generic;
using System.Linq;
using Fusion.Domain.Entities;
using Microsoft.EntityFrameworkCore;

namespace Fusion.Infra.Data.Repositories
{
    public class ServicoRepository : BaseRepository<Servico>
    {
        public override IList<Servico> SelectAll()
        {
            return context.Servicos
                .Include("Profissional")
                .Include("Agendamentos")
                .ToList();
        }

        public override Servico Select(int id)
        {
            return SelectAll().FirstOrDefault(x => x.ID == id);
        }
    }
}

My question is as follows, how do I hide in this request in specific fields professional.password, professional.servicos and schedules.usuario?

  • unfortunately using Rest the only way would be to do another dto without the attribute in question and map it, to be able to bring results more dynamically, I believe you need to try something like graphql

  • That’s exactly what I did and what worked for Lucas. Thank you so much for your help!

2 answers

5

You can ignore a property to be serialized by decorating it with the JsonIgnore

public class Profissional
{        
    public string Nome { get; set; }
    public string Cpf { get; set; }
    public string Endereco { get; set; }
    public int Idade { get; set; }
    public string Email { get; set; }
    [JsonIgnore]
    public string Senha { get; set; }
    public int Id { get; set; }
}

0

My solution, for those who wish to apply, was the use of a Viewdto class with the properties I would like to represent on the screen.

Class for Mapping using AutoMapper:

using AutoMapper;
using Fusion.Domain.Entities;
using Fusion.Infra.Data.DTO;

namespace Fusion.Infra.Data.AutoMapper
{
    public static class FusionMapper
    {
        public static MapperConfiguration mapper = new MapperConfiguration(conf => {
            conf.CreateMap<Profissional, ProfissionalViewDTO>();
        });
    }
}

Classe Professionalviewdto:

using Fusion.Domain.Entities;

namespace Fusion.Infra.Data.DTO
{
    public class ProfissionalViewDTO
    {
        public int ID { get; set; }
        public string Nome { get; set; }
        public string CPF { get; set; }
        public string Endereco { get; set; }
        public int Idade { get; set; }
        public string Email { get; set; }
    }
}

And in my context access layer repository:

namespace Fusion.Infra.Data.Repositories
{
    public class ProfissionalRepository : BaseRepository<Profissional>
    {
        IMapper mapper = FusionMapper.mapper.CreateMapper();

        public new IList<ProfissionalViewDTO> SelectAll()
        {
            return context.Profissionais
                .AsEnumerable()
                .Select(x => mapper.Map<Profissional, ProfissionalViewDTO>(x))
                .ToList();
        }
    }
}

And finally, my Professional mastery class:

using System.Collections.Generic;

namespace Fusion.Domain.Entities
{
    public class Profissional : Pessoa
    {        
        public ICollection<Servico> Servicos { get; set; }
    }
}

Where is inheriting a class Person:

namespace Fusion.Domain.Entities
{
    public abstract class Pessoa : BaseEntity
    {
        public virtual string Nome { get; set; }
        public virtual string CPF { get; set; }
        public virtual string Endereco { get; set; }
        public virtual int Idade { get; set; }
        public virtual string Email { get; set; }
        public virtual string Senha { get; set; }
    }
}

Note: I applied for tests in another class of my application, but it can be replicated to others using the same concept of Viewdto.

Browser other questions tagged

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