what would be better to do
public ActionResult Test()
{
User user = accountService.GetUser(User.Identity)
Product project = productService.GetSomething(user);
Store store = storeService.GetSomething(user);
}
Or
public ActionResult Test()
{
Product project = productService.GetSomething(User.Identity);
Store store = storeService.GetSomething(User.Identity);
}
In the first one I have a service layer that takes in the userName and calls up a repo to get the user record back. Then I pass it along and use it in other service layers.
Now in option 2 I pass in the username and in that service layer I call the same repo the accountService would call.
Only difference is I am passing in a string vs and object.
I see both have up sides and downsides. If your just having one method that needs the users data then it kinda sucks to have to call another service layer to get it and pass it into another one.
If you have multiple ones it might potentially save some queries to the database(however I nhibernate might just cache it anyways)
Law of Demeter : http://en.wikipedia.org/wiki/Law_of_Demeter
Or even better, look at this part of the excellent Clean Code Talk from Google: http://www.youtube.com/watch?feature=player_detailpage&v=RlfLCWKxHJ0#t=944s
There, Law of demeters gets epxlained very well. Or just view the whole video, they are all good btw.
In short, passing the smallest possible component makes the code more readable, more expressive, more testable. The “bigger” the object, the more it hides the intention of the method/constructor using it. And the harder/more complicated it is to create a stub/mock later on, especially because your own classes tend to “grow” over time. If you don’t have to create stubs at all (as you use e.g. value types like in your case instead of reference types) it’s the best you can achieve.