Data Transfer Objects and Reporting

690 views Asked by At

I have these scenario:

//This class maps to the "Item" table in the database
public class Item
{
   public int itemID { get; set;}
   public string itemName { get; set; }
   public int quantity { get; set; }
}

//Items record are returned from the database as list    
List<Item> items = ItemDAO.GetItems();

I want to generate a report that include aggregate values (from query/Stored Procedure)

ItemID| ItemName | Qty | QtySoldThisMonth | QtySoldThisYr
-----------------------------------------------------------
123   | Vit. C   |  20 |   55             |    400
239   | Maloxin  |  25 |   12             |    210

The Item class does not have these extra fields This would have been easier with datatable which could have varying number of columns but I am using a list

Please, how would I go about this

3

There are 3 answers

2
Dirk Trilsbeek On BEST ANSWER

you can always create another DTO specifically for that aggregated view and use that in your reports.

A little more general solution than a specific DTO for each view would be to create a DTO for reports on Item and use a collection of fields instead of class properties to store aggregate values in the DTO.

For instance:

 class AggregatedReportField {
     public string FieldName { get; set; }
     public decimal FieldValue { get; set; }
 }

 class ItemReportDTO {

     public Dictionary<string, AggregatedReportField> ReportFields { get; private set; }

     public ItemReportDTO()
     {
         ReportFields = new Dictionary<string, AggregatedReportField>();
     }

     public void Add(AggregatedReportField field)
     {
         if (!ReportFields.ContainsKey(fieldl.FieldName))
              ReportFields.Add(field.FieldName, field);
     }
 }

in your DAO, you would then first get the values from the database, create AggregatedReportField instances for each value and add them with a name to your DTO. In your report, you would then have to refer to your aggregated values with DTO.ReportFields["SomeValue"].FieldValue.

the code above is just an example and contains a few bad practices (exposed Dictionary<>).

0
dbf On

I suggest you to separate entities (like item) and report rows (that contains additional fields).

I think, that you have to options:

  1. Create separate class to represent each report (or row of the report)
  2. Create class to represent all reports (it will contain metadata about columns and all data)

What you should choose depends on your requirements. These reports can use DAO and aggregate values in java code, or call stored functions directly without working with DAO.

0
Shoaib Shaikh On

I think in your case if you want some BLL class library to give you compiled reports in List of DTOs you should be create specialized DTOs for report. all you can do is to use class inheritance to avoid redundant code. That is all you can have at it best.

public class AggregatedItem:Item
{
public int QtyMonth {get;set;}
public int QtyYear {get;set;}
}

Regards.