使用延迟类正确使用延迟加载功能

时间:2019-05-20 07:53:44

标签: c# lazy-loading

我有1个长期运行的过程,该过程分为以下2个步骤:

1)转型(长期运行)

2)版本管理

因此,一旦完成步骤1,便会执行步骤2。现在,对于第二步执行,我已经完成了 Versioning 惰性加载,如下所示:

public sealed class VariantProcessor
{
    private readonly Lazy<IEntityVersion> _entityVersion;
    private string _myAppConnectionString { get; set; }
    private readonly Action<Variant> _transform;

    public VariantProcessor(Func<IEntityVersion> entityVersion, string _myAppConnectionString, Action<Variant> transform)
    {
        _entityVersion = new Lazy<IEntityVersion>(entityVersion);
        _myAppConnectionString = _myAppConnectionString;
        _transform = transform;
    }

    public void Process(Variant model)
    {
        string variantVersion = string.empty;
        try
        {
            _transform(model);
            try
            {
                variantVersion = _entityVersion.Value.GetVersion();
            }
            catch (Exception) 
            {
                //rollback Transform operation
            }
        }
        catch (Exception) 
        {
        }
    }
}

public class AggregateCalculator : IVariantProcessor
{
    private string _myAppConnectionString { get; set; }  

    public void Process(Variant model)
    {
        _myAppConnectionString = ConfigurationManager.ConnectionStrings["dbConnectionString"].ConnectionString;
        new VariantProcessor(() => new EntityVersion(_myAppConnectionString), 
       _myAppConnectionString,Transform).
        Process(model);
    }

    private void Transform(Variant model)
    {
        //logic
    }
}

这是我在转换后需要执行的使用惰性加载来调用版本创建的方式:

variantVersion = _entityVersion.Value.GetVersion();

我是否正确使用了延迟加载?还是在这里不需要?

更新:将“版本控制”作为延迟加载的想法是避免在转换过程进行之前将其对象存储在内存中。我希望仅在其请求时才可用。

0 个答案:

没有答案