Repository Pattern - POCOs or IQueryable?

梦想的初衷 提交于 2019-12-03 16:53:46

问题


I'm new to the Repository Pattern and after doing a lot of reading on the web I have a rough understanding of what is going on, but there seems to be a conflict of ideas.

One is what the IRepository should return.

I would like to deal in ONLY Pocos so I would have an IRepository implementation for every aggregate root, like so:

public class OrangeRepository: IOrangeRepository
{
  public Orange GetOrange(IOrangeCriteria criteria);
}

where IOrangeCriteria takes a number of arguments specific to finding an Orange.

The other thing I have is a number of data back-ends - this is why I got into this pattern in the first place. I imagine I will have an implementation for each, e.g

OrangeRepositoryOracle, OrangeRepositorySQL, OrangeRepositoryMock etc

I would like to keep it open so that I could use EF or NHibernate - again if my IOrangeRepository deals in POCOs then I would encapsulate this within the Repository itself, by implementing a OrangeRepositoryNHibernate etc.

Am I on the right lines?

Thanks

EDIT: Thanks for the feedback, I don't have anyone else to bounce these ideas off at the moment so it is appreciated!


回答1:


Yes, your version is the safest / most compatible one. You can still use it with about any resources, not only data access ones, but with web services, files, whatever.

Note that with the IQueryable version you still get to work based on your POCOs classes, but you are tied to the IQueryable. Also consider that you could be having code that uses the IQueryable and then turns out it you hit a case where one of the repository's ORM doesn't handle it well.




回答2:


I use the same pattern as you do. I like it a lot. You can get your data from any resources.

But the advantage of using IQuerable is that you do not have to code your own criteria API like the OrangeCriteria.

When NHibernate gets full Linq support then I may switch to the IQueryable.

Then you get

public class OrangeRepository: IOrangeRepository {  
    public IQueryable<Orange> GetOranges();
}


来源:https://stackoverflow.com/questions/741490/repository-pattern-pocos-or-iqueryable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!