Add in-memory report storage and async report handling
Introduced `InMemoryReportStorageWebExtension` to manage reports in memory, enabling dynamic report storage and retrieval. Updated `ReportViewer.razor` to conditionally render the `DxReportViewer` component and initialize reports asynchronously. Configured dependency injection in `Program.cs` to register `InMemoryReportStorageWebExtension` as a singleton and integrated it with `CustomReportProvider`. Registered `EnvelopeGenerator.ReceiverUI.PredefinedReports.Report` as a trusted class for deserialization. Enhanced `CustomReportProvider` to fetch reports from memory or generate them dynamically using `ReportsFactory`. Added necessary `using` directives and ensured proper report lifecycle management.
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
using DevExpress.XtraReports.UI;
|
||||
using DevExpress.XtraReports.Web.Extensions;
|
||||
using EnvelopeGenerator.ReceiverUI.PredefinedReports;
|
||||
|
||||
namespace EnvelopeGenerator.ReceiverUI.Services;
|
||||
|
||||
public class InMemoryReportStorageWebExtension : ReportStorageWebExtension
|
||||
{
|
||||
private const string DefaultReportName = "LargeDatasetReport";
|
||||
private static readonly Dictionary<string, byte[]> Reports = new(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
public override bool CanSetData(string url) => IsValidUrl(url);
|
||||
|
||||
public override byte[] GetData(string url)
|
||||
{
|
||||
url = NormalizeUrl(url);
|
||||
|
||||
if (Reports.TryGetValue(url, out var reportLayout))
|
||||
return reportLayout;
|
||||
|
||||
if (ReportsFactory.Reports.TryGetValue(url, out var reportFactory))
|
||||
return SaveReport(reportFactory());
|
||||
|
||||
throw new DevExpress.XtraReports.Web.ClientControls.FaultException($"Report '{url}' was not found.");
|
||||
}
|
||||
|
||||
public override Dictionary<string, string> GetUrls()
|
||||
{
|
||||
var urls = ReportsFactory.Reports.Keys
|
||||
.Concat(Reports.Keys)
|
||||
.Distinct(StringComparer.OrdinalIgnoreCase)
|
||||
.ToDictionary(name => name, name => name, StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
return urls;
|
||||
}
|
||||
|
||||
public override bool IsValidUrl(string url)
|
||||
{
|
||||
return !string.IsNullOrWhiteSpace(url)
|
||||
&& url.IndexOfAny(Path.GetInvalidFileNameChars()) < 0;
|
||||
}
|
||||
|
||||
public override void SetData(XtraReport report, string url)
|
||||
{
|
||||
url = NormalizeUrl(url);
|
||||
Reports[url] = SaveReport(report);
|
||||
}
|
||||
|
||||
public override string SetNewData(XtraReport report, string defaultUrl)
|
||||
{
|
||||
var url = NormalizeUrl(defaultUrl);
|
||||
Reports[url] = SaveReport(report);
|
||||
return url;
|
||||
}
|
||||
|
||||
public bool TryGetReport(string url, out XtraReport report)
|
||||
{
|
||||
url = NormalizeUrl(url);
|
||||
|
||||
if (!Reports.ContainsKey(url))
|
||||
{
|
||||
report = null!;
|
||||
return false;
|
||||
}
|
||||
|
||||
using var stream = new MemoryStream(Reports[url]);
|
||||
report = XtraReport.FromXmlStream(stream, true);
|
||||
report.Name = url;
|
||||
return true;
|
||||
}
|
||||
|
||||
private static string NormalizeUrl(string url)
|
||||
{
|
||||
return string.IsNullOrWhiteSpace(url) ? DefaultReportName : url;
|
||||
}
|
||||
|
||||
private static byte[] SaveReport(XtraReport report)
|
||||
{
|
||||
using var stream = new MemoryStream();
|
||||
report.SaveLayoutToXml(stream);
|
||||
return stream.ToArray();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user