I am trying to create a fairly simple winform application to download similar data from several different systems, and then display the data in different views / reports. This is the first time I'm using the Entity Framework, and I'm not sure how these "report" views fit into the ORM framework.
The model is extremely simple, a basic abstract class called Trade is implemented as several specific classes with system data. As far as I understand, I can show all transactions in a datagrid, displayed as a base class, and specific implementations (for example, SystemATrade) if the user wants to see additional fields (thanks to this SO question ).
public abstract class BaseTrade { [Key, Column(Order=0)] public string SourceSystem { get; set; } [Key, Column(Order = 1)] public string TradeID {get; set;} public DateTime TradeDate { get; set; } public string Buy_Sell { get; set; } public decimal Quantity { get; set; } public decimal Price { get; set; } } public partial class SystemATrade : Trade { public string Field1 { get; set; } public string Field2 { get; set; } } public partial class SystemBTrade : Trade { public string Field3 { get; set; } public string Field4 { get; set; } } public class TradeComplianceContext : DbContext { public DbSet<BaseTrade> Trades { get; set; } public TradeComplianceContext() { } }
Now I want to add some custom views, such as Self-join Trades on TradeDate and Quantity, to show similar deals similar to this:
_context = new TradeComplianceContext(); var query = from t1 in _context.Trades join t2 in _context.Trades on new { Quantity = t1.Quantity, TradeDate = t1.TradeDate } equals new { Quantity = t2.Quantity, TradeDate = t2.TradeDate } where t1.Buy_Sell.Equals("P") && t2.Buy_Sell.Equals("S") select new { t1.SourceSystem, t1.TradeDate, t1.Quantity, t1.TradeID, t1.Price, t1.TraderID, t1.TraderName, T2_TradeID = t2.TradeID, T2_Price = t2.Price, T2_TraderID = t2.TraderID, T2_TraderName = t2.TraderName }; return query.ToList();
This code exists in my business logic layer and returns the list to the user interface layer that assigns the DataGrid.DataSource list. It works, but it does not seem right. Is it preferable to create POCO for the anonymous type embedded in the linq query and move this logic to DAL? Will this be done with the EDMX file and can it be in parallel with CodeFirst POCOs?
Secondary: if possible, I would like to create a polymorphic report similar to BaseTrade / SystemATrade and include additional inherited fields if the user would prefer to view the report as SystemATrade, and not just BaseTrade.