mardi 16 juin 2015

Nested classes for a multiple-phase state

I would like to use nested classes for a "developing state":

public class WebSvcClient : IDisposable
{
    //
    // 10 private fields required by all methods
    ...

    public Profile Profile { get; private set; }
    public void Authenticate()
    {
        this.Profile = new Profile();
    }
    public class Profile
    {
        // public methods that do require authentication
        ...
    }

    // public methods that don't require authentication
    ...
}  

I can see several benefits:

  • Encapsulation, Profile makes sense only in the context of WebSvcClient
  • Single IDisposable unit
  • Lack of need for 3rd class just to pass the private fields to Profile
  • No pollution of namespace

But is it considered good practice?

Aucun commentaire:

Enregistrer un commentaire