简体   繁体   English

返回一个IEnumerable <T> 或清单 <T> 从图书馆

[英]Return an IEnumerable<T> or List<T> from library

I'm designing a library that connects to a data source (similar to a DB) and returns various POCO objects. 我正在设计一个连接到数据源(类似于DB)并返回各种POCO对象的库。

I'm using the datasource vendors .NET library to make the connection (and retrieve the objects), which I'm then converting into my much lighter POCO objects. 我正在使用数据源供应商.NET库进行连接(并检索对象),然后将其转换为轻得多的POCO对象。 To build these POCO objects, I have a static method on each POCO class ( .FromDBObject(DBObject obj) that takes the DB object and returns a new POCO instance. 为了构建这些POCO对象,我对每个POCO类( .FromDBObject(DBObject obj)都有一个静态方法,该方法接受DB对象并返回一个新的POCO实例。

The initial connection to the DB isn't fast so it's not something I want to be doing on the fly (per POCO build), so I'm wrapping a library around it that manages the connection to limit this slowdown, as well as dispose of the connection correctly. 到数据库的初始连接不是很快,所以不是我想即时执行的操作(每个POCO构建),因此我在其周围包装了一个库,用于管理连接以限制此速度并进行处理正确连接。

As part of this library, I have a number of methods that return lists of my POCO's. 作为该库的一部分,我有许多方法可以返回POCO的列表。 One 'oddity' of the DB objects is that they aren't loaded into memory when the vendor library loads them, they require an open connection to load their data. 数据库对象的一个​​“怪癖”是,当供应商库加载它们时,它们没有被加载到内存中,它们需要一个开放的连接来加载数据。

As a result, if I return an IEnumerable<POCO> then it's possible that the connection could be closed (time/dispose) before this has been enumerated, thus crashing when creating the POCO's. 结果,如果我返回IEnumerable<POCO>则有可能在枚举连接之前关闭连接(时间/处置),从而在创建POCO时崩溃。

So a long set up for a short question. 因此,为简短的问题准备了很长时间。

  1. Should I return IEnumerable<POCO> but in the actual method perform a .ToList() to force the instantiation or should I specify the return type as List<POCO> ? 我应该返回IEnumerable<POCO>但是在实际方法中执行.ToList()来强制实例化,还是应该将返回类型指定为List<POCO>

Your method return type should have little bearing on your data access. 您的方法返回类型应该与数据访问无关。 Determine whether you want to return an IEnumerable or a List first. 确定要先返回IEnumerable还是List If IEnumerable is sufficient, then use a ToList() . 如果IEnumerable足够,则使用ToList()

If you need the extra methods that List provides, then returning List is fine since it does an implicit ToList() call, although I would explicitly call ToList() anyway just to make it clear that you want load the data from the database at that point in the code. 如果您需要List提供的额外方法,则返回List会很好,因为它会执行隐式的ToList()调用,尽管无论如何我都会明确地调用ToList()只是为了清楚地表明您想要从该数据库中加载数据点在代码中。

As you mentioned you must already force an eager evaluation of the query. 如前所述,您必须已经对查询进行了迫切的评估。 You have two choices for the eager evaluation 1. ToList() 2. ToArray() 渴望评估有两种选择。1. ToList()2. ToArray()

Pros and cons for returning an IList 返回IList的利弊

If consumers frequently need the data in an IList format then you are saving an extra enumeration (since you are returning query.ToList() instead of query.ToArray().. if subscribers needed the list they would need to effectively call query.ToArray().ToList(), causing a double enumeration). 如果使用者经常需要IList格式的数据,那么您将节省一个额外的枚举(因为返回的是query.ToList()而不是query.ToArray()..如果订户需要列表,则他们需要有效地调用query.ToArray ().ToList(),导致两次枚举)。 The con is that changing your return type from ILIST to IEnumerable would be breaking. 缺点是将返回类型从ILIST更改为IEnumerable会很麻烦。

Personally I would go with IEnumerable because it's making fewer assumptions. 就个人而言,我会选择IEnumerable,因为它所做的假设更少。 I assume if you really needed an IList method you could just add a special ListFromDBObject(DBOject obj) method 我假设如果您确实需要IList方法,则可以添加一个特殊的ListFromDBObject(DBOject obj)方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM