0

I create application with Web API and NHibernate ORM. I have a problem when calling get methods. There are many relationships in NHibernate Fluent mapping. For example:

public class Case : GuidEntityBase
{
    public virtual CaseType CaseType { get; set; }
    public virtual string CaseNumber { get; set; }
    public virtual DateTime CaseDate { get; set; }
    public virtual IList<Document> Documents { get; set; }

    public Case()
    {
        Documents = new List<Document>();
    }
}

public class Document : GuidEntityBase
{
    public virtual DocumentType DocumentType { get; set; }
    public virtual string DocumentNumber { get; set; }
    public virtual DateTime DocumentDate { get; set; }

    public virtual Case Case { get; set; }
}

So when I call following HttpGet,

    [Route("api/document/GetItem/{id}")]
    [HttpGet]
    public Document GetItem(string Id)
    {
        var response = service.GetItem(Id);

        //response.Value.Case = null;

        return response.Value;
    }

I get document data, but sametime I get case data also. How can I filter this process? I wrote response.Value.Case = null;, but it is not good way for solution.

Elvin Mammadov
  • 25,329
  • 11
  • 40
  • 82
  • 1
    Why don't turn off lazy loading? I'm not NHibernate user, but I'm sure, that there is an option to do this. http://stackoverflow.com/questions/3142845/eager-loading-using-fluent-nhibernate-nhibernate-automapping – Dennis Apr 11 '16 at 06:09

1 Answers1

3

It's a bad idea to send entities across, what you should do is to create a model based on your view, populate it and sent it across.

    public class DocumentDto
    {
        public Guid Id { get; set; }
        public DocumentType DocumentType { get; set; }
        public string DocumentNumber { get; set; }
        public DateTime DocumentDate { get; set; }
    }

    [Route("api/document/GetItem/{id}")]
    [HttpGet]
    public DocumentDto GetItem(string Id)
    {
        var doc = service.GetItem(Id).Value;
        return new DocumentDto(){
            Id = doc.Id,
            //set other properties from doc
        };
    }
Low Flying Pelican
  • 5,974
  • 1
  • 32
  • 43