In my application there are a fair number of existing "service commands" which generally return a List<TEntity>
. However, I wrote them in such a way that any queries would not be evaluated until the very last statement, when they are cast ToList<TEntity>
(or at least I think I did).
Now I need to start obtaining some "context-specific" information from the commands, and I am thinking about doing the following:
- Keep existing commands largely the same as they are today, but make sure they return an
IEnumerable<TEntity>
rather than anIList<TEntity>
. - Create new commands that call the old commands but return
IEnumerable<TResult>
whereTResult
is not an entity but rather a view model, result model, etc - some representation of the data that is useful for the application.
The first case in which I have needed this is while doing a search for a Group
entity. In my schema, Group
s come with User
-specific permissions, but it is not realistic for me to spit out the entire list of users and permissions in my result - first, because there could be many users, second, because there are many permissions, and third, because that information should not be available to insufficiently-privileged users (ie a "guest" should not be able to see what a "member" can do).
So, I want to be able to take the result of the original command, an IEnumerable<Group>
, and describe how each Group
ought to be transformed into a GroupResult
, given a specific input of User
(by Username
in this case).
If I try to iterate over the result of the original command with ForEach
I know this will force the execution of the result and therefore potentially result in a needlessly longer execution time. What if I wanted to further compose the result of the "new" command (that returns GroupResult
) and filter out certain groups? Then maybe I would be calculating a ton of privileges for the inputted user, only to filter out the parent GroupResult
objects later on anyway!
I guess my question boils down to... how do I tell C# how I'd like to transform each member of the IEnumerable
without necessarily doing it at the time the method is run?