currently, in my team, we have very live debate about some issues in our asp.net mvc project.
to cut a long story short, it’s all about should we be using objects as a some kind of container for parameters to call a methods in our project…other side(me :)) is more on the side that methods should be called with primitive parameters…
what do you think and what is for you the best approach, and in which cases?
thank you 🙂
Yes, models/objects should be passed to methods. It’s how the framework was intended to be used – hence the built-in model binder.
The approach of passing objects as parameters also allows you to refactor and change the details of your implementation without having to alter the method signature throughout your code if say, you wanted to include a new item of data in your method call, you could simply add a new property to your model object, rather than alter method signatures. It’s a much more flexible and easier to read approach.
In contrast, I’m sure you will have seen method signatures in ‘legacy’ codebases that get really out of hand in projects of any significant age where developers keep adding parameters as they need them, rather than use an object (not my code, I should add, lest my reputation be ruined!) – This kind of thing can get quickly out of hand when code is being rapidly developed and can lead to code rot.
Finally, on a more general note about method parameters, if you start to have more than say 4, or 5, then this is usually an indication that your method is doing too much and needs to be refactored with respect to SOC (Separation of Concerns).