Sunday, August 26, 2018

Where should you aggregate data in an ASP.NET Core MVC app?

I'm working on my first .NET Core MVC application with EF Core. I'm wondering what the best practice is for aggregating data.

I have a SalesRep and Sales table. And then a table that combines the two (a sale could have multiple sales reps). What if I want to get the number of Sales by a SalesRep and display that in the View? To this point, I have Models reflecting each table. And I have a ViewModel that would include the SalesRep name and then the total sales.

Where should the aggregation of the sales happen?

It seems like doing that at the EF Core level would be fastest, but then I would be returning a Model that feels more coupled to the UI. Instead, I could get the SalesRep record from a Service class and then go get all the Sales records from a different Service class and take the count. And then map the data into a ViewModel. But it feels a little wasteful.

What are people's recommendations for solving that kind of problem?

BONUS QUESTION: Do people typically take a Model that contains a foreign key, and hydrate it via EF Core to include the full object? I saw that example in Microsoft's docs, but they typically don't include great architectural patterns. So I didn't know if that was something people actually did.

Where should you aggregate data in an ASP.NET Core MVC app? Click here
  • Blogger Comment
  • Facebook Comment

0 comments:

Post a Comment

The webdev Team