开发者

EF 4.0 Repository pattern IList<T> or IEnumerable<T>

开发者 https://www.devze.com 2023-01-12 19:37 出处:网络
I did look at common implementations of EF Repository pattern on the web. Then i came with such question. For example if i have method like that:

I did look at common implementations of EF Repository pattern on the web. Then i came with such question. For example if i have method like that:

    public IEnumerable<Entity> FindEntity(Expression<Func<Entity, bool> where) {...}

Than If i have such case where i need to have IList instead of IEnumarable

i may create IList based on my IEnumerable FindEntity implementation

    IList<Entity> myList = new List<Entity>(FindEntity(x = x => x.Date == inputDate));

then i may have all the advantages of IList int开发者_开发问答erface (Count, etc...) in myList instance.

Is this good approach to do like that or may be there is better ways of casting IEnumerable to IList or also as an option i may have additional method in Repository which return IList by default e.g

    public IList<Entity> FindEntity(Expression<Func<Entity, bool> where) {....} 

in that case i may remove IEnumerable FindEntity implementation at all because I may simply cast IList to IEnumerable when i need it, or i can live both implementations just avoid any casts.

And one more question: Why common Repository implementation practice to have only IEnumerable not IList, there should be explanation for that? Or it is only personal preferences?

Yes i understand that IEnumerable is much more lighter rather than IList but anyway there is a lot situations where we need to have IList instead of IEnumerable and in this approach we need to cast IEnumerable To IList isn't it?


i think its a choice thing. IEnumerable is the least common denominator for a collection. Also you can foreach and write linq statements. ICollection or Ilist being higher up in the layer exposes more features that you may not want to allow such as adding something to the collection, removing things like that.

0

精彩评论

暂无评论...
验证码 换一张
取 消