2

I have a simple class hierarchy looking like this:

public class Top
{
    public string Id { get; set; }
    public string Description { get; set; }
    public List<Middle> Middles { get; set; } 
}

public class Middle
{
    public string Id { get; set; }
    public string Description { get; set; }
    public List<Bottom> Bottoms { get; set; } 
}

public class Bottom
{
    public string Id { get; set; }
    public string Description { get; set; }
}

The whole thing is saved as entity of type 'Top'. Document is designed to preserve and reflect relationships/hierarchy but half but at time I will, for example, care only about an 'Id' and 'Description' of a given relationship. So, the types of queries I'd want to run are

  • select all Top,
  • select all Middle,
  • select Middle where Top.Id=somevalue
  • select Bottom where Top.Id=somevalue and Middle.Id=somevalue

I would like the results to be transformed and returned to me like this:

public class Result
    {
        public int Id { get; set; }
        public string Description { get; set; }
    }

How can I implement TransformResults (I presume that that's the feature that can be used) to achieve this? I've read quite a few examples but all of the sudden I see parameters/values, which were not declared anywhere and as a result I don't understand what's happening.

user981375
  • 639
  • 1
  • 5
  • 15

1 Answers1

1

TransformResults doesn't have access to the outside world, you can't execute logic based on the query that you run. You can flatten this structure, sure, but unless you will create multiple indexes with different TransformResults, you can't do this. Note that this is a strange thing to do in the first place, because it doesn't matches the standard modeling of documents as a transaction boundary.

casperOne
  • 73,706
  • 19
  • 184
  • 253
Ayende Rahien
  • 22,925
  • 1
  • 36
  • 41
  • Ok, so what should I do in this case? Shoud I break this apart and treat Top, Middle and Bottom as separate documents? Why is my approach "strange"? Is it too relational? I'm only trying to understand what I'm doing wrong and what's the right way to do it. Thanks for help! – user981375 Aug 06 '12 at 13:27
  • I think I might have confused the issues. After reading http://stackoverflow.com/questions/7829379/ravendb-retrieving-part-of-document?rq=1 Live Projections produce what I wanted. If I specify my quey like `var top = session.Query.First(t => t.Id.Equals(1)).Middles.Select(m => new {m.Id, m.Description}).ToList()` then I get all Middles for a given Top.Id, which is exactly what I wanted. In the same fashion I can navigate all the way down. This works very well but is this the way to do it? – user981375 Aug 06 '12 at 17:11