C # Entity DbSet with DDD?

0

I have an application C# MVC with DDD and in repository I'm making the call like this:

 return DbSet.Include(i => i.Cliente).FirstOrDefault(a => a.ProcessoId == processoId);

DbSet-> is in repositoy of Process

public class Processo
{
    public int Id { get; set; }
    public string Obs { get; set; }  <- varchar(max)
    public int? ClienteId { get; set; }
    public virtual Cliente Cliente { get; set; }
}

Questions:

Using DbSet can you ignore the Obs field in the query return?

(ie instead of SELECT * seja feito SELECT Id, ClienteId, ... Join... )

Does anyone know of any other way to do this and keep the include?

Some component, similar project, suggestions welcome.

    
asked by anonymous 08.12.2016 / 20:44

1 answer

0

I was left with doubts in some fields, but, can be done Include , after a Select and finally the filter inside FirstOrDefault :

var result = DbSet
      .Include(i = i.Cliente)
      .Select(s => new {
          s.Id, 
          s.ClienteId,
          s.Cliente
      })
      .FirstOrDefault(i => i.Id == processoId); 

In this specific case the variable result is an anonymous type ( desconhecido ). If you want to return a specific type ( Strongly Typed ), create a classe that represents this result, eg

public class ProcessoViewModel
{
    public int Id {get;set;}
    public int? ClienteId {get;set;}
    public Cliente Cliente {get;set;
}

Make the change in the code:

ProcessoViewModel result = DbSet
      .Include(i = i.Cliente)
      .Select(s => new ProcessoViewModel
      {
          Id = s.Id, 
          ClienteId = s.ClienteId,
          Cliente = s.Cliente
      })
      .FirstOrDefault(i => i.Id == processoId);

Some answer that can help you:

08.12.2016 / 21:00