What should be the right practice to handle duplicate linq queries to Database?
By duplicate queries, I mean such queries that recurs in my code.
Example:
I notice that in my code recurs following query:
dataContext.Users.Where(u =>u.IsActive && u.IsAdmin).OrderByDescent(u => u.Name)
Where should I place the method that contains above query. Should it be UserRepository(in this manner, I will probably end with god class which is bad practice)? or should I create class architecture to handle this.
I wonder what are your thoughts about this. And what class structure in your opinion will be most generic and vulnerable to reuse.
I have in mind that, I can use a store procedure as a place for recurring queries, but I don’t want to go that way.
Yes, it can be in the
UserRepository, but you can also build up anGodclassfor standardized queries (Create, Delete, and so on) and take use of anService LayerorExtensionsto Filter the results fromGetAll()Method in theUserRepository.Update
I’m trying to show it a little bit more detailed.
Some code:
This approach is related to Rob Conerys MVC StoreFront Preview. The implementation maybe vary in other projects, but this is one way to do it.