简体   繁体   中英

ASP.Net MVC, ORM and “heavy” objects

i think this issue is common in web applications with a middle-sized model. Let's say I have a SportCenter class holding a list of BasketballField, when showing reservations or properties of a BasketballField I still want to show few information about the SportCenter it belongs to.

I'm using ASP.Net MVC and nHibernate for the data layer, so my question is: is it worth to make nHibernate load a whole SportCenter instance (actually contained collections are lazy-loaded but still the class is "heavy") together with my BasketballField and its infos just to show just few fields of the SportCenter?

On the other hand, building very fine-tuned queries in HQL take me back to old Classic ASP days with hand-made SQL queries...

Any best-practice to suggest?

Thank you all, Peter.

Try it, run it, profile it. Try both ways, and use a profile like Red Gate's ANTS Profiler to see whether there is a noticeable performance difference.

If there isn't that much of a difference, then use the one that is more readable - using the SportCenter class - otherwise, use HQL.

IMHO, OR/Ms, in their current state, aren't a complete replacement for SQL/variants.

If your querying it from the BasketballField end then each BasketballField can either "fetch join" the SportCentre, or turn lazy loading off from the BasketballField end (as each BasketballField will only have one SportCentre).

The major overheads I've experienced with this come from nHibernate thrashing the database with lots of small lazy loads, the solution is normally to make it return all the data in a single roundtrip.

I'm becoming a strong believer in Command/Query Separation (the Greg Young version) so I would say if you want to display data to the user (Query) you should definitely not be using your domain model. Getters are quite often an anti pattern when it comes to a good domain.

My currently preferred process is to use screen specific DTOs populated by NHibernate. These work best when driven off a separate table but you can use NHibernates AliasToBeanTransformer to populate from your domain table.

Consider Linq. NHibernate supports Linq. With Linq it's easy to write type safe queries that return "partial objects" or whatever fields you want really.

The other two options you presented are probably "good enough" and should work.

As person-b said, use a profiler if you are concerned with performance.

View specific DTOs are a best practice and probably make sense here.

Your problem is that you are exposing your Domain to UI layer. You should separate your Model from your ViewModel.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM