3 ms·
I just find that when organizing code it makes most sense to organize it after what it does. Example: E.g., an enrollment flow may be setting the user's name f
by dagss 4y ago
I just find that when organizing code it makes most sense to organize it after what it does. Example:
E.g., an enrollment flow may be setting the user's name for one reason, and a database import job fetch the user's name for a second reason, and an internal support tool the set the user's name for a third reason.
In either case the job of the backend is to "set the name", to take an action. (Either by inserting a row to UserNameChangedInSupportTool, or by doing an UPDATE to User.Name, depending on whether you are doing event sourcing or not).
Typically the "set the name" is a very small detail in a larger context (enrollment flow; support tool use; import job). So in this example I would likely organize the code after those tasks done / problems solved.
My point is really that in these 3 functions I find it doesn't lead to cleaner code to have some sort of mirror (in principle out-of-date the moment you load it) of the "User object" in the memory, and modify it, and "persist" it
Just focus on what you need to do (like, "change the name of the user") and execute that towards the database from the Function you are performing. The User lives in the database, not in an object in your backend.
I.e., "update User set Name = @newName where Id = @uid and Name = @oldName" (action to change the user's name, if no-one raced us), instead of "load user", "set user name", "save user" (OOP, with an impedance mismatch because there is one version in your backend and another in the DB)
- mbreese 4y agoOkay, I think I get what you're trying to say now. I hate... hate... hate... ORMs where you alter objects and then let the ORM magically persist the changes to the database. That is just a level of abstraction that I'm not comfortable with. I think it pushes the developer too far away from the data layer. I still think that the object model does have utility, but like everything, when taken too the logical ends, you end up with ORM managed objects. Instead I've always liked using a Service pattern, where you have data objects that are (largely) immutable. Then you have Service objects that contain the methods that operate on the data objects. I'm just not sure I'd have called it a relational model (hence my confusion).