So, I am having a hard time with this Repository pattern. In lots of articles, posts and etc. I read that the repository should return only database objects(no DTOs, no mappers inside the repository).So you return single database object or list of objects.
This is OK, but for EF(entity framework) performance reasons, it's OK to return only field's that you need, not the entire entity.
So, how are you supposed to do that with repository pattern? You cannot select the database object with specific fields because it is detached
(ctx.Users.Select(new User(){ properties }).FirstOrDefault()
)
, you cannot return an anonymous object, and as I read it's not good practice to return IQueryable<User>
and outside the repository to select desired fields.
Can you share some practice?
We solved it with 2 types of generic functions. You can either use
Select
method's signature looks like)In both cases the repository doesn't return the entity anymore, but it's also not responsible for for the transformation of the object, instead the Automapper library, or the caller with the select expression are taking care of that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With