为什么延迟加载 C# 中的数据在我需要之前加载?

亚当·弗罗贝尔

在一个网站上,我读到:

女士加载是延迟加载对象直到我们需要这些数据的概念。换句话说,我们按需加载对象,而不是提前加载不必要的数据。

我想尝试在我的简单控制台应用程序中使用延迟加载。

Student 班级

 public class Students
    {
        public string Name { get; set; }
        public string Surname { get; set; }
        public string PESEL { get; set; }

        private Lazy<List<Students>> LazyStudents;
        

        public Students()
        {
           
        }

        public List<Students> StudentsProperty
        {
            get
            {
                if (LazyStudents == null)
                    LazyStudents = new Lazy<List<Students>>(() => LoadStudensList());
                return LazyStudents.Value;
            }
        }

        public List<Students> LoadStudensList()
        {
            List<Students> tempStudentsList = new List<Students>();
            tempStudentsList.Add(new Students() { Name = "Adam", Surname = "Wróbel", PESEL = "96120904999" });
            tempStudentsList.Add(new Students() { Name = "Edyta", Surname = "Urbańczyk", PESEL = "76354736458" });
            tempStudentsList.Add(new Students() { Name = "Gabriela", Surname = "Rydwańska", PESEL = "72637284923" });
            tempStudentsList.Add(new Students() { Name = "Dawid", Surname = "Rytel", PESEL = "62736482732" });

            return tempStudentsList;
        }
    }

ProgramMain()方法:

 class Program
    {
        static void Main(string[] args)
        {
            Students students = new Students();
            
            Console.WriteLine("Hello World!");
            foreach (var items in students.StudentsProperty)
            {
                    Console.WriteLine($"Imię: {items.Name}");
                    Console.WriteLine($"Nazwisko: {items.Surname}");
                    Console.WriteLine($"PESEL: {items.PESEL}");
                    Console.WriteLine("");
            }

            Console.WriteLine("Hello World!");
        }
    }

我想我使用延迟加载,当我创建学生类 StudentsProperty 的新对象时(从延迟列表返回元素)仍然为空。当我使用使用 StudentsProperty 的方法时,将添加到 LazyList 的元素

 foreach (var items in students.StudentsProperty)
            {
                    Console.WriteLine($"Imię: {items.Name}");
                    Console.WriteLine($"Nazwisko: {items.Surname}");
                    Console.WriteLine($"PESEL: {items.PESEL}");
                    Console.WriteLine("");
            }

我在foreach循环之前添加断点,我看到 StudentsProperty 有元素:在此处输入图片说明

我是否错误地实现了延迟加载,或者我是否理解错误?

代理_L

Lazy.Value 是懒惰得到评估的时候。

  LazyStudents = new Lazy<List<Students>>(() => LoadStudensList());
  return LazyStudents.Value;

没有任何意义。在第一行中,您说“在需要时评估”,第二行说“我现在需要它”。

通过空值检查,您已经自己完成了所有懒惰的评估。使用这个:

    private Lazy<List<Students>> LazyStudents = new Lazy<List<Students>>(() => LoadStudensList());

    public List<Students> StudentsProperty
    {
        get
        {
            return LazyStudents.Value;
        }
    }

或这个:

    private List<Students> LazyStudents;

    public List<Students> StudentsProperty
    {
        get
        {
            if (LazyStudents == null)
                LazyStudents = LoadStudensList();
            return LazyStudents;
        }
    }

它们在概念上应该是相同的。除了多线程保护、调试和可能其他令人讨厌的细节外,大多数情况没有区别

为什么在调试器中看到值的问题有点复杂。查看Lazy source,它应该在调试器中显示 null,感谢internal T ValueForDebugDisplay. 然而,你不是在看懒惰的价值。通过查看Students.StudentsProperty您正在查看您的get和 debbuger 评估 getter 并且您的 getter 强制 Lazy 进行评估。您绕过了 MS 为我们创建的所有调试代码。Lazy直接暴露,这是应该的。或者,而不是调试器,登录LoadStudensList()

底线:Lazy只有在Value被调用之前才懒惰

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

休眠-为什么延迟加载String属性?

禁用的延迟加载仍会加载相关实体。为什么?

为什么在本机查询中,Hibernate延迟加载的子实体?

为什么 svg 加载仅在 Google Chrome 中延迟

在JMeter中,为什么加载时间等于延迟?

C ++动态加载类:为什么需要“销毁”功能?

为什么延迟加载不是React的默认设置?

为什么延迟加载的模块必须导入commonModule?角度2

LazyInitializationException:为什么Hibernate无法在延迟加载时创建会话?

Angular:为什么延迟加载装饰器不起作用?

为什么 Hibernate 延迟加载在 Kotlin 中表现不同?

为什么我必须在数据加载之前点击?

为什么我需要重新加载页面才能从Angular中的subjectSubscription获取数据?

在我重新加载目标C中的表视图时,为什么选择值会取消选择

为什么Webpack延迟加载会从文件夹加载所有文件?

为什么我不对每个延迟加载的关系都使用@BatchSize?

控制什么是延迟的(或渴望的)加载

为什么延迟加载无法在一对一关联中工作?

为什么从Material2中删除了forRoot(),对于延迟加载的功能模块我该怎么办?

为什么在引导加载程序的开头需要 ORG 0x7c00?

为什么我们需要C ++中的基本数据类型

为什么@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)修复了Hibernate延迟加载异常?

Wow.js不会以超过1s的延迟加载动画。为什么?

具有Eager fetch Type属性的对象对该属性的延迟加载属性执行查询。为什么?

将延迟加载的属性显式设置为null时,为什么会出现ObjectDisposedException?

为什么移动浏览器(iOS)上的 img 延迟加载会被破坏?

C ++延迟加载DLL链接器错误

C++ 插件不能延迟加载 dll

延迟加载C#自动实现的属性