.NET - ORM and all possible combinations - ViewModel?

How do you approach this problem with ORM? This is a hypothetical (simplified) example:

I have a table of cities:
1 - New York 2 - London 3 - San Francisco 4 - New Orleans

I have a score table: (first column Unique primary key, second month code, third FK for the city, fourth account (int))
1 - 352 - 1 - 9
2 - 352 - 2 - 10

For the month of 352, only New York and London are mentioned.

When I present this to the user in the user interface, I need a grid showing all 4 cities for this month. And zeros (spaces) for the cities of San Fran and New Orleans.

What is the best approach for this with ORM? Return your business objects to a "model" view, and then convert them to a "viewmodel"? How did you deal with similar situations in ORM? I deal with this earlier in ADO.NET with my SQL statements, but have never done it in ORM, and I'm looking for tips, tricks, or an approach.

+4
source share
2 answers

In fact, we are practicing a form for splitting command requests (the Greg Young version is not the Meyer version), so at least we will use the NHibernate ICriteria to select the parts we need, and then use the AliasToBeanTransformer to directly enter them into the DTO.

For our most complex objects, we actually have a split table with all the details that the screen should display minimized in one row (also known as OLAP). Then we can run queries directly to this table, which skips the cost of loading a complex object that contains much more information than our screen requires.

+2
source

I use LINQ to map domain objects to view models. (I use the Entity Framework, but this technique works in any ORM with good LINQ support.) First write a POCO view model and then project onto it:

var pm = from c in Context.Cities let score = c.Scores.Where(s => s.MonthCode == selectedMonthCode).FirstOrDefault() select new CityScoresPresentation { City = c.Name, Score = score.Score }; 

This is LINQ to Entities. LINQ to SQL works similarly. YMMV with other ORM LINQ implementations.

I highly recommend using a view model instead of binding views to types that support ORM.

0
source

All Articles