2018-02-12 12:21:55 +01:00
|
|
|
|
/*
|
2024-03-05 18:37:42 +01:00
|
|
|
|
* Copyright (c) 2024 ETH Zürich, IT Services
|
2018-02-12 12:21:55 +01:00
|
|
|
|
*
|
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
using System;
|
2019-08-30 09:55:26 +02:00
|
|
|
|
using SafeExamBrowser.Core.Contracts.OperationModel;
|
|
|
|
|
using SafeExamBrowser.Core.Contracts.OperationModel.Events;
|
2018-02-12 12:21:55 +01:00
|
|
|
|
|
2018-08-31 10:06:27 +02:00
|
|
|
|
namespace SafeExamBrowser.Core.Operations
|
2018-02-12 12:21:55 +01:00
|
|
|
|
{
|
2018-03-06 14:35:10 +01:00
|
|
|
|
/// <summary>
|
2018-09-25 12:10:34 +02:00
|
|
|
|
/// A wrapper operation to allow for a lazy (just-in-time) instantiation of an operation, initialized on <see cref="Perform"/>.
|
|
|
|
|
/// Is useful when e.g. dependencies for a certain operation are not available during execution of the composition root, but rather
|
|
|
|
|
/// only after a preceding operation within an <see cref="IOperationSequence"/> has finished.
|
2018-03-06 14:35:10 +01:00
|
|
|
|
/// </summary>
|
2018-09-25 12:10:34 +02:00
|
|
|
|
public class LazyInitializationOperation : IOperation
|
2018-02-12 12:21:55 +01:00
|
|
|
|
{
|
|
|
|
|
private Func<IOperation> initialize;
|
|
|
|
|
private IOperation operation;
|
|
|
|
|
|
2018-10-03 14:35:27 +02:00
|
|
|
|
private event ActionRequiredEventHandler ActionRequiredImpl;
|
|
|
|
|
private event StatusChangedEventHandler StatusChangedImpl;
|
|
|
|
|
|
|
|
|
|
public event ActionRequiredEventHandler ActionRequired
|
|
|
|
|
{
|
|
|
|
|
add
|
|
|
|
|
{
|
|
|
|
|
ActionRequiredImpl += value;
|
|
|
|
|
|
|
|
|
|
if (operation != null)
|
|
|
|
|
{
|
|
|
|
|
operation.ActionRequired += value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
remove
|
|
|
|
|
{
|
|
|
|
|
ActionRequiredImpl -= value;
|
|
|
|
|
|
|
|
|
|
if (operation != null)
|
|
|
|
|
{
|
|
|
|
|
operation.ActionRequired -= value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public event StatusChangedEventHandler StatusChanged
|
|
|
|
|
{
|
|
|
|
|
add
|
|
|
|
|
{
|
|
|
|
|
StatusChangedImpl += value;
|
|
|
|
|
|
|
|
|
|
if (operation != null)
|
|
|
|
|
{
|
|
|
|
|
operation.StatusChanged += value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
remove
|
|
|
|
|
{
|
|
|
|
|
StatusChangedImpl -= value;
|
|
|
|
|
|
|
|
|
|
if (operation != null)
|
|
|
|
|
{
|
|
|
|
|
operation.StatusChanged -= value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-02-12 12:21:55 +01:00
|
|
|
|
|
2018-09-25 12:10:34 +02:00
|
|
|
|
public LazyInitializationOperation(Func<IOperation> initialize)
|
2018-02-12 12:21:55 +01:00
|
|
|
|
{
|
|
|
|
|
this.initialize = initialize;
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-28 15:49:06 +01:00
|
|
|
|
public OperationResult Perform()
|
2018-02-12 12:21:55 +01:00
|
|
|
|
{
|
|
|
|
|
operation = initialize.Invoke();
|
2018-10-03 14:35:27 +02:00
|
|
|
|
operation.ActionRequired += ActionRequiredImpl;
|
|
|
|
|
operation.StatusChanged += StatusChangedImpl;
|
2018-02-12 12:21:55 +01:00
|
|
|
|
|
2018-02-28 15:49:06 +01:00
|
|
|
|
return operation.Perform();
|
2018-02-12 12:21:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-10 09:19:03 +02:00
|
|
|
|
public OperationResult Revert()
|
2018-02-12 12:21:55 +01:00
|
|
|
|
{
|
2018-10-10 09:19:03 +02:00
|
|
|
|
return operation.Revert();
|
2018-02-12 12:21:55 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|