Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I returning IQueryable<T> from a repository in DDD

Recently, I am refactoring my DDD project. When I look at my Repository Layer. I found it return IQueryable< T> in my repository. I am puzzled and should I return IQueryable< T> from a repository in my DDD project in Repository Layer? I usually return IQueryable type in my Repository design. But today I found a opposite idea from this article I can't figure it out!

like image 304
doublnt Avatar asked Mar 09 '23 22:03

doublnt


1 Answers

If you return IQueryable you permit domain knowledge leaking from Domain layer to the consumer layers. It increases the risk that your Domain objects will become anemic and all behavior will move to other layers.

Although it seams very handy to return a IQueryable and you think that your code becomes simpler, that is just an illusion; when the project will grow that IQueryable will transform your code into a big ball of mud, with domain code scattered every where. You won't be able to optimize your repository or to change one persistence with another (i.e. from sql to nosql).

like image 59
Constantin Galbenu Avatar answered Mar 19 '23 03:03

Constantin Galbenu