Ervin E
Ervin E

Reputation: 452

Performance benefit? Passing Class instances or just IDs. (ASP.net MVC3)

The small web application I am working on is becoming bigger and bigger. I've noticed that when posting forms or just calling other functions I've passed parameters that consist of IDs or a whole instance of a Model class.

In a performance stand point, is it better for me to pass the whole Model object (filled with values) or should I pass the ID, then retrieve from the database?

Thanks!

Upvotes: 0

Views: 70

Answers (3)

Amit Prajapati
Amit Prajapati

Reputation: 332

For Performance benefits, you can do lot of things, common things are

1) Fetch as many as records which are needed, e.g. customized paging, in LINQ use (skip and take methods)

2) Use Data caching in controllers and Cache dependencies for Lists which are bound with View

3) Use Compiled query to fetch records. (see here)

Apply all these and see the mark-able page load speed.

EDIt: For IDs recommendations, In this question, Both will be same performance impact if you pass only ID and fetch rest of the model from database OR pass filled model.

Upvotes: 1

Slicksim
Slicksim

Reputation: 7172

It is always best to consider these from the use case. For example, if I want to get an item by ID, then I pass the ID, not the whole object with the ID filled out.

I use WCF services to host my BLL and interface to my DAL, so passing data around is a costly exercise, so I do it sparingly.

If I need to update an object, I pass the object, if I just want to perform an action on an object, such as delete or get, I use the ID.

Si

Upvotes: 0

Alfredo Cavalcanti
Alfredo Cavalcanti

Reputation: 517

Do not solve problems which do not exist yet. Use a tool to measure the performance problem and then try to solve.

Upvotes: 0

Related Questions