I need some opinions on choosing which signatures for my web based business layer function:
function void CreateUser(Company UserCompany, string name…)
or
function void CreateUser(int CompanyID, string name…)
If this is a window based I would choose function to take object instead of int because since the company already loaded to the window form, why not just utilize it. Also, I think business layer should take business objects and it’s type safe(preventing accidently pass in 0 or -1).
But on the other hand, if this is a web based application, only id will be loaded to client sites. So it’s kind of redundant to load company object because eventually only companyID will be saved in the dabasebase anyway.
Hopefully my description is not too confusing =P
Thank you for reading.
I think you answered your own question, it depends on the presentation layer you’re using. But when you want to keep this business layer generic, you’ll have to go with the ID.
Here’s another idea. I personally like to wrap all mutations into classes, that I call commands. You could for instance have an
CreateUserCommandthat inherits from a base command. This would allow you to use it like this (I’m assuming C# here):When wrapping this logic in a command is very nice pattern. It allows you to put a single use case in a single command. Especially when the amount of logic in a command grows you will appreciate this pattern, but I found it to be very effective for CRUD operations as well.
This pattern also allows you to abstract the transactional model away in the base class. The base class can wrap a call to execute in a database transaction. Here is an simple implementation:
And this is what the
CreateUserCommandwould look like:I hope this helps.