I have a class with a virtual property (public virtual List<Ingredients>), but in some cases I don’t want to get that data. How can I do this? I know about .Include and .Select, but if I do that, I need to change lots of code cause in my repository I return a Queryable and Service layer too.
I have a class with a virtual property ( public virtual List<Ingredients> ), but
Share
An indirect answer: I would not expose
IQueryablein a service layer. For (at least) three reasons:So if you expose
IEnumerables of objects that you need in your application you can offer objects that have exactly those navigation properties loaded that are needed in a specific scenario. E.g. a maintenance function for product entities (name etc.) would get bare products only (and no code would ever access ingredients). A function that composes products from ingredients would get its ingredients as well. Service/repository methods should have parameters to specify what you need.If you want 100% guarantee that lazy navigation properties are not going to be accessed you need to expose projected objects (or DTO’s) to your application.