Eager loading with ormlite servicestack
Asked Answered
H

1

12

This is entity framework:

var department = _context.Departments
                .Include(dep => dep.Employees.Select(emp => emp.ContactTypes))
                .SingleOrDefault(d => d.Id == departmentId);

Here I expect one department to be returned containing all related employees and all contact types for each employee.

This is ormlite servicestack:

I have no idea. When I look at the docu/samples: https://github.com/ServiceStack/ServiceStack.OrmLite

They write:

Right now the Expression support can satisfy most simple queries with a strong-typed API. For anything more complex (e.g. queries with table joins) you can still easily fall back to raw SQL queries as seen below.

I have seen there is a JoinSqlBuilder class but I do not think it can return nested collections.

Maybe what I want is not possible but maybe I can do a compromise like get all employees for the departmentId. Then I inmemory foreach the employees and fetch all contact types for a certain employeeId. Creating the hierarchy and assigning the lists would still be my job.

But I hope there is a shorter solution.

What would also be fine is when the query however it might look like return an object (Dynamic?) with 3 flat properties: Department, Employees, ContactTypes and assign thoese properties to my DTO.

Humiliate answered 17/12, 2013 at 22:23 Comment(4)
@Voters All you guys should go to: servicestack.uservoice.com/forums/176786-feature-requests/… and vote there with your maximum 3 votes than we can surpass the top feature request !!!Humiliate
@Voters Cool some guys have already voted :pHumiliate
If you check the code now they have Load Reference and Save Reference, I never use it though since it's feature for version 4Brooder
LoadReference just loads the Employees in my scenario not the Contact Types... Its a simple ById fetch on the first level.Hardej
B
0

Ok, please don't take this as a definitive answer, but more just my take on the situation (I don't use service stack very much) however...

When I first started to use EF many years ago, I came across a similar situation, where the references just would not load. Like you I was faced with the likely hood of having to enumerate the individual collections myself and write a lot of extra code for an operation the ORM should be able to handle easily.

What I ended up doing, was to use auto-mapper, which basically reduce all the multiline loops I had everywhere to a single line mapping statement.

Granted, I still had to do one mapping statement for each linked property, but it reduced the code I had to write, and more importantly got me up and running until EF improved, or I found a better way of doing things.

Let me stress, I'm not proposing this as an answer, and it's a bit big for a comment, I'm simply suggesting shifting your thought in a different direction, that may help a better solution come to the surface.

Biggs answered 10/2, 2014 at 10:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.