This is something that has been pulling at me for a while. Consider a (MVC type) web application with an ORM (e.g. Nhiberate) as the data access layer.
**On one hand** - *the OOP/Rich domain model hand* - I feel I should be passing around (references to) the real objects I am talking about.
**On the other hand** - *the DB/Web App hand* - I feel that it is easier and more efficient just to pass the integer Ids of the objects rather than the object themselves.
***Consider an ecommerce catalogue type application:***
- The user is logged in and navigates to a product page.
- They post a comment.
- The controller action tasked with persisting this comment has 3 pieces of information: a) The user id (from the auth cookie or wherever), b) The product id (probably from the querystring), and c) the comment text.
- Now, what what is best practice here? Is it really worth inflating the user and product objects (e.g. by getting them from the repository, with all the DB work that entails) when we know that all they will be used for is so the ORM can read their IDs and set the appropriate foreign keys in the DB table that stores the comments?
*What are peoples views on this? Perhaps web apps should be given a little more leway than other apps, due to their stateless nature? I imagine there will be 'it depends' answers, but maybe some people are purists about the issue.*
This is a general question which probably is applicable to many platforms, but **if giving examples I would prefer them to be ASP.NET MVC if possible**.
Thank you. How is it easier to "just" pass around id's rather than references? Also, aside from fetching an extra entity from the database, is this more performant in any way?
以上就是Nhibernate (and ORMs in General): work with Objects or ObjectIds?的详细内容，更多请关注web前端其它相关文章！