Given this:
namespace TheEntities
{
[DataContract(IsReference=true)]
public class Question
{
[DataMember] public virtual int QuestionId { get; set; }
[DataMember] public virtual string Text { get; set; }
[DataMember] public virtual string Poster { get; set; }
[DataMember] public virtual IList<QuestionComment> Comments { get; set; }
[DataMember] public virtual IList<Answer> Answers{ get; set; }
[DataMember] public virtual byte[] RowVersion { get; set; }
}
[DataContract]
public class QuestionComment
{
[DataMember] public virtual Question Question { get; set; }
[DataMember] public virtual int QuestionCommentId { get; set; }
[DataMember] public virtual string Text { get; set; }
[DataMember] public virtual string Poster { get; set; }
}
[DataContract(IsReference = true)]
public class Answer
{
[DataMember] public virtual Question Question { get; set; }
[DataMember] public virtual int AnswerId { get; set; }
[DataMember] public virtual string Text { get; set; }
[DataMember] public virtual string Poster { get; set; }
[DataMember] public virtual IList<AnswerComment> Comments { get; set; }
}
[DataContract]
public class AnswerComment
{
[DataMember] public virtual Answer Answer { get; set; }
[DataMember] public virtual int AnswerCommentId { get; set; }
[DataMember] public virtual string Text { get; set; }
[DataMember] public virtual string Poster { get; set; }
}
}
Entity Framework didn't produce duplicate objects for Answer, QuestionComment, AnswerComment while NHibernate does.
public Question OpenQuestion(int id)
{
var repo = QuestionRepository;
var query = repo.All.Where(y => y.QuestionId == id);
if (QuestionRepository.GetType() == typeof(EfRepository<Question>))
{
query = query
.Include("Answers")
.Include("Answers.Comments")
.Include("Comments");
return query.Single();
}
else if (QuestionRepository.GetType() == typeof(NhRepository<Question>))
{
// kinda sad, produces duplicate objects
query = query
.FetchMany(x => x.Answers)
.ThenFetchMany(x => x.Comments)
.FetchMany(x => x.Comments);
}
else
throw new Exception("Something unsupported");
return query.Single();
}
This also produces duplicate objects (three levels deep, using three relations):
query = query
.FetchMany(x => x.Answers)
.ThenFetchMany(x => x.Comments)
This also produces duplicate objects(two levels deep only, but using three relations):
query = query
.FetchMany(x => x.Answers)
.FetchMany(x => x.Comments);
This doesn't produces duplicate objects, however the eager loading is for two levels deep only and two relations, i.e. from Question to Answer. For comments to question, and comments to answer, they are performed on separate query.
query = query
.FetchMany(x => x.Answers);
If NHibernate can only do its job well for two levels FetchMany only with two relations only, why bother creating ThenFetchMany(used on three levels, but has error, has duplicate objects)? In fact, even FetchMany is useless too if you want to use it on three relations also, it produces duplicate objects too.
Can NHibernate team be bothered to remove ThenFetchMany anyway since it can't work properly?
There's no error in my mapping, things are working properly (i.e. doesn't produce duplicate objects) when I removed the fetching strategies.
.TrasformUsing(Transformers.DistinctRootentity)
. this is annoying sure. – Firo.TransformUsing
? – Michael Buen