Hello there,
I have a class responsible to upload, download and delete files to a cloud storage (Azure). To upload the files I can override the OnSaving() method in which I call the storageHelper Upload method to store the file in the cloud (see code below). However I can't find any method to override when the object is deleted, as I want to delete it as well from the cloud storage.
In XPO objects you have got the OnDeleting() method. Is there any similar method for EF objects? If not, do you have any suggestions for a solution?
This is the Class I'm talking about:
Codepublic class FileDataReference : BaseObject, IFileData, IEmptyCheckable
{
private readonly AzureStorageHelper storageHelper;
public virtual Guid FileId { get; set; }
public virtual string? FileName { get; set; }
public virtual int Size { get; set; }
public virtual long SizeLong { get; set; }
public virtual required string Container { get; set; }
public virtual bool IsEmpty => Size == 0;
string? fileToClear;
byte[]? bytesToSave;
public FileDataReference()
{
storageHelper = new AzureStorageHelper();
}
public override void OnCreated()
{
base.OnCreated();
FileId = Guid.NewGuid();
Container = "images";
}
public void Clear()
{
bytesToSave = null;
fileToClear = FileName;
FileName = null;
Size = 0;
SizeLong = 0;
}
public void LoadFromStream(string fileName, Stream stream)
{
using var ms = new MemoryStream();
stream.CopyTo(ms);
FileName = fileName;
Size = stream.Length <= long.MaxValue ? ((int)stream.Length) : int.MaxValue;
SizeLong = stream.Length;
bytesToSave = ms.ToArray();
fileToClear = null;
}
public void SaveToStream(Stream stream)
{
storageHelper.Download(Container, FileId, ref stream);
}
public override void OnSaving()
{
base.OnSaving();
storageHelper.Upload(Container, FileId, FileName, bytesToSave);
bytesToSave = null;
}
public override void Deleting() ===> **This method does not exist!**
{
Clear();
storageHelper.Delete(Container, FileId);
}
}
Thank you in advance,
Kind regards,
Herman.