I am refactoring a base class, implementations of which are plug-ins, loaded at runtime by another class using reflection.
The class I am refactoring uses a user ID of the form domain\user. I need to extend it to also work with domain email addresses.
To do this, I have introduced an enum of user identifier types, e.g UserIdentifierType.DomainUser, UserIdentifierType.SmtpAddress, UserIdentifierType.SomeFutureType
.
In order for the class to work, the user identifier type must be set.
My usual approach would be to create a constructor which takes the required identifier type as an argument but the problem I have is that the class consuming it only knows how to call the empty constructor. It is not feasible to change the consuming class.
Instead, I have implemented an initialising method:
public void Initialise(UserIdentifierType userIdentifierType)
{
_initialised = true;
}
Each method of the base class checks that the initialiser has been called and throws an exception if not.
if(!_initialised){throw new Exception("The Initialise() method must be called before using any other methods of this class");}
To me, that smells worse than my socks! It's not transparent, it's not self documenting and doesn't fit the principles of OOP.
Is there some neater way to accomplish setting the identifier type in the base instance without using a constructor or an initialiser?
.Create(identifer)
instead ofInitialize
to get an actual instance. Creating objects in an invalid state is never a good idea. – amon Nov 15 '14 at 15:00:base.Create(identifier)
in the class constructor? – Simon Nov 15 '14 at 15:26