Which is the better approach
public class Account
{
public UserAccount GetUserDetails(string acctId)
{
return new UserAccount().GetDetails(); //call method from class UserAccount
}
public UserAccount GetUserDetails(string acctId)
{
return new UserOtherDetails().GetDetails(); //call method from another class
}
}
or contain the class like this
public class Account
{
private UserAccount userAccount; //contain UserAccount class
private UserOtherDetails userOtherDetails;
public UserAccount GetUserDetails(string acctId)
{
return userAccount.GetDetails(); //invoke the method from UserAccount class
}
public UserOtherDetails GetOtherDetails(string acctId)
{
return new userOtherDetails().GetDetails(); //call method from another class
}
}
or any other approach guys?
EDIT: what if I Add parameters on the method?
Additional: My point here is I don’t want to instantiate UserOtherDetails class when I’m calling the GetUserDetails Method
You second approach is better. The first approach is nasty because everytime you call GetUserDetails a new instance of the UserAccount class will be created.
You could also just expose this as a property
Here is an example
The above assumes you have already loaded the UserDetails and UserOtherDetails. If you want to load the details on demand then you might do something like this.