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.
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
};
}