Refactor report creation and property initialization
Replaced nullable fields with non-nullable properties for `Report` and `BasePdfBytes` to improve initialization and encapsulation. Updated `OnInitializedAsync` to use `BasePdfBytes` and added error handling for missing PDF bytes. Refactored report creation logic by removing `BuildFreshBaseReport` and `CreateReportInstance`, consolidating functionality into a new `CreateReport` method. Enhanced `CreateReport` to support both predefined and dynamically generated reports, simplifying the code structure and improving maintainability.
This commit is contained in:
@@ -19,34 +19,37 @@
|
||||
@code {
|
||||
[Parameter] public string EnvelopeKey { get; set; } = string.Empty;
|
||||
|
||||
XtraReport? Report;
|
||||
byte[]? _basePdfBytes;
|
||||
XtraReport Report { get; set; } = null!;
|
||||
|
||||
byte[] BasePdfBytes { get; set; } = null!;
|
||||
|
||||
protected override async Task OnInitializedAsync() {
|
||||
if (!AppOptions.Value.UsePredefinedReports) {
|
||||
var (pdfBytes, _) = await DocumentService.GetDocumentAsync(EnvelopeKey);
|
||||
if (pdfBytes is { Length: > 0 })
|
||||
_basePdfBytes = pdfBytes;
|
||||
BasePdfBytes = pdfBytes;
|
||||
else
|
||||
throw new InvalidOperationException($"No PDF bytes found for EnvelopeKey: {EnvelopeKey}");
|
||||
}
|
||||
|
||||
Report = BuildFreshBaseReport();
|
||||
Report = CreateReport();
|
||||
}
|
||||
|
||||
XtraReport BuildFreshBaseReport() {
|
||||
if (_basePdfBytes is { Length: > 0 }) {
|
||||
XtraReport CreateReport()
|
||||
{
|
||||
if (AppOptions.Value.UsePredefinedReports)
|
||||
{
|
||||
return ReportStorage.TryGetReport("LargeDatasetReport", out var savedReport)
|
||||
? savedReport
|
||||
: PredefinedReports.ReportsFactory.GetReport("LargeDatasetReport");
|
||||
}
|
||||
else
|
||||
{
|
||||
var report = new XtraReport();
|
||||
var detail = new DevExpress.XtraReports.UI.DetailBand();
|
||||
report.Bands.Add(detail);
|
||||
detail.Controls.Add(new DevExpress.XtraReports.UI.XRPdfContent { Source = _basePdfBytes, GenerateOwnPages = true });
|
||||
detail.Controls.Add(new DevExpress.XtraReports.UI.XRPdfContent { Source = BasePdfBytes, GenerateOwnPages = true });
|
||||
return report;
|
||||
}
|
||||
return CreateReportInstance();
|
||||
}
|
||||
|
||||
XtraReport CreateReportInstance() {
|
||||
return ReportStorage.TryGetReport("LargeDatasetReport", out var savedReport)
|
||||
? savedReport
|
||||
: PredefinedReports.ReportsFactory.GetReport("LargeDatasetReport");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user