开发者

Lazy Loading Question

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 GetU开发者_如何学运维serDetails Method


As the userAccount will have to be initialized somehow [at least passing account number], I would prefer to use second one.


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

public class Account
{
  public UserAccount Details {get; private set;}
}

Here is an example

public class Account 
{ 
  public UserAccount UserDetails {get; private set;}
  public UserOtherDetails OtherDetails {get; private set;} 

  public Account (UserAccount userDetails, UserOtherDetails otherDetails)
  {
    this.UserDetails = userDetails;
    this.OtherDetails = otherDetails;
  }
} 

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.

public class Account 
{ 
  private UserAccount _userDetails;
  private UserOtherDetails _otherDetails;

  public GetUserAccountDetails(string accountId)
  {
    if (_userDetails == null)
    {
      // This could be a nice place to look at various factory patterns.
      _userDetails = new UserAccount();
      _userDetails.Load(accountId);
    }
    return _userDetails;
  }

  ...
} 


I don't work with C#, but personally I am not totally for this kind of aggregation. You would use something like that when you need some other implementation other than what UserAccount::GetDetails() provide or if you want Account to be a factory for different kinds of more specific accounts.

Going back to the question, I prefer the second approach if that helps you somehow.


Getters and setters would be the easy way to go:

public class User
{
    public string Name { get; set; }
    public string TagLine { get; set; }
}

public class Account
{
    public User Owner { get; set; }
}

// then...
Console.WriteLine(account.Owner.Name);
Console.WriteLine(account.Owner.TagLine);


As always: It depends.

The first example creates a new instance of UserAccount for every call, whereas the second example reuses an instance that was created previously. Answering which option is better is not possible without knowing what UserAccount represents and what it's constructor does.

Are you considering option 1 because you want to avoid creating a UserAccount object if the method is never called? In that case, lazy loading might be what you want:

private UserAccount userAccount;

public Details GetUserDetails()
{
    if (userAccount == null)
        userAccount = new UserAccount();
    return userAccount.GetDetails();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜