LazyList< T> VS System.Lazy<名单,LT; T>>在ASP.NET MVC 2? [英] LazyList<T> vs System.Lazy<List<T>> in ASP.NET MVC 2?

查看:225
本文介绍了LazyList< T> VS System.Lazy<名单,LT; T>>在ASP.NET MVC 2?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在罗布科纳的店面系列,罗布大量使用了 LazyList&LT的; ...> 结构从 IQueryables


  • 这是如何从不同的 System.Lazy< ...> 结构现已在.NET 4.0(或更早也许)


基于DoctaJones伟大的回答更深入:


  1. 你会推荐一个比,如果我想在的IQueryable 的操作之外的列表< T> ?结果
    我假设,因为懒< T> 在框架现在,它是未来支持和维护
  2. 一个更安全的赌注?
  3. 如果我想使用强类型,而不是一个匿名的( VAR )类型将下面的语句是功能上等同?

    • 懒<名单,LT;产品>>产品=新懒人<名单,LT;产品与GT;>();

    • LazyList<产品与GT; =新LazyList<产品及GT;();



解决方案

LasyList< T> 只能与的IQueryable< T> 来源。这是的IList&LT的实现; T> 并通过填充一个私人工程从指定的IQueryable&LT所有结果列表; T&取代。初始化发生第一次访问的IList中的任; T>成员。

用法示例将

  VAR myList中=新LazyList(products.Where(P => p.Name.StartsWith(T));
//初始化发生在这里
Console.Write(myList.Count);

借助 System.Lazy< T> 类适用于任何类型的并且不限于IQueryable的&所述; T&取代。延迟初始化时首次href=\"http://msdn.microsoft.com/en-us/library/dd642177.aspx\" rel=\"nofollow\">懒&LT的属性访问。

用法示例将

  VAR lazyString =新的懒惰(()=>的Hello world);
//初始化发生在这里
Console.Write(lazyString.Value);

我们可以重写LazyList< T>例如使用延迟< T>如下:

  VAR myList中=新懒惰(()=> products.Where(P =方式> p.Name.StartsWith(T)了ToList());
//初始化发生在这里
Console.Write(myList.Value.Count);

简而言之: LazyList< T>仅适用于IQueryable的<吨>中懒< T>与任何类型的作品。

&LazyList LT; T>是当你想要一个IQueryable&LT结果的具体使用情况; T>作为一个List< T&GT ;,但你不希望发生的评价,直到你使用它


更新回答问题展开:


  

你会推荐一个比其他,如果我想在IQueryable的一个列表与下运行; T&GT ;?
  我假设,因为懒< T>在目前的框架,它是未来支持和维护一个更安全的赌注?


我个人也不使用。如果你有一个IQueryable我会保持它作为一个IQueryable最大化您的灵活性。通过保持IQueryable的你仍然可以访问的LINQ to SQL的查询COM prehension(只要环境还活着)。

例如,如果你在一个IQueryable调用.ToList()你问的LINQ to SQL从目标表和水合物的所有选择所有列结果(这可能是非常昂贵的,特别是如果你已经有了结果十万)。这将让翻译成类似SELECT * FROM MyTable的。

如果您在IQueryable的调用.Count之间的()你问LINQ to SQL中只得到结果的数量,这将让翻译成类似SELECT COUNT(*)FROM MyTable的。这样做比保湿所有结果,然后计算它们,特别是如果你只在一些有关更高效!

通过对IQueryable的LINQ to SQL的使用。凡()将您的条件添加到SQL查询的WHERE子句。这意味着,你将只能拉离SQL您感兴趣的数据,而不是保湿,你有没有使用意向的结果。

您看,通过保持你的IQueryable使事情为自己灵活得多。大部分的时候它会给你更好的性能比保湿整个结果​​集。


  

如果我想使用强类型,而不是一个匿名(VAR)键入将下面的语句是功能上等同?


 懒<名单,LT;产品与GT;>产品=新懒人<名单,LT;产品与GT;>();
LazyList<产品与GT;产品=新LazyList<产品及GT;();

我认为你得到与隐式类型混淆匿名打字。使用var关键字声明的变量隐式类型,以匹配被分配给它的类型。它的强类型,因此,初始分配后不能更改。

这两个语句并不功能等效。该LazyList<产品与GT;是一个IList<产品>中wheras懒惰<名单,LT;产品与GT;>是一个包含列表℃的包装;产品取代。因为你在一个懒惰的评价列表上操作特别感兴趣,我想说的是LazyList的可能的更具体的为你的目的。

您应该问自己,如果你的真的需要的产品的实际列表。如果没有具有实际的名单我会与IQueryable的坚持一个令人信服的理由。

In Rob Conery's Storefront series, Rob makes extensive use of the LazyList<..> construct to pull data from IQueryables.

  • How does this differ from the System.Lazy<...> construct now available in .NET 4.0 (and perhaps earlier)?

More depth based on DoctaJones' great answer:

  1. Would you recommend one over the other if I wanted to operate on IQueryable as a List<T>?
    I'm assuming that since Lazy<T> is in the framework now, it is a safer bet for future support and maintainability?
  2. If I want to use a strong type instead of an anonymous (var) type would the following statements be functionally equivalent?
    • Lazy<List<Products>> Products = new Lazy<List<Product>>();
    • LazyList<Product> = new LazyList<Product>();

解决方案

The LasyList<T> only works with an IQueryable<T> source. It is an implementation of IList<T> and works by populating a private List with all results from the specified IQueryable<T>. The initialization occurs the first time you access any of the IList<T> members.

Example usage would be

var myList = new LazyList(products.Where(p => p.Name.StartsWith("T"));
//initialization occurs here
Console.Write(myList.Count);

The System.Lazy<T> class works with any type and is not limited to IQueryable<T>. Lazy initialization occurs the first time the Lazy<T>.Value property is accessed.

Example usage would be

var lazyString = new Lazy(() => "Hello world");
//initialization occurs here
Console.Write(lazyString.Value);

We could rewrite the LazyList<T> example to use Lazy<T> as follows:

var myList = new Lazy(() => products.Where(p => p.Name.StartsWith("T").ToList());
//initialization occurs here
Console.Write(myList.Value.Count);

In short: LazyList<T> only works with IQueryable<T>, Lazy<T> works with any type.

LazyList<T> is for the specific use case of when you want the results of an IQueryable<T> as a List<T>, but you don't want the evaluation to occur until you use it.


UPDATE to answer expanded question:

Would you recommend one over the other if I wanted to operate on IQueryable as a List<T>? I'm assuming that since Lazy<T> is in the framework now, it is a safer bet for future support and maintainability?

Personally I wouldn't use either. If you've got an IQueryable I would keep it as an IQueryable to maximise your flexibility. By keeping the IQueryable you still get access to LINQ to SQL's query comprehension (as long as the context is still alive).

For example, if you call .ToList() on an IQueryable you are asking LINQ to SQL to select all of the columns from the target table and hydrate all of the results (this could be very expensive, especially if you've got thousands of results). This will get translated to something like "SELECT * FROM MyTable".

If you call .Count() on the IQueryable you are asking LINQ to SQL to just get the number of results, this will get translated to something like "SELECT COUNT(*) FROM MyTable". Doing this is much more efficient than hydrating all of the results and then counting them, especially if you're only interested in the number!

By using .Where() on the IQueryable LINQ to SQL will add your conditions to the WHERE clause in the SQL query. This means that you will only pull the data from SQL that you're interested in, rather than hydrating results that you have no intention of using.

You see, by keeping the IQueryable you make things much more flexible for yourself. The majority of the time it will give you better performance than hydrating the entire result set.

If I want to use a strong type instead of an anonymous (var) type would the following statements be functionally equivalent?

Lazy<List<Product>> products = new Lazy<List<Product>>(); 
LazyList<Product> products = new LazyList<Product>();

I think you're getting anonymous typing confused with implicit typing. A variable declared using the var keyword is implicitly typed to match the type that is being assigned to it. It is strongly typed and therefore cannot be changed after the initial assignment.

The two statements are not functionally equivalent. The LazyList<Product> is an IList<Product>, wheras the Lazy<List<Product>> is a wrapper that contains a List<Product>. Because you're specifically interested in operating on a lazy evaluated list, I'd say the LazyList is probably more specific for your purposes.

You should ask yourself if you really need an actual List of products. If there isn't a compelling reason for having an actual List I'd stick with the IQueryable.

这篇关于LazyList&LT; T&GT; VS System.Lazy&LT;名单,LT; T&GT;&GT;在ASP.NET MVC 2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆