Best Practices

Posts   
 
    
Posts: 9
Joined: 16-Jul-2008
# Posted on: 16-Nov-2009 15:24:54   

I've often struggled with figuring out the "right" way to do things. Once thing specific is that which way is to update a user is better?

Calling User.Update(UserID,FirstName,LastName, etc)

or

Opening up the properties to do:

Dim MyUser as New User(UserID) MyUser.FirstName = FirstName MyUser.LastName = LastName MyUser.Save

Any thoughts on the subject?

psandler
User
Posts: 540
Joined: 22-Feb-2005
# Posted on: 16-Nov-2009 16:09:35   

In my opinion, setters make the most sense for this kind of thing. In the case of an entity, it's very likely you want/need the ability to change just one field at a time. For example, if the user got married and changed her last name, would you want your code to send all the fields (with their current values) into an update method? Also, the update method could get really ugly if your user object had 15 fields on it.

In many/most cases, I avoid allowing changing the state of an object via setters, but when you're talking about a class whose main purpose is to store data (and expose it at a field level), it makes perfect sense to use them.

Phil