CSharp: Lazy Load Pattern in donet core 6

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Geovin.Du.DuLazyLoad.DuGhosts
{

    /// <summary>
    /// Lazy Load Pattern
    /// </summary>
    public enum LoadStatus
    {
        /// <summary>
        /// 无加载
        /// </summary>
        Unloaded,
        /// <summary>
        /// 正加哉
        /// </summary>
        Loading,
        /// <summary>
        /// 已加载
        /// </summary>
        Loaded,
    }

}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Geovin.Du.DuLazyLoad.DuGhosts
{

    /// <summary>
    /// Lazy Load Pattern
    /// </summary>
    public class GhostCollege : DomainObject
    {
        private string? _missionStatement;
        private int? _numberOfStudents;
        private Library? _library;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        public GhostCollege(int id)
            : base(id)
        {
            Id = id;
            Console.WriteLine("学院初始化完成.");
        }
        /// <summary>
        /// 
        /// </summary>
        public string MissionStatement
        {
            get
            {
                LoadIfNecessary();
                return _missionStatement!;
            }
            set => _missionStatement = value;
        }
        /// <summary>
        /// 
        /// </summary>
        public int NumberOfStudents
        {
            get
            {
                LoadIfNecessary();
                return _numberOfStudents!.Value;
            }
            set => _numberOfStudents = value;
        }
        /// <summary>
        /// 
        /// </summary>
        public Library Library
        {
            get
            {
                LoadIfNecessary();
                return _library!;
            }
            set => _library = value;
        }
        /// <summary>
        /// 
        /// </summary>
        public void ShowDetails() =>
            Console.WriteLine(
                $"大学的使命是: '{MissionStatement}'.\n" +
                $"这所学院图书馆拥有藏书 {Library.NumberOfBooks} 册, " +
                $"成立于 {Library.EstablishmentDate.ToShortDateString()}.");
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        protected override object[] FetchData() =>
            new object[3]
            {
            "学习、发现、治愈、创造,让世界变得更美好",
            850,
            new Library(),
            };
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dataRow"></param>
        protected override void ApplyLoadedData(object[] dataRow)
        {
            Console.WriteLine("数据加载中...");

            _missionStatement = (string)dataRow[0];
            _numberOfStudents = (int)dataRow[1];
            _library = (Library)dataRow[2];
        }
    }

}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Geovin.Du.DuLazyLoad.DuGhosts
{

    /// <summary>
    /// Lazy Load Pattern
    /// </summary>
    public abstract class DomainObject
    {
        private LoadStatus _status;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        protected DomainObject(int id)
        {
            Id = id;
        }

        public int Id { get; init; }
        public bool IsGhost => _status == LoadStatus.Unloaded;
        public bool IsLoaded => _status == LoadStatus.Loaded;

        /// <summary>
        /// Note template method pattern.
        /// </summary>
        public virtual void LoadIfNecessary()
        {
            if (!IsGhost)
            {
                return;
            }

            var dataRow = FetchData();

            MarkAsLoading();
            ApplyLoadedData(dataRow);
            MarkAsLoaded();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        protected abstract object[] FetchData();
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dataRow"></param>
        protected abstract void ApplyLoadedData(object[] dataRow);
        /// <summary>
        /// 
        /// </summary>
        private void MarkAsLoading() => _status = LoadStatus.Loading;
        /// <summary>
        /// 
        /// </summary>
        private void MarkAsLoaded() => _status = LoadStatus.Loaded;
    }

}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Geovin.Du.DuLazyLoad.DuGhosts
{

    /// <summary>
    /// Lazy Load Pattern
    /// </summary>
    public class CollegeFactory
    {

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public GhostCollege CreateFromId(int id) => new(id);
    }

}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Geovin.Du.BuildingBlocks;


namespace Geovin.Du.DuLazyLoad.DuGhosts
{

    /// <summary>
    /// Lazy Load Pattern
    /// </summary>
    public static class GhostsExecutor
    {

        /// <summary>
        /// 
        /// </summary>
        public static void Execute()
        {
            ConsoleExtension.WriteSeparator("Lazy Load Pattern:Ghosts demo");

            var collegeFactory = new CollegeFactory();
            var college = collegeFactory.CreateFromId(1);

            // College library shouldn't have been constructed before calling ShowDetails method.
            Console.WriteLine("给我更多关于这所大学的细节!");
            college.ShowDetails();

            Console.WriteLine("\n给我更多关于这所大学的细节!");
            college.ShowDetails();
        }
    }
}

  

调用:

Geovin.Du.DuLazyLoad.DuGhosts.GhostsExecutor.Execute();

  

输出:

Lazy Load Pattern:Ghosts demo
--------------------------------------------------
学院初始化完成.
给我更多关于这所大学的细节!
加载库...
数据加载中...
大学的使命是: '学习、发现、治愈、创造,让世界变得更美好'.
这所学院图书馆拥有藏210000册 ,成立于 2008/5/28.

给我更多关于这所大学的细节!
大学的使命是: '学习、发现、治愈、创造,让世界变得更美好'.
这所学院拥有图书馆210000 书 和 成立于 2008/5/28.

  

 

posted @ 2023-01-24 16:47  ®Geovin Du Dream Park™  阅读(10)  评论(0编辑  收藏  举报