我有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();
我是否正确使用了延迟加载?还是在这里不需要?
更新:将“版本控制”作为延迟加载的想法是避免在转换过程进行之前将其对象存储在内存中。我希望仅在其请求时才可用。